2 URL: svn://svnanon.samba.org/samba/branches/SAMBA_4_0/source/lib/tdb/common
4 Last Changed Date: 2007-06-22 13:36:10 -0400 (Fri, 22 Jun 2007)
7 trivial database library - standalone version
9 Copyright (C) Andrew Tridgell 1999-2005
10 Copyright (C) Jeremy Allison 2000-2006
11 Copyright (C) Paul `Rusty' Russell 2000
13 ** NOTE! The following LGPL license applies to the tdb
14 ** library. This does NOT imply that all of Samba is released
17 This library is free software; you can redistribute it and/or
18 modify it under the terms of the GNU Lesser General Public
19 License as published by the Free Software Foundation; either
20 version 2 of the License, or (at your option) any later version.
22 This library is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 Lesser General Public License for more details.
27 You should have received a copy of the GNU Lesser General Public
28 License along with this library; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 #ifdef CONFIG_STAND_ALONE
35 #define HAVE_SYS_MMAN_H
40 #define _XOPEN_SOURCE 600
51 #ifdef HAVE_SYS_SELECT_H
52 #include <sys/select.h>
55 #include <sys/types.h>
64 #ifdef HAVE_SYS_MMAN_H
73 #define MAP_FAILED ((void *)-1)
77 #define strdup rep_strdup
78 static char *rep_strdup(const char *s
)
88 ret
= malloc(length
+ 1);
90 strncpy(ret
, s
, length
);
97 #ifndef PRINTF_ATTRIBUTE
98 #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 )
99 /** Use gcc attribute to check printf fns. a1 is the 1-based index of
100 * the parameter containing the format, and a2 the index of the first
101 * argument. Note that some gcc 2.x versions don't handle this
103 #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
105 #define PRINTF_ATTRIBUTE(a1, a2)
113 static TDB_DATA tdb_null
;
119 typedef u32 tdb_len_t
;
120 typedef u32 tdb_off_t
;
123 #define offsetof(t,f) ((unsigned int)&((t *)0)->f)
126 #define TDB_MAGIC_FOOD "TDB file\n"
127 #define TDB_VERSION (0x26011967 + 6)
128 #define TDB_MAGIC (0x26011999U)
129 #define TDB_FREE_MAGIC (~TDB_MAGIC)
130 #define TDB_DEAD_MAGIC (0xFEE1DEAD)
131 #define TDB_RECOVERY_MAGIC (0xf53bc0e7U)
132 #define TDB_ALIGNMENT 4
133 #define MIN_REC_SIZE (2*sizeof(struct list_struct) + TDB_ALIGNMENT)
134 #define DEFAULT_HASH_SIZE 131
135 #define FREELIST_TOP (sizeof(struct tdb_header))
136 #define TDB_ALIGN(x,a) (((x) + (a)-1) & ~((a)-1))
137 #define TDB_BYTEREV(x) (((((x)&0xff)<<24)|((x)&0xFF00)<<8)|(((x)>>8)&0xFF00)|((x)>>24))
138 #define TDB_DEAD(r) ((r)->magic == TDB_DEAD_MAGIC)
139 #define TDB_BAD_MAGIC(r) ((r)->magic != TDB_MAGIC && !TDB_DEAD(r))
140 #define TDB_HASH_TOP(hash) (FREELIST_TOP + (BUCKET(hash)+1)*sizeof(tdb_off_t))
141 #define TDB_HASHTABLE_SIZE(tdb) ((tdb->header.hash_size+1)*sizeof(tdb_off_t))
142 #define TDB_DATA_START(hash_size) TDB_HASH_TOP(hash_size-1)
143 #define TDB_RECOVERY_HEAD offsetof(struct tdb_header, recovery_start)
144 #define TDB_SEQNUM_OFS offsetof(struct tdb_header, sequence_number)
145 #define TDB_PAD_BYTE 0x42
146 #define TDB_PAD_U32 0x42424242
148 /* NB assumes there is a local variable called "tdb" that is the
149 * current context, also takes doubly-parenthesized print-style
151 #define TDB_LOG(x) tdb->log.log_fn x
154 #define GLOBAL_LOCK 0
155 #define ACTIVE_LOCK 4
156 #define TRANSACTION_LOCK 8
158 /* free memory if the pointer is valid and zero the pointer */
160 #define SAFE_FREE(x) do { if ((x) != NULL) {free(x); (x)=NULL;} } while(0)
163 #define BUCKET(hash) ((hash) % tdb->header.hash_size)
165 #define DOCONV() (tdb->flags & TDB_CONVERT)
166 #define CONVERT(x) (DOCONV() ? tdb_convert(&x, sizeof(x)) : &x)
169 /* the body of the database is made of one list_struct for the free space
170 plus a separate data list for each hash value */
172 tdb_off_t next
; /* offset of the next record in the list */
173 tdb_len_t rec_len
; /* total byte length of record */
174 tdb_len_t key_len
; /* byte length of key */
175 tdb_len_t data_len
; /* byte length of data */
176 u32 full_hash
; /* the full 32 bit hash of the key */
177 u32 magic
; /* try to catch errors */
178 /* the following union is implied:
180 char record[rec_len];
185 u32 totalsize; (tailer)
191 /* this is stored at the front of every database */
193 char magic_food
[32]; /* for /etc/magic */
194 u32 version
; /* version of the code */
195 u32 hash_size
; /* number of hash entries */
196 tdb_off_t rwlocks
; /* obsolete - kept to detect old formats */
197 tdb_off_t recovery_start
; /* offset of transaction recovery region */
198 tdb_off_t sequence_number
; /* used when TDB_SEQNUM is set */
199 tdb_off_t reserved
[29];
202 struct tdb_lock_type
{
208 struct tdb_traverse_lock
{
209 struct tdb_traverse_lock
*next
;
217 int (*tdb_read
)(struct tdb_context
*, tdb_off_t
, void *, tdb_len_t
, int );
218 int (*tdb_write
)(struct tdb_context
*, tdb_off_t
, const void *, tdb_len_t
);
219 void (*next_hash_chain
)(struct tdb_context
*, u32
*);
220 int (*tdb_oob
)(struct tdb_context
*, tdb_off_t
, int );
221 int (*tdb_expand_file
)(struct tdb_context
*, tdb_off_t
, tdb_off_t
);
222 int (*tdb_brlock
)(struct tdb_context
*, tdb_off_t
, int, int, int, size_t);
226 char *name
; /* the name of the database */
227 void *map_ptr
; /* where it is currently mapped */
228 int fd
; /* open file descriptor for the database */
229 tdb_len_t map_size
; /* how much space has been mapped */
230 int read_only
; /* opened read-only */
231 int traverse_read
; /* read-only traversal */
232 struct tdb_lock_type global_lock
;
234 struct tdb_lock_type
*lockrecs
; /* only real locks, all with count>0 */
235 enum TDB_ERROR ecode
; /* error code for last tdb error */
236 struct tdb_header header
; /* a cached copy of the header */
237 u32 flags
; /* the flags passed to tdb_open */
238 struct tdb_traverse_lock travlocks
; /* current traversal locks */
239 struct tdb_context
*next
; /* all tdbs to avoid multiple opens */
240 dev_t device
; /* uniquely identifies this tdb */
241 ino_t inode
; /* uniquely identifies this tdb */
242 struct tdb_logging_context log
;
243 unsigned int (*hash_fn
)(TDB_DATA
*key
);
244 int open_flags
; /* flags used in the open - needed by reopen */
245 unsigned int num_locks
; /* number of chain locks held */
246 const struct tdb_methods
*methods
;
247 struct tdb_transaction
*transaction
;
249 int max_dead_records
;
250 bool have_transaction_lock
;
251 tdb_len_t real_map_size
; /* how much space has been mapped */
258 static int tdb_munmap(struct tdb_context
*tdb
);
259 static void tdb_mmap(struct tdb_context
*tdb
);
260 static int tdb_lock(struct tdb_context
*tdb
, int list
, int ltype
);
261 static int tdb_unlock(struct tdb_context
*tdb
, int list
, int ltype
);
262 static int tdb_brlock(struct tdb_context
*tdb
, tdb_off_t offset
, int rw_type
, int lck_type
, int probe
, size_t len
);
263 static int tdb_transaction_lock(struct tdb_context
*tdb
, int ltype
);
264 static int tdb_transaction_unlock(struct tdb_context
*tdb
);
265 static int tdb_brlock_upgrade(struct tdb_context
*tdb
, tdb_off_t offset
, size_t len
);
266 static int tdb_write_lock_record(struct tdb_context
*tdb
, tdb_off_t off
);
267 static int tdb_write_unlock_record(struct tdb_context
*tdb
, tdb_off_t off
);
268 static int tdb_ofs_read(struct tdb_context
*tdb
, tdb_off_t offset
, tdb_off_t
*d
);
269 static int tdb_ofs_write(struct tdb_context
*tdb
, tdb_off_t offset
, tdb_off_t
*d
);
270 static void *tdb_convert(void *buf
, u32 size
);
271 static int tdb_free(struct tdb_context
*tdb
, tdb_off_t offset
, struct list_struct
*rec
);
272 static tdb_off_t
tdb_allocate(struct tdb_context
*tdb
, tdb_len_t length
, struct list_struct
*rec
);
273 static int tdb_ofs_read(struct tdb_context
*tdb
, tdb_off_t offset
, tdb_off_t
*d
);
274 static int tdb_ofs_write(struct tdb_context
*tdb
, tdb_off_t offset
, tdb_off_t
*d
);
275 static int tdb_lock_record(struct tdb_context
*tdb
, tdb_off_t off
);
276 static int tdb_unlock_record(struct tdb_context
*tdb
, tdb_off_t off
);
277 static int tdb_rec_read(struct tdb_context
*tdb
, tdb_off_t offset
, struct list_struct
*rec
);
278 static int tdb_rec_write(struct tdb_context
*tdb
, tdb_off_t offset
, struct list_struct
*rec
);
279 static int tdb_do_delete(struct tdb_context
*tdb
, tdb_off_t rec_ptr
, struct list_struct
*rec
);
280 static unsigned char *tdb_alloc_read(struct tdb_context
*tdb
, tdb_off_t offset
, tdb_len_t len
);
281 static int tdb_parse_data(struct tdb_context
*tdb
, TDB_DATA key
,
282 tdb_off_t offset
, tdb_len_t len
,
283 int (*parser
)(TDB_DATA key
, TDB_DATA data
,
286 static tdb_off_t
tdb_find_lock_hash(struct tdb_context
*tdb
, TDB_DATA key
, u32 hash
, int locktype
,
287 struct list_struct
*rec
);
288 static void tdb_io_init(struct tdb_context
*tdb
);
289 static int tdb_expand(struct tdb_context
*tdb
, tdb_off_t size
);
290 static int tdb_rec_free_read(struct tdb_context
*tdb
, tdb_off_t off
,
291 struct list_struct
*rec
);
296 enum TDB_ERROR
tdb_error(struct tdb_context
*tdb
)
301 static struct tdb_errname
{
302 enum TDB_ERROR ecode
; const char *estring
;
303 } emap
[] = { {TDB_SUCCESS
, "Success"},
304 {TDB_ERR_CORRUPT
, "Corrupt database"},
305 {TDB_ERR_IO
, "IO Error"},
306 {TDB_ERR_LOCK
, "Locking error"},
307 {TDB_ERR_OOM
, "Out of memory"},
308 {TDB_ERR_EXISTS
, "Record exists"},
309 {TDB_ERR_NOLOCK
, "Lock exists on other keys"},
310 {TDB_ERR_EINVAL
, "Invalid parameter"},
311 {TDB_ERR_NOEXIST
, "Record does not exist"},
312 {TDB_ERR_RDONLY
, "write not permitted"} };
314 /* Error string for the last tdb error */
315 const char *tdb_errorstr(struct tdb_context
*tdb
)
318 for (i
= 0; i
< sizeof(emap
) / sizeof(struct tdb_errname
); i
++)
319 if (tdb
->ecode
== emap
[i
].ecode
)
320 return emap
[i
].estring
;
321 return "Invalid error code";
326 #define TDB_MARK_LOCK 0x80000000
328 /* a byte range locking function - return 0 on success
329 this functions locks/unlocks 1 byte at the specified offset.
331 On error, errno is also set so that errors are passed back properly
334 note that a len of zero means lock to end of file
336 int tdb_brlock(struct tdb_context
*tdb
, tdb_off_t offset
,
337 int rw_type
, int lck_type
, int probe
, size_t len
)
342 if (tdb
->flags
& TDB_NOLOCK
) {
346 if ((rw_type
== F_WRLCK
) && (tdb
->read_only
|| tdb
->traverse_read
)) {
347 tdb
->ecode
= TDB_ERR_RDONLY
;
352 fl
.l_whence
= SEEK_SET
;
358 ret
= fcntl(tdb
->fd
,lck_type
,&fl
);
359 } while (ret
== -1 && errno
== EINTR
);
362 /* Generic lock error. errno set by fcntl.
363 * EAGAIN is an expected return from non-blocking
365 if (!probe
&& lck_type
!= F_SETLK
) {
366 /* Ensure error code is set for log fun to examine. */
367 tdb
->ecode
= TDB_ERR_LOCK
;
368 TDB_LOG((tdb
, TDB_DEBUG_TRACE
,"tdb_brlock failed (fd=%d) at offset %d rw_type=%d lck_type=%d len=%d\n",
369 tdb
->fd
, offset
, rw_type
, lck_type
, (int)len
));
371 return TDB_ERRCODE(TDB_ERR_LOCK
, -1);
378 upgrade a read lock to a write lock. This needs to be handled in a
379 special way as some OSes (such as solaris) have too conservative
380 deadlock detection and claim a deadlock when progress can be
381 made. For those OSes we may loop for a while.
383 int tdb_brlock_upgrade(struct tdb_context
*tdb
, tdb_off_t offset
, size_t len
)
388 if (tdb_brlock(tdb
, offset
, F_WRLCK
, F_SETLKW
, 1, len
) == 0) {
391 if (errno
!= EDEADLK
) {
394 /* sleep for as short a time as we can - more portable than usleep() */
397 select(0, NULL
, NULL
, NULL
, &tv
);
399 TDB_LOG((tdb
, TDB_DEBUG_TRACE
,"tdb_brlock_upgrade failed at offset %d\n", offset
));
404 /* lock a list in the database. list -1 is the alloc list */
405 static int _tdb_lock(struct tdb_context
*tdb
, int list
, int ltype
, int op
)
407 struct tdb_lock_type
*new_lck
;
409 bool mark_lock
= ((ltype
& TDB_MARK_LOCK
) == TDB_MARK_LOCK
);
411 ltype
&= ~TDB_MARK_LOCK
;
413 /* a global lock allows us to avoid per chain locks */
414 if (tdb
->global_lock
.count
&&
415 (ltype
== tdb
->global_lock
.ltype
|| ltype
== F_RDLCK
)) {
419 if (tdb
->global_lock
.count
) {
420 return TDB_ERRCODE(TDB_ERR_LOCK
, -1);
423 if (list
< -1 || list
>= (int)tdb
->header
.hash_size
) {
424 TDB_LOG((tdb
, TDB_DEBUG_ERROR
,"tdb_lock: invalid list %d for ltype=%d\n",
428 if (tdb
->flags
& TDB_NOLOCK
)
431 for (i
=0; i
<tdb
->num_lockrecs
; i
++) {
432 if (tdb
->lockrecs
[i
].list
== list
) {
433 if (tdb
->lockrecs
[i
].count
== 0) {
435 * Can't happen, see tdb_unlock(). It should
438 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_lock: "
439 "lck->count == 0 for list %d", list
));
442 * Just increment the in-memory struct, posix locks
445 tdb
->lockrecs
[i
].count
++;
450 new_lck
= (struct tdb_lock_type
*)realloc(
452 sizeof(*tdb
->lockrecs
) * (tdb
->num_lockrecs
+1));
453 if (new_lck
== NULL
) {
457 tdb
->lockrecs
= new_lck
;
459 /* Since fcntl locks don't nest, we do a lock for the first one,
460 and simply bump the count for future ones */
462 tdb
->methods
->tdb_brlock(tdb
,FREELIST_TOP
+4*list
, ltype
, op
,
469 tdb
->lockrecs
[tdb
->num_lockrecs
].list
= list
;
470 tdb
->lockrecs
[tdb
->num_lockrecs
].count
= 1;
471 tdb
->lockrecs
[tdb
->num_lockrecs
].ltype
= ltype
;
472 tdb
->num_lockrecs
+= 1;
477 /* lock a list in the database. list -1 is the alloc list */
478 int tdb_lock(struct tdb_context
*tdb
, int list
, int ltype
)
481 ret
= _tdb_lock(tdb
, list
, ltype
, F_SETLKW
);
483 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_lock failed on list %d "
484 "ltype=%d (%s)\n", list
, ltype
, strerror(errno
)));
489 /* lock a list in the database. list -1 is the alloc list. non-blocking lock */
490 int tdb_lock_nonblock(struct tdb_context
*tdb
, int list
, int ltype
)
492 return _tdb_lock(tdb
, list
, ltype
, F_SETLK
);
496 /* unlock the database: returns void because it's too late for errors. */
497 /* changed to return int it may be interesting to know there
498 has been an error --simo */
499 int tdb_unlock(struct tdb_context
*tdb
, int list
, int ltype
)
503 struct tdb_lock_type
*lck
= NULL
;
504 bool mark_lock
= ((ltype
& TDB_MARK_LOCK
) == TDB_MARK_LOCK
);
506 ltype
&= ~TDB_MARK_LOCK
;
508 /* a global lock allows us to avoid per chain locks */
509 if (tdb
->global_lock
.count
&&
510 (ltype
== tdb
->global_lock
.ltype
|| ltype
== F_RDLCK
)) {
514 if (tdb
->global_lock
.count
) {
515 return TDB_ERRCODE(TDB_ERR_LOCK
, -1);
518 if (tdb
->flags
& TDB_NOLOCK
)
522 if (list
< -1 || list
>= (int)tdb
->header
.hash_size
) {
523 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlock: list %d invalid (%d)\n", list
, tdb
->header
.hash_size
));
527 for (i
=0; i
<tdb
->num_lockrecs
; i
++) {
528 if (tdb
->lockrecs
[i
].list
== list
) {
529 lck
= &tdb
->lockrecs
[i
];
534 if ((lck
== NULL
) || (lck
->count
== 0)) {
535 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlock: count is 0\n"));
539 if (lck
->count
> 1) {
545 * This lock has count==1 left, so we need to unlock it in the
546 * kernel. We don't bother with decrementing the in-memory array
547 * element, we're about to overwrite it with the last array element
554 ret
= tdb
->methods
->tdb_brlock(tdb
, FREELIST_TOP
+4*list
, F_UNLCK
,
560 * Shrink the array by overwriting the element just unlocked with the
561 * last array element.
564 if (tdb
->num_lockrecs
> 1) {
565 *lck
= tdb
->lockrecs
[tdb
->num_lockrecs
-1];
567 tdb
->num_lockrecs
-= 1;
570 * We don't bother with realloc when the array shrinks, but if we have
571 * a completely idle tdb we should get rid of the locked array.
574 if (tdb
->num_lockrecs
== 0) {
575 SAFE_FREE(tdb
->lockrecs
);
579 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlock: An error occurred unlocking!\n"));
584 get the transaction lock
586 int tdb_transaction_lock(struct tdb_context
*tdb
, int ltype
)
588 if (tdb
->have_transaction_lock
|| tdb
->global_lock
.count
) {
591 if (tdb
->methods
->tdb_brlock(tdb
, TRANSACTION_LOCK
, ltype
,
592 F_SETLKW
, 0, 1) == -1) {
593 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_transaction_lock: failed to get transaction lock\n"));
594 tdb
->ecode
= TDB_ERR_LOCK
;
597 tdb
->have_transaction_lock
= 1;
602 release the transaction lock
604 int tdb_transaction_unlock(struct tdb_context
*tdb
)
607 if (!tdb
->have_transaction_lock
) {
610 ret
= tdb
->methods
->tdb_brlock(tdb
, TRANSACTION_LOCK
, F_UNLCK
, F_SETLKW
, 0, 1);
612 tdb
->have_transaction_lock
= 0;
620 /* lock/unlock entire database */
621 static int _tdb_lockall(struct tdb_context
*tdb
, int ltype
, int op
)
623 bool mark_lock
= ((ltype
& TDB_MARK_LOCK
) == TDB_MARK_LOCK
);
625 ltype
&= ~TDB_MARK_LOCK
;
627 /* There are no locks on read-only dbs */
628 if (tdb
->read_only
|| tdb
->traverse_read
)
629 return TDB_ERRCODE(TDB_ERR_LOCK
, -1);
631 if (tdb
->global_lock
.count
&& tdb
->global_lock
.ltype
== ltype
) {
632 tdb
->global_lock
.count
++;
636 if (tdb
->global_lock
.count
) {
637 /* a global lock of a different type exists */
638 return TDB_ERRCODE(TDB_ERR_LOCK
, -1);
641 if (tdb
->num_locks
!= 0) {
642 /* can't combine global and chain locks */
643 return TDB_ERRCODE(TDB_ERR_LOCK
, -1);
647 tdb
->methods
->tdb_brlock(tdb
, FREELIST_TOP
, ltype
, op
,
648 0, 4*tdb
->header
.hash_size
)) {
649 if (op
== F_SETLKW
) {
650 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_lockall failed (%s)\n", strerror(errno
)));
655 tdb
->global_lock
.count
= 1;
656 tdb
->global_lock
.ltype
= ltype
;
663 /* unlock entire db */
664 static int _tdb_unlockall(struct tdb_context
*tdb
, int ltype
)
666 bool mark_lock
= ((ltype
& TDB_MARK_LOCK
) == TDB_MARK_LOCK
);
668 ltype
&= ~TDB_MARK_LOCK
;
670 /* There are no locks on read-only dbs */
671 if (tdb
->read_only
|| tdb
->traverse_read
) {
672 return TDB_ERRCODE(TDB_ERR_LOCK
, -1);
675 if (tdb
->global_lock
.ltype
!= ltype
|| tdb
->global_lock
.count
== 0) {
676 return TDB_ERRCODE(TDB_ERR_LOCK
, -1);
679 if (tdb
->global_lock
.count
> 1) {
680 tdb
->global_lock
.count
--;
685 tdb
->methods
->tdb_brlock(tdb
, FREELIST_TOP
, F_UNLCK
, F_SETLKW
,
686 0, 4*tdb
->header
.hash_size
)) {
687 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_unlockall failed (%s)\n", strerror(errno
)));
691 tdb
->global_lock
.count
= 0;
692 tdb
->global_lock
.ltype
= 0;
697 /* lock entire database with write lock */
698 int tdb_lockall(struct tdb_context
*tdb
)
700 return _tdb_lockall(tdb
, F_WRLCK
, F_SETLKW
);
703 /* lock entire database with write lock - mark only */
704 int tdb_lockall_mark(struct tdb_context
*tdb
)
706 return _tdb_lockall(tdb
, F_WRLCK
| TDB_MARK_LOCK
, F_SETLKW
);
709 /* unlock entire database with write lock - unmark only */
710 int tdb_lockall_unmark(struct tdb_context
*tdb
)
712 return _tdb_unlockall(tdb
, F_WRLCK
| TDB_MARK_LOCK
);
715 /* lock entire database with write lock - nonblocking variant */
716 int tdb_lockall_nonblock(struct tdb_context
*tdb
)
718 return _tdb_lockall(tdb
, F_WRLCK
, F_SETLK
);
721 /* unlock entire database with write lock */
722 int tdb_unlockall(struct tdb_context
*tdb
)
724 return _tdb_unlockall(tdb
, F_WRLCK
);
727 /* lock entire database with read lock */
728 int tdb_lockall_read(struct tdb_context
*tdb
)
730 return _tdb_lockall(tdb
, F_RDLCK
, F_SETLKW
);
733 /* lock entire database with read lock - nonblock variant */
734 int tdb_lockall_read_nonblock(struct tdb_context
*tdb
)
736 return _tdb_lockall(tdb
, F_RDLCK
, F_SETLK
);
739 /* unlock entire database with read lock */
740 int tdb_unlockall_read(struct tdb_context
*tdb
)
742 return _tdb_unlockall(tdb
, F_RDLCK
);
745 /* lock/unlock one hash chain. This is meant to be used to reduce
746 contention - it cannot guarantee how many records will be locked */
747 int tdb_chainlock(struct tdb_context
*tdb
, TDB_DATA key
)
749 return tdb_lock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
);
752 /* lock/unlock one hash chain, non-blocking. This is meant to be used
753 to reduce contention - it cannot guarantee how many records will be
755 int tdb_chainlock_nonblock(struct tdb_context
*tdb
, TDB_DATA key
)
757 return tdb_lock_nonblock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
);
760 /* mark a chain as locked without actually locking it. Warning! use with great caution! */
761 int tdb_chainlock_mark(struct tdb_context
*tdb
, TDB_DATA key
)
763 return tdb_lock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
| TDB_MARK_LOCK
);
766 /* unmark a chain as locked without actually locking it. Warning! use with great caution! */
767 int tdb_chainlock_unmark(struct tdb_context
*tdb
, TDB_DATA key
)
769 return tdb_unlock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
| TDB_MARK_LOCK
);
772 int tdb_chainunlock(struct tdb_context
*tdb
, TDB_DATA key
)
774 return tdb_unlock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_WRLCK
);
777 int tdb_chainlock_read(struct tdb_context
*tdb
, TDB_DATA key
)
779 return tdb_lock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_RDLCK
);
782 int tdb_chainunlock_read(struct tdb_context
*tdb
, TDB_DATA key
)
784 return tdb_unlock(tdb
, BUCKET(tdb
->hash_fn(&key
)), F_RDLCK
);
789 /* record lock stops delete underneath */
790 int tdb_lock_record(struct tdb_context
*tdb
, tdb_off_t off
)
792 return off
? tdb
->methods
->tdb_brlock(tdb
, off
, F_RDLCK
, F_SETLKW
, 0, 1) : 0;
796 Write locks override our own fcntl readlocks, so check it here.
797 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
798 an error to fail to get the lock here.
800 int tdb_write_lock_record(struct tdb_context
*tdb
, tdb_off_t off
)
802 struct tdb_traverse_lock
*i
;
803 for (i
= &tdb
->travlocks
; i
; i
= i
->next
)
806 return tdb
->methods
->tdb_brlock(tdb
, off
, F_WRLCK
, F_SETLK
, 1, 1);
810 Note this is meant to be F_SETLK, *not* F_SETLKW, as it's not
811 an error to fail to get the lock here.
813 int tdb_write_unlock_record(struct tdb_context
*tdb
, tdb_off_t off
)
815 return tdb
->methods
->tdb_brlock(tdb
, off
, F_UNLCK
, F_SETLK
, 0, 1);
818 /* fcntl locks don't stack: avoid unlocking someone else's */
819 int tdb_unlock_record(struct tdb_context
*tdb
, tdb_off_t off
)
821 struct tdb_traverse_lock
*i
;
826 for (i
= &tdb
->travlocks
; i
; i
= i
->next
)
829 return (count
== 1 ? tdb
->methods
->tdb_brlock(tdb
, off
, F_UNLCK
, F_SETLKW
, 0, 1) : 0);
834 /* check for an out of bounds access - if it is out of bounds then
835 see if the database has been expanded by someone else and expand
837 note that "len" is the minimum length needed for the db
839 static int tdb_oob(struct tdb_context
*tdb
, tdb_off_t len
, int probe
)
842 if (len
<= tdb
->map_size
)
844 if (tdb
->flags
& TDB_INTERNAL
) {
846 /* Ensure ecode is set for log fn. */
847 tdb
->ecode
= TDB_ERR_IO
;
848 TDB_LOG((tdb
, TDB_DEBUG_FATAL
,"tdb_oob len %d beyond internal malloc size %d\n",
849 (int)len
, (int)tdb
->map_size
));
851 return TDB_ERRCODE(TDB_ERR_IO
, -1);
854 if (fstat(tdb
->fd
, &st
) == -1) {
855 return TDB_ERRCODE(TDB_ERR_IO
, -1);
858 if (st
.st_size
< (size_t)len
) {
860 /* Ensure ecode is set for log fn. */
861 tdb
->ecode
= TDB_ERR_IO
;
862 TDB_LOG((tdb
, TDB_DEBUG_FATAL
,"tdb_oob len %d beyond eof at %d\n",
863 (int)len
, (int)st
.st_size
));
865 return TDB_ERRCODE(TDB_ERR_IO
, -1);
868 /* Unmap, update size, remap */
869 if (tdb_munmap(tdb
) == -1)
870 return TDB_ERRCODE(TDB_ERR_IO
, -1);
871 tdb
->map_size
= st
.st_size
;
876 /* write a lump of data at a specified offset */
877 static int tdb_write(struct tdb_context
*tdb
, tdb_off_t off
,
878 const void *buf
, tdb_len_t len
)
884 if (tdb
->read_only
|| tdb
->traverse_read
) {
885 tdb
->ecode
= TDB_ERR_RDONLY
;
889 if (tdb
->methods
->tdb_oob(tdb
, off
+ len
, 0) != 0)
893 memcpy(off
+ (char *)tdb
->map_ptr
, buf
, len
);
894 } else if (pwrite(tdb
->fd
, buf
, len
, off
) != (ssize_t
)len
) {
895 /* Ensure ecode is set for log fn. */
896 tdb
->ecode
= TDB_ERR_IO
;
897 TDB_LOG((tdb
, TDB_DEBUG_FATAL
,"tdb_write failed at %d len=%d (%s)\n",
898 off
, len
, strerror(errno
)));
899 return TDB_ERRCODE(TDB_ERR_IO
, -1);
904 /* Endian conversion: we only ever deal with 4 byte quantities */
905 void *tdb_convert(void *buf
, u32 size
)
907 u32 i
, *p
= (u32
*)buf
;
908 for (i
= 0; i
< size
/ 4; i
++)
909 p
[i
] = TDB_BYTEREV(p
[i
]);
914 /* read a lump of data at a specified offset, maybe convert */
915 static int tdb_read(struct tdb_context
*tdb
, tdb_off_t off
, void *buf
,
916 tdb_len_t len
, int cv
)
918 if (tdb
->methods
->tdb_oob(tdb
, off
+ len
, 0) != 0) {
923 memcpy(buf
, off
+ (char *)tdb
->map_ptr
, len
);
925 ssize_t ret
= pread(tdb
->fd
, buf
, len
, off
);
926 if (ret
!= (ssize_t
)len
) {
927 /* Ensure ecode is set for log fn. */
928 tdb
->ecode
= TDB_ERR_IO
;
929 TDB_LOG((tdb
, TDB_DEBUG_FATAL
,"tdb_read failed at %d "
930 "len=%d ret=%d (%s) map_size=%d\n",
931 (int)off
, (int)len
, (int)ret
, strerror(errno
),
932 (int)tdb
->map_size
));
933 return TDB_ERRCODE(TDB_ERR_IO
, -1);
937 tdb_convert(buf
, len
);
945 do an unlocked scan of the hash table heads to find the next non-zero head. The value
946 will then be confirmed with the lock held
948 static void tdb_next_hash_chain(struct tdb_context
*tdb
, u32
*chain
)
952 for (;h
< tdb
->header
.hash_size
;h
++) {
953 if (0 != *(u32
*)(TDB_HASH_TOP(h
) + (unsigned char *)tdb
->map_ptr
)) {
959 for (;h
< tdb
->header
.hash_size
;h
++) {
960 if (tdb_ofs_read(tdb
, TDB_HASH_TOP(h
), &off
) != 0 || off
!= 0) {
969 int tdb_munmap(struct tdb_context
*tdb
)
971 if (tdb
->flags
& TDB_INTERNAL
)
976 int ret
= munmap(tdb
->map_ptr
, tdb
->real_map_size
);
979 tdb
->real_map_size
= 0;
986 void tdb_mmap(struct tdb_context
*tdb
)
988 if (tdb
->flags
& TDB_INTERNAL
)
992 if (!(tdb
->flags
& TDB_NOMMAP
)) {
993 tdb
->map_ptr
= mmap(NULL
, tdb
->map_size
,
994 PROT_READ
|(tdb
->read_only
? 0:PROT_WRITE
),
995 MAP_SHARED
|MAP_FILE
, tdb
->fd
, 0);
998 * NB. When mmap fails it returns MAP_FAILED *NOT* NULL !!!!
1001 if (tdb
->map_ptr
== MAP_FAILED
) {
1002 tdb
->real_map_size
= 0;
1003 tdb
->map_ptr
= NULL
;
1004 TDB_LOG((tdb
, TDB_DEBUG_WARNING
, "tdb_mmap failed for size %d (%s)\n",
1005 tdb
->map_size
, strerror(errno
)));
1007 tdb
->real_map_size
= tdb
->map_size
;
1009 tdb
->map_ptr
= NULL
;
1012 tdb
->map_ptr
= NULL
;
1016 /* expand a file. we prefer to use ftruncate, as that is what posix
1017 says to use for mmap expansion */
1018 static int tdb_expand_file(struct tdb_context
*tdb
, tdb_off_t size
, tdb_off_t addition
)
1022 if (tdb
->read_only
|| tdb
->traverse_read
) {
1023 tdb
->ecode
= TDB_ERR_RDONLY
;
1027 if (ftruncate(tdb
->fd
, size
+addition
) == -1) {
1029 if (pwrite(tdb
->fd
, &b
, 1, (size
+addition
) - 1) != 1) {
1030 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "expand_file to %d failed (%s)\n",
1031 size
+addition
, strerror(errno
)));
1036 /* now fill the file with something. This ensures that the
1037 file isn't sparse, which would be very bad if we ran out of
1038 disk. This must be done with write, not via mmap */
1039 memset(buf
, TDB_PAD_BYTE
, sizeof(buf
));
1041 int n
= addition
>sizeof(buf
)?sizeof(buf
):addition
;
1042 int ret
= pwrite(tdb
->fd
, buf
, n
, size
);
1044 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "expand_file write of %d failed (%s)\n",
1045 n
, strerror(errno
)));
1055 /* expand the database at least size bytes by expanding the underlying
1056 file and doing the mmap again if necessary */
1057 int tdb_expand(struct tdb_context
*tdb
, tdb_off_t size
)
1059 struct list_struct rec
;
1062 if (tdb_lock(tdb
, -1, F_WRLCK
) == -1) {
1063 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "lock failed in tdb_expand\n"));
1067 /* must know about any previous expansions by another process */
1068 tdb
->methods
->tdb_oob(tdb
, tdb
->map_size
+ 1, 1);
1070 /* always make room for at least 10 more records, and round
1071 the database up to a multiple of the page size */
1072 size
= TDB_ALIGN(tdb
->map_size
+ size
*10, tdb
->page_size
) - tdb
->map_size
;
1074 if (!(tdb
->flags
& TDB_INTERNAL
))
1078 * We must ensure the file is unmapped before doing this
1079 * to ensure consistency with systems like OpenBSD where
1080 * writes and mmaps are not consistent.
1083 /* expand the file itself */
1084 if (!(tdb
->flags
& TDB_INTERNAL
)) {
1085 if (tdb
->methods
->tdb_expand_file(tdb
, tdb
->map_size
, size
) != 0)
1089 tdb
->map_size
+= size
;
1091 if (tdb
->flags
& TDB_INTERNAL
) {
1092 char *new_map_ptr
= (char *)realloc(tdb
->map_ptr
,
1095 tdb
->map_size
-= size
;
1098 tdb
->map_ptr
= new_map_ptr
;
1101 * We must ensure the file is remapped before adding the space
1102 * to ensure consistency with systems like OpenBSD where
1103 * writes and mmaps are not consistent.
1106 /* We're ok if the mmap fails as we'll fallback to read/write */
1110 /* form a new freelist record */
1111 memset(&rec
,'\0',sizeof(rec
));
1112 rec
.rec_len
= size
- sizeof(rec
);
1114 /* link it into the free list */
1115 offset
= tdb
->map_size
- size
;
1116 if (tdb_free(tdb
, offset
, &rec
) == -1)
1119 tdb_unlock(tdb
, -1, F_WRLCK
);
1122 tdb_unlock(tdb
, -1, F_WRLCK
);
1126 /* read/write a tdb_off_t */
1127 int tdb_ofs_read(struct tdb_context
*tdb
, tdb_off_t offset
, tdb_off_t
*d
)
1129 return tdb
->methods
->tdb_read(tdb
, offset
, (char*)d
, sizeof(*d
), DOCONV());
1132 int tdb_ofs_write(struct tdb_context
*tdb
, tdb_off_t offset
, tdb_off_t
*d
)
1135 return tdb
->methods
->tdb_write(tdb
, offset
, CONVERT(off
), sizeof(*d
));
1139 /* read a lump of data, allocating the space for it */
1140 unsigned char *tdb_alloc_read(struct tdb_context
*tdb
, tdb_off_t offset
, tdb_len_t len
)
1144 /* some systems don't like zero length malloc */
1149 if (!(buf
= (unsigned char *)malloc(len
))) {
1150 /* Ensure ecode is set for log fn. */
1151 tdb
->ecode
= TDB_ERR_OOM
;
1152 TDB_LOG((tdb
, TDB_DEBUG_ERROR
,"tdb_alloc_read malloc failed len=%d (%s)\n",
1153 len
, strerror(errno
)));
1154 return TDB_ERRCODE(TDB_ERR_OOM
, buf
);
1156 if (tdb
->methods
->tdb_read(tdb
, offset
, buf
, len
, 0) == -1) {
1163 /* Give a piece of tdb data to a parser */
1165 int tdb_parse_data(struct tdb_context
*tdb
, TDB_DATA key
,
1166 tdb_off_t offset
, tdb_len_t len
,
1167 int (*parser
)(TDB_DATA key
, TDB_DATA data
,
1168 void *private_data
),
1176 if ((tdb
->transaction
== NULL
) && (tdb
->map_ptr
!= NULL
)) {
1178 * Optimize by avoiding the malloc/memcpy/free, point the
1179 * parser directly at the mmap area.
1181 if (tdb
->methods
->tdb_oob(tdb
, offset
+len
, 0) != 0) {
1184 data
.dptr
= offset
+ (unsigned char *)tdb
->map_ptr
;
1185 return parser(key
, data
, private_data
);
1188 if (!(data
.dptr
= tdb_alloc_read(tdb
, offset
, len
))) {
1192 result
= parser(key
, data
, private_data
);
1197 /* read/write a record */
1198 int tdb_rec_read(struct tdb_context
*tdb
, tdb_off_t offset
, struct list_struct
*rec
)
1200 if (tdb
->methods
->tdb_read(tdb
, offset
, rec
, sizeof(*rec
),DOCONV()) == -1)
1202 if (TDB_BAD_MAGIC(rec
)) {
1203 /* Ensure ecode is set for log fn. */
1204 tdb
->ecode
= TDB_ERR_CORRUPT
;
1205 TDB_LOG((tdb
, TDB_DEBUG_FATAL
,"tdb_rec_read bad magic 0x%x at offset=%d\n", rec
->magic
, offset
));
1206 return TDB_ERRCODE(TDB_ERR_CORRUPT
, -1);
1208 return tdb
->methods
->tdb_oob(tdb
, rec
->next
+sizeof(*rec
), 0);
1211 int tdb_rec_write(struct tdb_context
*tdb
, tdb_off_t offset
, struct list_struct
*rec
)
1213 struct list_struct r
= *rec
;
1214 return tdb
->methods
->tdb_write(tdb
, offset
, CONVERT(r
), sizeof(r
));
1217 static const struct tdb_methods io_methods
= {
1220 tdb_next_hash_chain
,
1227 initialise the default methods table
1229 void tdb_io_init(struct tdb_context
*tdb
)
1231 tdb
->methods
= &io_methods
;
1234 /* file: transaction.c */
1239 - only allow a single transaction at a time per database. This makes
1240 using the transaction API simpler, as otherwise the caller would
1241 have to cope with temporary failures in transactions that conflict
1242 with other current transactions
1244 - keep the transaction recovery information in the same file as the
1245 database, using a special 'transaction recovery' record pointed at
1246 by the header. This removes the need for extra journal files as
1247 used by some other databases
1249 - dynamically allocated the transaction recover record, re-using it
1250 for subsequent transactions. If a larger record is needed then
1251 tdb_free() the old record to place it on the normal tdb freelist
1252 before allocating the new record
1254 - during transactions, keep a linked list of writes all that have
1255 been performed by intercepting all tdb_write() calls. The hooked
1256 transaction versions of tdb_read() and tdb_write() check this
1257 linked list and try to use the elements of the list in preference
1258 to the real database.
1260 - don't allow any locks to be held when a transaction starts,
1261 otherwise we can end up with deadlock (plus lack of lock nesting
1262 in posix locks would mean the lock is lost)
1264 - if the caller gains a lock during the transaction but doesn't
1265 release it then fail the commit
1267 - allow for nested calls to tdb_transaction_start(), re-using the
1268 existing transaction record. If the inner transaction is cancelled
1269 then a subsequent commit will fail
1271 - keep a mirrored copy of the tdb hash chain heads to allow for the
1272 fast hash heads scan on traverse, updating the mirrored copy in
1273 the transaction version of tdb_write
1275 - allow callers to mix transaction and non-transaction use of tdb,
1276 although once a transaction is started then an exclusive lock is
1277 gained until the transaction is committed or cancelled
1279 - the commit strategy involves first saving away all modified data
1280 into a linearised buffer in the transaction recovery area, then
1281 marking the transaction recovery area with a magic value to
1282 indicate a valid recovery record. In total 4 fsync/msync calls are
1283 needed per commit to prevent race conditions. It might be possible
1284 to reduce this to 3 or even 2 with some more work.
1286 - check for a valid recovery record on open of the tdb, while the
1287 global lock is held. Automatically recover from the transaction
1288 recovery area if needed, then continue with the open as
1289 usual. This allows for smooth crash recovery with no administrator
1292 - if TDB_NOSYNC is passed to flags in tdb_open then transactions are
1293 still available, but no transaction recovery area is used and no
1294 fsync/msync calls are made.
1298 struct tdb_transaction_el
{
1299 struct tdb_transaction_el
*next
, *prev
;
1302 unsigned char *data
;
1306 hold the context of any current transaction
1308 struct tdb_transaction
{
1309 /* we keep a mirrored copy of the tdb hash heads here so
1310 tdb_next_hash_chain() can operate efficiently */
1313 /* the original io methods - used to do IOs to the real db */
1314 const struct tdb_methods
*io_methods
;
1316 /* the list of transaction elements. We use a doubly linked
1317 list with a last pointer to allow us to keep the list
1318 ordered, with first element at the front of the list. It
1319 needs to be doubly linked as the read/write traversals need
1320 to be backwards, while the commit needs to be forwards */
1321 struct tdb_transaction_el
*elements
, *elements_last
;
1323 /* non-zero when an internal transaction error has
1324 occurred. All write operations will then fail until the
1325 transaction is ended */
1326 int transaction_error
;
1328 /* when inside a transaction we need to keep track of any
1329 nested tdb_transaction_start() calls, as these are allowed,
1330 but don't create a new transaction */
1333 /* old file size before transaction */
1334 tdb_len_t old_map_size
;
1339 read while in a transaction. We need to check first if the data is in our list
1340 of transaction elements, then if not do a real read
1342 static int transaction_read(struct tdb_context
*tdb
, tdb_off_t off
, void *buf
,
1343 tdb_len_t len
, int cv
)
1345 struct tdb_transaction_el
*el
;
1347 /* we need to walk the list backwards to get the most recent data */
1348 for (el
=tdb
->transaction
->elements_last
;el
;el
=el
->prev
) {
1351 if (off
+len
<= el
->offset
) {
1354 if (off
>= el
->offset
+ el
->length
) {
1358 /* an overlapping read - needs to be split into up to
1359 2 reads and a memcpy */
1360 if (off
< el
->offset
) {
1361 partial
= el
->offset
- off
;
1362 if (transaction_read(tdb
, off
, buf
, partial
, cv
) != 0) {
1367 buf
= (void *)(partial
+ (char *)buf
);
1369 if (off
+ len
<= el
->offset
+ el
->length
) {
1372 partial
= el
->offset
+ el
->length
- off
;
1374 memcpy(buf
, el
->data
+ (off
- el
->offset
), partial
);
1376 tdb_convert(buf
, len
);
1380 buf
= (void *)(partial
+ (char *)buf
);
1382 if (len
!= 0 && transaction_read(tdb
, off
, buf
, len
, cv
) != 0) {
1389 /* its not in the transaction elements - do a real read */
1390 return tdb
->transaction
->io_methods
->tdb_read(tdb
, off
, buf
, len
, cv
);
1393 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "transaction_read: failed at off=%d len=%d\n", off
, len
));
1394 tdb
->ecode
= TDB_ERR_IO
;
1395 tdb
->transaction
->transaction_error
= 1;
1401 write while in a transaction
1403 static int transaction_write(struct tdb_context
*tdb
, tdb_off_t off
,
1404 const void *buf
, tdb_len_t len
)
1406 struct tdb_transaction_el
*el
, *best_el
=NULL
;
1412 /* if the write is to a hash head, then update the transaction
1414 if (len
== sizeof(tdb_off_t
) && off
>= FREELIST_TOP
&&
1415 off
< FREELIST_TOP
+TDB_HASHTABLE_SIZE(tdb
)) {
1416 u32 chain
= (off
-FREELIST_TOP
) / sizeof(tdb_off_t
);
1417 memcpy(&tdb
->transaction
->hash_heads
[chain
], buf
, len
);
1420 /* first see if we can replace an existing entry */
1421 for (el
=tdb
->transaction
->elements_last
;el
;el
=el
->prev
) {
1424 if (best_el
== NULL
&& off
== el
->offset
+el
->length
) {
1428 if (off
+len
<= el
->offset
) {
1431 if (off
>= el
->offset
+ el
->length
) {
1435 /* an overlapping write - needs to be split into up to
1436 2 writes and a memcpy */
1437 if (off
< el
->offset
) {
1438 partial
= el
->offset
- off
;
1439 if (transaction_write(tdb
, off
, buf
, partial
) != 0) {
1444 buf
= (const void *)(partial
+ (const char *)buf
);
1446 if (off
+ len
<= el
->offset
+ el
->length
) {
1449 partial
= el
->offset
+ el
->length
- off
;
1451 memcpy(el
->data
+ (off
- el
->offset
), buf
, partial
);
1454 buf
= (const void *)(partial
+ (const char *)buf
);
1456 if (len
!= 0 && transaction_write(tdb
, off
, buf
, len
) != 0) {
1463 /* see if we can append the new entry to an existing entry */
1464 if (best_el
&& best_el
->offset
+ best_el
->length
== off
&&
1465 (off
+len
< tdb
->transaction
->old_map_size
||
1466 off
> tdb
->transaction
->old_map_size
)) {
1467 unsigned char *data
= best_el
->data
;
1469 el
->data
= (unsigned char *)realloc(el
->data
,
1471 if (el
->data
== NULL
) {
1472 tdb
->ecode
= TDB_ERR_OOM
;
1473 tdb
->transaction
->transaction_error
= 1;
1478 memcpy(el
->data
+ el
->length
, buf
, len
);
1480 memset(el
->data
+ el
->length
, TDB_PAD_BYTE
, len
);
1486 /* add a new entry at the end of the list */
1487 el
= (struct tdb_transaction_el
*)malloc(sizeof(*el
));
1489 tdb
->ecode
= TDB_ERR_OOM
;
1490 tdb
->transaction
->transaction_error
= 1;
1494 el
->prev
= tdb
->transaction
->elements_last
;
1497 el
->data
= (unsigned char *)malloc(len
);
1498 if (el
->data
== NULL
) {
1500 tdb
->ecode
= TDB_ERR_OOM
;
1501 tdb
->transaction
->transaction_error
= 1;
1505 memcpy(el
->data
, buf
, len
);
1507 memset(el
->data
, TDB_PAD_BYTE
, len
);
1510 el
->prev
->next
= el
;
1512 tdb
->transaction
->elements
= el
;
1514 tdb
->transaction
->elements_last
= el
;
1518 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "transaction_write: failed at off=%d len=%d\n", off
, len
));
1519 tdb
->ecode
= TDB_ERR_IO
;
1520 tdb
->transaction
->transaction_error
= 1;
1525 accelerated hash chain head search, using the cached hash heads
1527 static void transaction_next_hash_chain(struct tdb_context
*tdb
, u32
*chain
)
1530 for (;h
< tdb
->header
.hash_size
;h
++) {
1531 /* the +1 takes account of the freelist */
1532 if (0 != tdb
->transaction
->hash_heads
[h
+1]) {
1540 out of bounds check during a transaction
1542 static int transaction_oob(struct tdb_context
*tdb
, tdb_off_t len
, int probe
)
1544 if (len
<= tdb
->map_size
) {
1547 return TDB_ERRCODE(TDB_ERR_IO
, -1);
1551 transaction version of tdb_expand().
1553 static int transaction_expand_file(struct tdb_context
*tdb
, tdb_off_t size
,
1556 /* add a write to the transaction elements, so subsequent
1557 reads see the zero data */
1558 if (transaction_write(tdb
, size
, NULL
, addition
) != 0) {
1566 brlock during a transaction - ignore them
1568 static int transaction_brlock(struct tdb_context
*tdb
, tdb_off_t offset
,
1569 int rw_type
, int lck_type
, int probe
, size_t len
)
1574 static const struct tdb_methods transaction_methods
= {
1577 transaction_next_hash_chain
,
1579 transaction_expand_file
,
1585 start a tdb transaction. No token is returned, as only a single
1586 transaction is allowed to be pending per tdb_context
1588 int tdb_transaction_start(struct tdb_context
*tdb
)
1590 /* some sanity checks */
1591 if (tdb
->read_only
|| (tdb
->flags
& TDB_INTERNAL
) || tdb
->traverse_read
) {
1592 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_transaction_start: cannot start a transaction on a read-only or internal db\n"));
1593 tdb
->ecode
= TDB_ERR_EINVAL
;
1597 /* cope with nested tdb_transaction_start() calls */
1598 if (tdb
->transaction
!= NULL
) {
1599 tdb
->transaction
->nesting
++;
1600 TDB_LOG((tdb
, TDB_DEBUG_TRACE
, "tdb_transaction_start: nesting %d\n",
1601 tdb
->transaction
->nesting
));
1605 if (tdb
->num_locks
!= 0 || tdb
->global_lock
.count
) {
1606 /* the caller must not have any locks when starting a
1607 transaction as otherwise we'll be screwed by lack
1608 of nested locks in posix */
1609 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_transaction_start: cannot start a transaction with locks held\n"));
1610 tdb
->ecode
= TDB_ERR_LOCK
;
1614 if (tdb
->travlocks
.next
!= NULL
) {
1615 /* you cannot use transactions inside a traverse (although you can use
1616 traverse inside a transaction) as otherwise you can end up with
1618 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_transaction_start: cannot start a transaction within a traverse\n"));
1619 tdb
->ecode
= TDB_ERR_LOCK
;
1623 tdb
->transaction
= (struct tdb_transaction
*)
1624 calloc(sizeof(struct tdb_transaction
), 1);
1625 if (tdb
->transaction
== NULL
) {
1626 tdb
->ecode
= TDB_ERR_OOM
;
1630 /* get the transaction write lock. This is a blocking lock. As
1631 discussed with Volker, there are a number of ways we could
1632 make this async, which we will probably do in the future */
1633 if (tdb_transaction_lock(tdb
, F_WRLCK
) == -1) {
1634 SAFE_FREE(tdb
->transaction
);
1638 /* get a read lock from the freelist to the end of file. This
1639 is upgraded to a write lock during the commit */
1640 if (tdb_brlock(tdb
, FREELIST_TOP
, F_RDLCK
, F_SETLKW
, 0, 0) == -1) {
1641 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_transaction_start: failed to get hash locks\n"));
1642 tdb
->ecode
= TDB_ERR_LOCK
;
1646 /* setup a copy of the hash table heads so the hash scan in
1647 traverse can be fast */
1648 tdb
->transaction
->hash_heads
= (u32
*)
1649 calloc(tdb
->header
.hash_size
+1, sizeof(u32
));
1650 if (tdb
->transaction
->hash_heads
== NULL
) {
1651 tdb
->ecode
= TDB_ERR_OOM
;
1654 if (tdb
->methods
->tdb_read(tdb
, FREELIST_TOP
, tdb
->transaction
->hash_heads
,
1655 TDB_HASHTABLE_SIZE(tdb
), 0) != 0) {
1656 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_start: failed to read hash heads\n"));
1657 tdb
->ecode
= TDB_ERR_IO
;
1661 /* make sure we know about any file expansions already done by
1663 tdb
->methods
->tdb_oob(tdb
, tdb
->map_size
+ 1, 1);
1664 tdb
->transaction
->old_map_size
= tdb
->map_size
;
1666 /* finally hook the io methods, replacing them with
1667 transaction specific methods */
1668 tdb
->transaction
->io_methods
= tdb
->methods
;
1669 tdb
->methods
= &transaction_methods
;
1671 /* by calling this transaction write here, we ensure that we don't grow the
1672 transaction linked list due to hash table updates */
1673 if (transaction_write(tdb
, FREELIST_TOP
, tdb
->transaction
->hash_heads
,
1674 TDB_HASHTABLE_SIZE(tdb
)) != 0) {
1675 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_start: failed to prime hash table\n"));
1676 tdb
->ecode
= TDB_ERR_IO
;
1677 tdb
->methods
= tdb
->transaction
->io_methods
;
1684 tdb_brlock(tdb
, FREELIST_TOP
, F_UNLCK
, F_SETLKW
, 0, 0);
1685 tdb_transaction_unlock(tdb
);
1686 SAFE_FREE(tdb
->transaction
->hash_heads
);
1687 SAFE_FREE(tdb
->transaction
);
1693 cancel the current transaction
1695 int tdb_transaction_cancel(struct tdb_context
*tdb
)
1697 if (tdb
->transaction
== NULL
) {
1698 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_transaction_cancel: no transaction\n"));
1702 if (tdb
->transaction
->nesting
!= 0) {
1703 tdb
->transaction
->transaction_error
= 1;
1704 tdb
->transaction
->nesting
--;
1708 tdb
->map_size
= tdb
->transaction
->old_map_size
;
1710 /* free all the transaction elements */
1711 while (tdb
->transaction
->elements
) {
1712 struct tdb_transaction_el
*el
= tdb
->transaction
->elements
;
1713 tdb
->transaction
->elements
= el
->next
;
1718 /* remove any global lock created during the transaction */
1719 if (tdb
->global_lock
.count
!= 0) {
1720 tdb_brlock(tdb
, FREELIST_TOP
, F_UNLCK
, F_SETLKW
, 0, 4*tdb
->header
.hash_size
);
1721 tdb
->global_lock
.count
= 0;
1724 /* remove any locks created during the transaction */
1725 if (tdb
->num_locks
!= 0) {
1727 for (i
=0;i
<tdb
->num_lockrecs
;i
++) {
1728 tdb_brlock(tdb
,FREELIST_TOP
+4*tdb
->lockrecs
[i
].list
,
1729 F_UNLCK
,F_SETLKW
, 0, 1);
1732 tdb
->num_lockrecs
= 0;
1733 SAFE_FREE(tdb
->lockrecs
);
1736 /* restore the normal io methods */
1737 tdb
->methods
= tdb
->transaction
->io_methods
;
1739 tdb_brlock(tdb
, FREELIST_TOP
, F_UNLCK
, F_SETLKW
, 0, 0);
1740 tdb_transaction_unlock(tdb
);
1741 SAFE_FREE(tdb
->transaction
->hash_heads
);
1742 SAFE_FREE(tdb
->transaction
);
1750 static int transaction_sync(struct tdb_context
*tdb
, tdb_off_t offset
, tdb_len_t length
)
1752 if (fsync(tdb
->fd
) != 0) {
1753 tdb
->ecode
= TDB_ERR_IO
;
1754 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction: fsync failed\n"));
1757 #if defined(HAVE_MSYNC) && defined(MS_SYNC)
1759 tdb_off_t moffset
= offset
& ~(tdb
->page_size
-1);
1760 if (msync(moffset
+ (char *)tdb
->map_ptr
,
1761 length
+ (offset
- moffset
), MS_SYNC
) != 0) {
1762 tdb
->ecode
= TDB_ERR_IO
;
1763 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction: msync failed - %s\n",
1774 work out how much space the linearised recovery data will consume
1776 static tdb_len_t
tdb_recovery_size(struct tdb_context
*tdb
)
1778 struct tdb_transaction_el
*el
;
1779 tdb_len_t recovery_size
= 0;
1781 recovery_size
= sizeof(u32
);
1782 for (el
=tdb
->transaction
->elements
;el
;el
=el
->next
) {
1783 if (el
->offset
>= tdb
->transaction
->old_map_size
) {
1786 recovery_size
+= 2*sizeof(tdb_off_t
) + el
->length
;
1789 return recovery_size
;
1793 allocate the recovery area, or use an existing recovery area if it is
1796 static int tdb_recovery_allocate(struct tdb_context
*tdb
,
1797 tdb_len_t
*recovery_size
,
1798 tdb_off_t
*recovery_offset
,
1799 tdb_len_t
*recovery_max_size
)
1801 struct list_struct rec
;
1802 const struct tdb_methods
*methods
= tdb
->transaction
->io_methods
;
1803 tdb_off_t recovery_head
;
1805 if (tdb_ofs_read(tdb
, TDB_RECOVERY_HEAD
, &recovery_head
) == -1) {
1806 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_recovery_allocate: failed to read recovery head\n"));
1812 if (recovery_head
!= 0 &&
1813 methods
->tdb_read(tdb
, recovery_head
, &rec
, sizeof(rec
), DOCONV()) == -1) {
1814 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_recovery_allocate: failed to read recovery record\n"));
1818 *recovery_size
= tdb_recovery_size(tdb
);
1820 if (recovery_head
!= 0 && *recovery_size
<= rec
.rec_len
) {
1821 /* it fits in the existing area */
1822 *recovery_max_size
= rec
.rec_len
;
1823 *recovery_offset
= recovery_head
;
1827 /* we need to free up the old recovery area, then allocate a
1828 new one at the end of the file. Note that we cannot use
1829 tdb_allocate() to allocate the new one as that might return
1830 us an area that is being currently used (as of the start of
1832 if (recovery_head
!= 0) {
1833 if (tdb_free(tdb
, recovery_head
, &rec
) == -1) {
1834 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_recovery_allocate: failed to free previous recovery area\n"));
1839 /* the tdb_free() call might have increased the recovery size */
1840 *recovery_size
= tdb_recovery_size(tdb
);
1842 /* round up to a multiple of page size */
1843 *recovery_max_size
= TDB_ALIGN(sizeof(rec
) + *recovery_size
, tdb
->page_size
) - sizeof(rec
);
1844 *recovery_offset
= tdb
->map_size
;
1845 recovery_head
= *recovery_offset
;
1847 if (methods
->tdb_expand_file(tdb
, tdb
->transaction
->old_map_size
,
1848 (tdb
->map_size
- tdb
->transaction
->old_map_size
) +
1849 sizeof(rec
) + *recovery_max_size
) == -1) {
1850 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_recovery_allocate: failed to create recovery area\n"));
1854 /* remap the file (if using mmap) */
1855 methods
->tdb_oob(tdb
, tdb
->map_size
+ 1, 1);
1857 /* we have to reset the old map size so that we don't try to expand the file
1858 again in the transaction commit, which would destroy the recovery area */
1859 tdb
->transaction
->old_map_size
= tdb
->map_size
;
1861 /* write the recovery header offset and sync - we can sync without a race here
1862 as the magic ptr in the recovery record has not been set */
1863 CONVERT(recovery_head
);
1864 if (methods
->tdb_write(tdb
, TDB_RECOVERY_HEAD
,
1865 &recovery_head
, sizeof(tdb_off_t
)) == -1) {
1866 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_recovery_allocate: failed to write recovery head\n"));
1875 setup the recovery data that will be used on a crash during commit
1877 static int transaction_setup_recovery(struct tdb_context
*tdb
,
1878 tdb_off_t
*magic_offset
)
1880 struct tdb_transaction_el
*el
;
1881 tdb_len_t recovery_size
;
1882 unsigned char *data
, *p
;
1883 const struct tdb_methods
*methods
= tdb
->transaction
->io_methods
;
1884 struct list_struct
*rec
;
1885 tdb_off_t recovery_offset
, recovery_max_size
;
1886 tdb_off_t old_map_size
= tdb
->transaction
->old_map_size
;
1890 check that the recovery area has enough space
1892 if (tdb_recovery_allocate(tdb
, &recovery_size
,
1893 &recovery_offset
, &recovery_max_size
) == -1) {
1897 data
= (unsigned char *)malloc(recovery_size
+ sizeof(*rec
));
1899 tdb
->ecode
= TDB_ERR_OOM
;
1903 rec
= (struct list_struct
*)data
;
1904 memset(rec
, 0, sizeof(*rec
));
1907 rec
->data_len
= recovery_size
;
1908 rec
->rec_len
= recovery_max_size
;
1909 rec
->key_len
= old_map_size
;
1912 /* build the recovery data into a single blob to allow us to do a single
1913 large write, which should be more efficient */
1914 p
= data
+ sizeof(*rec
);
1915 for (el
=tdb
->transaction
->elements
;el
;el
=el
->next
) {
1916 if (el
->offset
>= old_map_size
) {
1919 if (el
->offset
+ el
->length
> tdb
->transaction
->old_map_size
) {
1920 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_setup_recovery: transaction data over new region boundary\n"));
1922 tdb
->ecode
= TDB_ERR_CORRUPT
;
1925 memcpy(p
, &el
->offset
, 4);
1926 memcpy(p
+4, &el
->length
, 4);
1930 /* the recovery area contains the old data, not the
1931 new data, so we have to call the original tdb_read
1933 if (methods
->tdb_read(tdb
, el
->offset
, p
+ 8, el
->length
, 0) != 0) {
1935 tdb
->ecode
= TDB_ERR_IO
;
1938 p
+= 8 + el
->length
;
1941 /* and the tailer */
1942 tailer
= sizeof(*rec
) + recovery_max_size
;
1943 memcpy(p
, &tailer
, 4);
1946 /* write the recovery data to the recovery area */
1947 if (methods
->tdb_write(tdb
, recovery_offset
, data
, sizeof(*rec
) + recovery_size
) == -1) {
1948 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_setup_recovery: failed to write recovery data\n"));
1950 tdb
->ecode
= TDB_ERR_IO
;
1954 /* as we don't have ordered writes, we have to sync the recovery
1955 data before we update the magic to indicate that the recovery
1957 if (transaction_sync(tdb
, recovery_offset
, sizeof(*rec
) + recovery_size
) == -1) {
1964 magic
= TDB_RECOVERY_MAGIC
;
1967 *magic_offset
= recovery_offset
+ offsetof(struct list_struct
, magic
);
1969 if (methods
->tdb_write(tdb
, *magic_offset
, &magic
, sizeof(magic
)) == -1) {
1970 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_setup_recovery: failed to write recovery magic\n"));
1971 tdb
->ecode
= TDB_ERR_IO
;
1975 /* ensure the recovery magic marker is on disk */
1976 if (transaction_sync(tdb
, *magic_offset
, sizeof(magic
)) == -1) {
1984 commit the current transaction
1986 int tdb_transaction_commit(struct tdb_context
*tdb
)
1988 const struct tdb_methods
*methods
;
1989 tdb_off_t magic_offset
= 0;
1992 if (tdb
->transaction
== NULL
) {
1993 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_transaction_commit: no transaction\n"));
1997 if (tdb
->transaction
->transaction_error
) {
1998 tdb
->ecode
= TDB_ERR_IO
;
1999 tdb_transaction_cancel(tdb
);
2000 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_transaction_commit: transaction error pending\n"));
2004 if (tdb
->transaction
->nesting
!= 0) {
2005 tdb
->transaction
->nesting
--;
2009 /* check for a null transaction */
2010 if (tdb
->transaction
->elements
== NULL
) {
2011 tdb_transaction_cancel(tdb
);
2015 methods
= tdb
->transaction
->io_methods
;
2017 /* if there are any locks pending then the caller has not
2018 nested their locks properly, so fail the transaction */
2019 if (tdb
->num_locks
|| tdb
->global_lock
.count
) {
2020 tdb
->ecode
= TDB_ERR_LOCK
;
2021 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_transaction_commit: locks pending on commit\n"));
2022 tdb_transaction_cancel(tdb
);
2026 /* upgrade the main transaction lock region to a write lock */
2027 if (tdb_brlock_upgrade(tdb
, FREELIST_TOP
, 0) == -1) {
2028 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_transaction_start: failed to upgrade hash locks\n"));
2029 tdb
->ecode
= TDB_ERR_LOCK
;
2030 tdb_transaction_cancel(tdb
);
2034 /* get the global lock - this prevents new users attaching to the database
2035 during the commit */
2036 if (tdb_brlock(tdb
, GLOBAL_LOCK
, F_WRLCK
, F_SETLKW
, 0, 1) == -1) {
2037 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_transaction_commit: failed to get global lock\n"));
2038 tdb
->ecode
= TDB_ERR_LOCK
;
2039 tdb_transaction_cancel(tdb
);
2043 if (!(tdb
->flags
& TDB_NOSYNC
)) {
2044 /* write the recovery data to the end of the file */
2045 if (transaction_setup_recovery(tdb
, &magic_offset
) == -1) {
2046 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_commit: failed to setup recovery data\n"));
2047 tdb_brlock(tdb
, GLOBAL_LOCK
, F_UNLCK
, F_SETLKW
, 0, 1);
2048 tdb_transaction_cancel(tdb
);
2053 /* expand the file to the new size if needed */
2054 if (tdb
->map_size
!= tdb
->transaction
->old_map_size
) {
2055 if (methods
->tdb_expand_file(tdb
, tdb
->transaction
->old_map_size
,
2057 tdb
->transaction
->old_map_size
) == -1) {
2058 tdb
->ecode
= TDB_ERR_IO
;
2059 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_commit: expansion failed\n"));
2060 tdb_brlock(tdb
, GLOBAL_LOCK
, F_UNLCK
, F_SETLKW
, 0, 1);
2061 tdb_transaction_cancel(tdb
);
2064 tdb
->map_size
= tdb
->transaction
->old_map_size
;
2065 methods
->tdb_oob(tdb
, tdb
->map_size
+ 1, 1);
2068 /* perform all the writes */
2069 while (tdb
->transaction
->elements
) {
2070 struct tdb_transaction_el
*el
= tdb
->transaction
->elements
;
2072 if (methods
->tdb_write(tdb
, el
->offset
, el
->data
, el
->length
) == -1) {
2073 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_commit: write failed during commit\n"));
2075 /* we've overwritten part of the data and
2076 possibly expanded the file, so we need to
2077 run the crash recovery code */
2078 tdb
->methods
= methods
;
2079 tdb_transaction_recover(tdb
);
2081 tdb_transaction_cancel(tdb
);
2082 tdb_brlock(tdb
, GLOBAL_LOCK
, F_UNLCK
, F_SETLKW
, 0, 1);
2084 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_commit: write failed\n"));
2087 tdb
->transaction
->elements
= el
->next
;
2092 if (!(tdb
->flags
& TDB_NOSYNC
)) {
2093 /* ensure the new data is on disk */
2094 if (transaction_sync(tdb
, 0, tdb
->map_size
) == -1) {
2098 /* remove the recovery marker */
2099 if (methods
->tdb_write(tdb
, magic_offset
, &zero
, 4) == -1) {
2100 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_commit: failed to remove recovery magic\n"));
2104 /* ensure the recovery marker has been removed on disk */
2105 if (transaction_sync(tdb
, magic_offset
, 4) == -1) {
2110 tdb_brlock(tdb
, GLOBAL_LOCK
, F_UNLCK
, F_SETLKW
, 0, 1);
2113 TODO: maybe write to some dummy hdr field, or write to magic
2114 offset without mmap, before the last sync, instead of the
2118 /* on some systems (like Linux 2.6.x) changes via mmap/msync
2119 don't change the mtime of the file, this means the file may
2120 not be backed up (as tdb rounding to block sizes means that
2121 file size changes are quite rare too). The following forces
2122 mtime changes when a transaction completes */
2124 utime(tdb
->name
, NULL
);
2127 /* use a transaction cancel to free memory and remove the
2128 transaction locks */
2129 tdb_transaction_cancel(tdb
);
2135 recover from an aborted transaction. Must be called with exclusive
2136 database write access already established (including the global
2137 lock to prevent new processes attaching)
2139 int tdb_transaction_recover(struct tdb_context
*tdb
)
2141 tdb_off_t recovery_head
, recovery_eof
;
2142 unsigned char *data
, *p
;
2144 struct list_struct rec
;
2146 /* find the recovery area */
2147 if (tdb_ofs_read(tdb
, TDB_RECOVERY_HEAD
, &recovery_head
) == -1) {
2148 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_recover: failed to read recovery head\n"));
2149 tdb
->ecode
= TDB_ERR_IO
;
2153 if (recovery_head
== 0) {
2154 /* we have never allocated a recovery record */
2158 /* read the recovery record */
2159 if (tdb
->methods
->tdb_read(tdb
, recovery_head
, &rec
,
2160 sizeof(rec
), DOCONV()) == -1) {
2161 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_recover: failed to read recovery record\n"));
2162 tdb
->ecode
= TDB_ERR_IO
;
2166 if (rec
.magic
!= TDB_RECOVERY_MAGIC
) {
2167 /* there is no valid recovery data */
2171 if (tdb
->read_only
) {
2172 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_recover: attempt to recover read only database\n"));
2173 tdb
->ecode
= TDB_ERR_CORRUPT
;
2177 recovery_eof
= rec
.key_len
;
2179 data
= (unsigned char *)malloc(rec
.data_len
);
2181 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_recover: failed to allocate recovery data\n"));
2182 tdb
->ecode
= TDB_ERR_OOM
;
2186 /* read the full recovery data */
2187 if (tdb
->methods
->tdb_read(tdb
, recovery_head
+ sizeof(rec
), data
,
2188 rec
.data_len
, 0) == -1) {
2189 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_recover: failed to read recovery data\n"));
2190 tdb
->ecode
= TDB_ERR_IO
;
2194 /* recover the file data */
2196 while (p
+8 < data
+ rec
.data_len
) {
2202 memcpy(&len
, p
+4, 4);
2204 if (tdb
->methods
->tdb_write(tdb
, ofs
, p
+8, len
) == -1) {
2206 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_recover: failed to recover %d bytes at offset %d\n", len
, ofs
));
2207 tdb
->ecode
= TDB_ERR_IO
;
2215 if (transaction_sync(tdb
, 0, tdb
->map_size
) == -1) {
2216 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_recover: failed to sync recovery\n"));
2217 tdb
->ecode
= TDB_ERR_IO
;
2221 /* if the recovery area is after the recovered eof then remove it */
2222 if (recovery_eof
<= recovery_head
) {
2223 if (tdb_ofs_write(tdb
, TDB_RECOVERY_HEAD
, &zero
) == -1) {
2224 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_recover: failed to remove recovery head\n"));
2225 tdb
->ecode
= TDB_ERR_IO
;
2230 /* remove the recovery magic */
2231 if (tdb_ofs_write(tdb
, recovery_head
+ offsetof(struct list_struct
, magic
),
2233 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_recover: failed to remove recovery magic\n"));
2234 tdb
->ecode
= TDB_ERR_IO
;
2238 /* reduce the file size to the old size */
2240 if (ftruncate(tdb
->fd
, recovery_eof
) != 0) {
2241 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_recover: failed to reduce to recovery size\n"));
2242 tdb
->ecode
= TDB_ERR_IO
;
2245 tdb
->map_size
= recovery_eof
;
2248 if (transaction_sync(tdb
, 0, recovery_eof
) == -1) {
2249 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_transaction_recover: failed to sync2 recovery\n"));
2250 tdb
->ecode
= TDB_ERR_IO
;
2254 TDB_LOG((tdb
, TDB_DEBUG_TRACE
, "tdb_transaction_recover: recovered %d byte database\n",
2261 /* file: freelist.c */
2263 /* read a freelist record and check for simple errors */
2264 static int tdb_rec_free_read(struct tdb_context
*tdb
, tdb_off_t off
, struct list_struct
*rec
)
2266 if (tdb
->methods
->tdb_read(tdb
, off
, rec
, sizeof(*rec
),DOCONV()) == -1)
2269 if (rec
->magic
== TDB_MAGIC
) {
2270 /* this happens when a app is showdown while deleting a record - we should
2271 not completely fail when this happens */
2272 TDB_LOG((tdb
, TDB_DEBUG_WARNING
, "tdb_rec_free_read non-free magic 0x%x at offset=%d - fixing\n",
2274 rec
->magic
= TDB_FREE_MAGIC
;
2275 if (tdb
->methods
->tdb_write(tdb
, off
, rec
, sizeof(*rec
)) == -1)
2279 if (rec
->magic
!= TDB_FREE_MAGIC
) {
2280 /* Ensure ecode is set for log fn. */
2281 tdb
->ecode
= TDB_ERR_CORRUPT
;
2282 TDB_LOG((tdb
, TDB_DEBUG_WARNING
, "tdb_rec_free_read bad magic 0x%x at offset=%d\n",
2284 return TDB_ERRCODE(TDB_ERR_CORRUPT
, -1);
2286 if (tdb
->methods
->tdb_oob(tdb
, rec
->next
+sizeof(*rec
), 0) != 0)
2293 /* Remove an element from the freelist. Must have alloc lock. */
2294 static int remove_from_freelist(struct tdb_context
*tdb
, tdb_off_t off
, tdb_off_t next
)
2296 tdb_off_t last_ptr
, i
;
2298 /* read in the freelist top */
2299 last_ptr
= FREELIST_TOP
;
2300 while (tdb_ofs_read(tdb
, last_ptr
, &i
) != -1 && i
!= 0) {
2302 /* We've found it! */
2303 return tdb_ofs_write(tdb
, last_ptr
, &next
);
2305 /* Follow chain (next offset is at start of record) */
2308 TDB_LOG((tdb
, TDB_DEBUG_FATAL
,"remove_from_freelist: not on list at off=%d\n", off
));
2309 return TDB_ERRCODE(TDB_ERR_CORRUPT
, -1);
2313 /* update a record tailer (must hold allocation lock) */
2314 static int update_tailer(struct tdb_context
*tdb
, tdb_off_t offset
,
2315 const struct list_struct
*rec
)
2317 tdb_off_t totalsize
;
2319 /* Offset of tailer from record header */
2320 totalsize
= sizeof(*rec
) + rec
->rec_len
;
2321 return tdb_ofs_write(tdb
, offset
+ totalsize
- sizeof(tdb_off_t
),
2325 /* Add an element into the freelist. Merge adjacent records if
2327 int tdb_free(struct tdb_context
*tdb
, tdb_off_t offset
, struct list_struct
*rec
)
2329 tdb_off_t right
, left
;
2331 /* Allocation and tailer lock */
2332 if (tdb_lock(tdb
, -1, F_WRLCK
) != 0)
2335 /* set an initial tailer, so if we fail we don't leave a bogus record */
2336 if (update_tailer(tdb
, offset
, rec
) != 0) {
2337 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_free: update_tailer failed!\n"));
2341 /* Look right first (I'm an Australian, dammit) */
2342 right
= offset
+ sizeof(*rec
) + rec
->rec_len
;
2343 if (right
+ sizeof(*rec
) <= tdb
->map_size
) {
2344 struct list_struct r
;
2346 if (tdb
->methods
->tdb_read(tdb
, right
, &r
, sizeof(r
), DOCONV()) == -1) {
2347 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_free: right read failed at %u\n", right
));
2351 /* If it's free, expand to include it. */
2352 if (r
.magic
== TDB_FREE_MAGIC
) {
2353 if (remove_from_freelist(tdb
, right
, r
.next
) == -1) {
2354 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_free: right free failed at %u\n", right
));
2357 rec
->rec_len
+= sizeof(r
) + r
.rec_len
;
2363 left
= offset
- sizeof(tdb_off_t
);
2364 if (left
> TDB_DATA_START(tdb
->header
.hash_size
)) {
2365 struct list_struct l
;
2368 /* Read in tailer and jump back to header */
2369 if (tdb_ofs_read(tdb
, left
, &leftsize
) == -1) {
2370 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_free: left offset read failed at %u\n", left
));
2374 /* it could be uninitialised data */
2375 if (leftsize
== 0 || leftsize
== TDB_PAD_U32
) {
2379 left
= offset
- leftsize
;
2381 /* Now read in record */
2382 if (tdb
->methods
->tdb_read(tdb
, left
, &l
, sizeof(l
), DOCONV()) == -1) {
2383 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_free: left read failed at %u (%u)\n", left
, leftsize
));
2387 /* If it's free, expand to include it. */
2388 if (l
.magic
== TDB_FREE_MAGIC
) {
2389 if (remove_from_freelist(tdb
, left
, l
.next
) == -1) {
2390 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_free: left free failed at %u\n", left
));
2394 rec
->rec_len
+= leftsize
;
2400 if (update_tailer(tdb
, offset
, rec
) == -1) {
2401 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_free: update_tailer failed at %u\n", offset
));
2405 /* Now, prepend to free list */
2406 rec
->magic
= TDB_FREE_MAGIC
;
2408 if (tdb_ofs_read(tdb
, FREELIST_TOP
, &rec
->next
) == -1 ||
2409 tdb_rec_write(tdb
, offset
, rec
) == -1 ||
2410 tdb_ofs_write(tdb
, FREELIST_TOP
, &offset
) == -1) {
2411 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_free record write failed at offset=%d\n", offset
));
2415 /* And we're done. */
2416 tdb_unlock(tdb
, -1, F_WRLCK
);
2420 tdb_unlock(tdb
, -1, F_WRLCK
);
2426 the core of tdb_allocate - called when we have decided which
2427 free list entry to use
2429 static tdb_off_t
tdb_allocate_ofs(struct tdb_context
*tdb
, tdb_len_t length
, tdb_off_t rec_ptr
,
2430 struct list_struct
*rec
, tdb_off_t last_ptr
)
2432 struct list_struct newrec
;
2433 tdb_off_t newrec_ptr
;
2435 memset(&newrec
, '\0', sizeof(newrec
));
2437 /* found it - now possibly split it up */
2438 if (rec
->rec_len
> length
+ MIN_REC_SIZE
) {
2439 /* Length of left piece */
2440 length
= TDB_ALIGN(length
, TDB_ALIGNMENT
);
2442 /* Right piece to go on free list */
2443 newrec
.rec_len
= rec
->rec_len
- (sizeof(*rec
) + length
);
2444 newrec_ptr
= rec_ptr
+ sizeof(*rec
) + length
;
2446 /* And left record is shortened */
2447 rec
->rec_len
= length
;
2452 /* Remove allocated record from the free list */
2453 if (tdb_ofs_write(tdb
, last_ptr
, &rec
->next
) == -1) {
2457 /* Update header: do this before we drop alloc
2458 lock, otherwise tdb_free() might try to
2459 merge with us, thinking we're free.
2460 (Thanks Jeremy Allison). */
2461 rec
->magic
= TDB_MAGIC
;
2462 if (tdb_rec_write(tdb
, rec_ptr
, rec
) == -1) {
2466 /* Did we create new block? */
2468 /* Update allocated record tailer (we
2470 if (update_tailer(tdb
, rec_ptr
, rec
) == -1) {
2474 /* Free new record */
2475 if (tdb_free(tdb
, newrec_ptr
, &newrec
) == -1) {
2480 /* all done - return the new record offset */
2484 /* allocate some space from the free list. The offset returned points
2485 to a unconnected list_struct within the database with room for at
2486 least length bytes of total data
2488 0 is returned if the space could not be allocated
2490 tdb_off_t
tdb_allocate(struct tdb_context
*tdb
, tdb_len_t length
, struct list_struct
*rec
)
2492 tdb_off_t rec_ptr
, last_ptr
, newrec_ptr
;
2494 tdb_off_t rec_ptr
, last_ptr
;
2498 if (tdb_lock(tdb
, -1, F_WRLCK
) == -1)
2501 /* Extra bytes required for tailer */
2502 length
+= sizeof(tdb_off_t
);
2505 last_ptr
= FREELIST_TOP
;
2507 /* read in the freelist top */
2508 if (tdb_ofs_read(tdb
, FREELIST_TOP
, &rec_ptr
) == -1)
2511 bestfit
.rec_ptr
= 0;
2512 bestfit
.last_ptr
= 0;
2513 bestfit
.rec_len
= 0;
2516 this is a best fit allocation strategy. Originally we used
2517 a first fit strategy, but it suffered from massive fragmentation
2518 issues when faced with a slowly increasing record size.
2521 if (tdb_rec_free_read(tdb
, rec_ptr
, rec
) == -1) {
2525 if (rec
->rec_len
>= length
) {
2526 if (bestfit
.rec_ptr
== 0 ||
2527 rec
->rec_len
< bestfit
.rec_len
) {
2528 bestfit
.rec_len
= rec
->rec_len
;
2529 bestfit
.rec_ptr
= rec_ptr
;
2530 bestfit
.last_ptr
= last_ptr
;
2531 /* consider a fit to be good enough if
2532 we aren't wasting more than half
2534 if (bestfit
.rec_len
< 2*length
) {
2540 /* move to the next record */
2542 rec_ptr
= rec
->next
;
2545 if (bestfit
.rec_ptr
!= 0) {
2546 if (tdb_rec_free_read(tdb
, bestfit
.rec_ptr
, rec
) == -1) {
2550 newrec_ptr
= tdb_allocate_ofs(tdb
, length
, bestfit
.rec_ptr
, rec
, bestfit
.last_ptr
);
2551 tdb_unlock(tdb
, -1, F_WRLCK
);
2555 /* we didn't find enough space. See if we can expand the
2556 database and if we can then try again */
2557 if (tdb_expand(tdb
, length
+ sizeof(*rec
)) == 0)
2560 tdb_unlock(tdb
, -1, F_WRLCK
);
2564 /* file: freelistcheck.c */
2566 /* Check the freelist is good and contains no loops.
2567 Very memory intensive - only do this as a consistency
2568 checker. Heh heh - uses an in memory tdb as the storage
2569 for the "seen" record list. For some reason this strikes
2570 me as extremely clever as I don't have to write another tree
2571 data structure implementation :-).
2574 static int seen_insert(struct tdb_context
*mem_tdb
, tdb_off_t rec_ptr
)
2578 memset(&data
, '\0', sizeof(data
));
2579 key
.dptr
= (unsigned char *)&rec_ptr
;
2580 key
.dsize
= sizeof(rec_ptr
);
2581 return tdb_store(mem_tdb
, key
, data
, TDB_INSERT
);
2584 int tdb_validate_freelist(struct tdb_context
*tdb
, int *pnum_entries
)
2586 struct tdb_context
*mem_tdb
= NULL
;
2587 struct list_struct rec
;
2588 tdb_off_t rec_ptr
, last_ptr
;
2593 mem_tdb
= tdb_open("flval", tdb
->header
.hash_size
,
2594 TDB_INTERNAL
, O_RDWR
, 0600);
2599 if (tdb_lock(tdb
, -1, F_WRLCK
) == -1) {
2604 last_ptr
= FREELIST_TOP
;
2606 /* Store the FREELIST_TOP record. */
2607 if (seen_insert(mem_tdb
, last_ptr
) == -1) {
2608 ret
= TDB_ERRCODE(TDB_ERR_CORRUPT
, -1);
2612 /* read in the freelist top */
2613 if (tdb_ofs_read(tdb
, FREELIST_TOP
, &rec_ptr
) == -1) {
2619 /* If we can't store this record (we've seen it
2620 before) then the free list has a loop and must
2623 if (seen_insert(mem_tdb
, rec_ptr
)) {
2624 ret
= TDB_ERRCODE(TDB_ERR_CORRUPT
, -1);
2628 if (tdb_rec_free_read(tdb
, rec_ptr
, &rec
) == -1) {
2632 /* move to the next record */
2643 tdb_unlock(tdb
, -1, F_WRLCK
);
2647 /* file: traverse.c */
2649 /* Uses traverse lock: 0 = finish, -1 = error, other = record offset */
2650 static int tdb_next_lock(struct tdb_context
*tdb
, struct tdb_traverse_lock
*tlock
,
2651 struct list_struct
*rec
)
2653 int want_next
= (tlock
->off
!= 0);
2655 /* Lock each chain from the start one. */
2656 for (; tlock
->hash
< tdb
->header
.hash_size
; tlock
->hash
++) {
2657 if (!tlock
->off
&& tlock
->hash
!= 0) {
2658 /* this is an optimisation for the common case where
2659 the hash chain is empty, which is particularly
2660 common for the use of tdb with ldb, where large
2661 hashes are used. In that case we spend most of our
2662 time in tdb_brlock(), locking empty hash chains.
2664 To avoid this, we do an unlocked pre-check to see
2665 if the hash chain is empty before starting to look
2666 inside it. If it is empty then we can avoid that
2667 hash chain. If it isn't empty then we can't believe
2668 the value we get back, as we read it without a
2669 lock, so instead we get the lock and re-fetch the
2672 Notice that not doing this optimisation on the
2673 first hash chain is critical. We must guarantee
2674 that we have done at least one fcntl lock at the
2675 start of a search to guarantee that memory is
2676 coherent on SMP systems. If records are added by
2677 others during the search then that's OK, and we
2678 could possibly miss those with this trick, but we
2679 could miss them anyway without this trick, so the
2680 semantics don't change.
2682 With a non-indexed ldb search this trick gains us a
2683 factor of around 80 in speed on a linux 2.6.x
2684 system (testing using ldbtest).
2686 tdb
->methods
->next_hash_chain(tdb
, &tlock
->hash
);
2687 if (tlock
->hash
== tdb
->header
.hash_size
) {
2692 if (tdb_lock(tdb
, tlock
->hash
, tlock
->lock_rw
) == -1)
2695 /* No previous record? Start at top of chain. */
2697 if (tdb_ofs_read(tdb
, TDB_HASH_TOP(tlock
->hash
),
2701 /* Otherwise unlock the previous record. */
2702 if (tdb_unlock_record(tdb
, tlock
->off
) != 0)
2707 /* We have offset of old record: grab next */
2708 if (tdb_rec_read(tdb
, tlock
->off
, rec
) == -1)
2710 tlock
->off
= rec
->next
;
2713 /* Iterate through chain */
2714 while( tlock
->off
) {
2716 if (tdb_rec_read(tdb
, tlock
->off
, rec
) == -1)
2719 /* Detect infinite loops. From "Shlomi Yaakobovich" <Shlomi@exanet.com>. */
2720 if (tlock
->off
== rec
->next
) {
2721 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_next_lock: loop detected.\n"));
2725 if (!TDB_DEAD(rec
)) {
2726 /* Woohoo: we found one! */
2727 if (tdb_lock_record(tdb
, tlock
->off
) != 0)
2732 /* Try to clean dead ones from old traverses */
2733 current
= tlock
->off
;
2734 tlock
->off
= rec
->next
;
2735 if (!(tdb
->read_only
|| tdb
->traverse_read
) &&
2736 tdb_do_delete(tdb
, current
, rec
) != 0)
2739 tdb_unlock(tdb
, tlock
->hash
, tlock
->lock_rw
);
2742 /* We finished iteration without finding anything */
2743 return TDB_ERRCODE(TDB_SUCCESS
, 0);
2747 if (tdb_unlock(tdb
, tlock
->hash
, tlock
->lock_rw
) != 0)
2748 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_next_lock: On error unlock failed!\n"));
2752 /* traverse the entire database - calling fn(tdb, key, data) on each element.
2753 return -1 on error or the record count traversed
2754 if fn is NULL then it is not called
2755 a non-zero return value from fn() indicates that the traversal should stop
2757 static int tdb_traverse_internal(struct tdb_context
*tdb
,
2758 tdb_traverse_func fn
, void *private_data
,
2759 struct tdb_traverse_lock
*tl
)
2762 struct list_struct rec
;
2765 /* This was in the initialization, above, but the IRIX compiler
2766 * did not like it. crh
2768 tl
->next
= tdb
->travlocks
.next
;
2770 /* fcntl locks don't stack: beware traverse inside traverse */
2771 tdb
->travlocks
.next
= tl
;
2773 /* tdb_next_lock places locks on the record returned, and its chain */
2774 while ((ret
= tdb_next_lock(tdb
, tl
, &rec
)) > 0) {
2776 /* now read the full record */
2777 key
.dptr
= tdb_alloc_read(tdb
, tl
->off
+ sizeof(rec
),
2778 rec
.key_len
+ rec
.data_len
);
2781 if (tdb_unlock(tdb
, tl
->hash
, tl
->lock_rw
) != 0)
2783 if (tdb_unlock_record(tdb
, tl
->off
) != 0)
2784 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_traverse: key.dptr == NULL and unlock_record failed!\n"));
2787 key
.dsize
= rec
.key_len
;
2788 dbuf
.dptr
= key
.dptr
+ rec
.key_len
;
2789 dbuf
.dsize
= rec
.data_len
;
2791 /* Drop chain lock, call out */
2792 if (tdb_unlock(tdb
, tl
->hash
, tl
->lock_rw
) != 0) {
2794 SAFE_FREE(key
.dptr
);
2797 if (fn
&& fn(tdb
, key
, dbuf
, private_data
)) {
2798 /* They want us to terminate traversal */
2800 if (tdb_unlock_record(tdb
, tl
->off
) != 0) {
2801 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_traverse: unlock_record failed!\n"));;
2804 SAFE_FREE(key
.dptr
);
2807 SAFE_FREE(key
.dptr
);
2810 tdb
->travlocks
.next
= tl
->next
;
2819 a write style traverse - temporarily marks the db read only
2821 int tdb_traverse_read(struct tdb_context
*tdb
,
2822 tdb_traverse_func fn
, void *private_data
)
2824 struct tdb_traverse_lock tl
= { NULL
, 0, 0, F_RDLCK
};
2827 /* we need to get a read lock on the transaction lock here to
2828 cope with the lock ordering semantics of solaris10 */
2829 if (tdb_transaction_lock(tdb
, F_RDLCK
)) {
2833 tdb
->traverse_read
++;
2834 ret
= tdb_traverse_internal(tdb
, fn
, private_data
, &tl
);
2835 tdb
->traverse_read
--;
2837 tdb_transaction_unlock(tdb
);
2843 a write style traverse - needs to get the transaction lock to
2846 int tdb_traverse(struct tdb_context
*tdb
,
2847 tdb_traverse_func fn
, void *private_data
)
2849 struct tdb_traverse_lock tl
= { NULL
, 0, 0, F_WRLCK
};
2852 if (tdb
->read_only
|| tdb
->traverse_read
) {
2853 return tdb_traverse_read(tdb
, fn
, private_data
);
2856 if (tdb_transaction_lock(tdb
, F_WRLCK
)) {
2860 ret
= tdb_traverse_internal(tdb
, fn
, private_data
, &tl
);
2862 tdb_transaction_unlock(tdb
);
2868 /* find the first entry in the database and return its key */
2869 TDB_DATA
tdb_firstkey(struct tdb_context
*tdb
)
2872 struct list_struct rec
;
2874 /* release any old lock */
2875 if (tdb_unlock_record(tdb
, tdb
->travlocks
.off
) != 0)
2877 tdb
->travlocks
.off
= tdb
->travlocks
.hash
= 0;
2878 tdb
->travlocks
.lock_rw
= F_RDLCK
;
2880 /* Grab first record: locks chain and returned record. */
2881 if (tdb_next_lock(tdb
, &tdb
->travlocks
, &rec
) <= 0)
2883 /* now read the key */
2884 key
.dsize
= rec
.key_len
;
2885 key
.dptr
=tdb_alloc_read(tdb
,tdb
->travlocks
.off
+sizeof(rec
),key
.dsize
);
2887 /* Unlock the hash chain of the record we just read. */
2888 if (tdb_unlock(tdb
, tdb
->travlocks
.hash
, tdb
->travlocks
.lock_rw
) != 0)
2889 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_firstkey: error occurred while tdb_unlocking!\n"));
2893 /* find the next entry in the database, returning its key */
2894 TDB_DATA
tdb_nextkey(struct tdb_context
*tdb
, TDB_DATA oldkey
)
2897 TDB_DATA key
= tdb_null
;
2898 struct list_struct rec
;
2899 unsigned char *k
= NULL
;
2901 /* Is locked key the old key? If so, traverse will be reliable. */
2902 if (tdb
->travlocks
.off
) {
2903 if (tdb_lock(tdb
,tdb
->travlocks
.hash
,tdb
->travlocks
.lock_rw
))
2905 if (tdb_rec_read(tdb
, tdb
->travlocks
.off
, &rec
) == -1
2906 || !(k
= tdb_alloc_read(tdb
,tdb
->travlocks
.off
+sizeof(rec
),
2908 || memcmp(k
, oldkey
.dptr
, oldkey
.dsize
) != 0) {
2909 /* No, it wasn't: unlock it and start from scratch */
2910 if (tdb_unlock_record(tdb
, tdb
->travlocks
.off
) != 0) {
2914 if (tdb_unlock(tdb
, tdb
->travlocks
.hash
, tdb
->travlocks
.lock_rw
) != 0) {
2918 tdb
->travlocks
.off
= 0;
2924 if (!tdb
->travlocks
.off
) {
2925 /* No previous element: do normal find, and lock record */
2926 tdb
->travlocks
.off
= tdb_find_lock_hash(tdb
, oldkey
, tdb
->hash_fn(&oldkey
), tdb
->travlocks
.lock_rw
, &rec
);
2927 if (!tdb
->travlocks
.off
)
2929 tdb
->travlocks
.hash
= BUCKET(rec
.full_hash
);
2930 if (tdb_lock_record(tdb
, tdb
->travlocks
.off
) != 0) {
2931 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_nextkey: lock_record failed (%s)!\n", strerror(errno
)));
2935 oldhash
= tdb
->travlocks
.hash
;
2937 /* Grab next record: locks chain and returned record,
2938 unlocks old record */
2939 if (tdb_next_lock(tdb
, &tdb
->travlocks
, &rec
) > 0) {
2940 key
.dsize
= rec
.key_len
;
2941 key
.dptr
= tdb_alloc_read(tdb
, tdb
->travlocks
.off
+sizeof(rec
),
2943 /* Unlock the chain of this new record */
2944 if (tdb_unlock(tdb
, tdb
->travlocks
.hash
, tdb
->travlocks
.lock_rw
) != 0)
2945 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
2947 /* Unlock the chain of old record */
2948 if (tdb_unlock(tdb
, BUCKET(oldhash
), tdb
->travlocks
.lock_rw
) != 0)
2949 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_nextkey: WARNING tdb_unlock failed!\n"));
2955 static tdb_off_t
tdb_dump_record(struct tdb_context
*tdb
, int hash
,
2958 struct list_struct rec
;
2959 tdb_off_t tailer_ofs
, tailer
;
2961 if (tdb
->methods
->tdb_read(tdb
, offset
, (char *)&rec
,
2962 sizeof(rec
), DOCONV()) == -1) {
2963 printf("ERROR: failed to read record at %u\n", offset
);
2967 printf(" rec: hash=%d offset=0x%08x next=0x%08x rec_len=%d "
2968 "key_len=%d data_len=%d full_hash=0x%x magic=0x%x\n",
2969 hash
, offset
, rec
.next
, rec
.rec_len
, rec
.key_len
, rec
.data_len
,
2970 rec
.full_hash
, rec
.magic
);
2972 tailer_ofs
= offset
+ sizeof(rec
) + rec
.rec_len
- sizeof(tdb_off_t
);
2974 if (tdb_ofs_read(tdb
, tailer_ofs
, &tailer
) == -1) {
2975 printf("ERROR: failed to read tailer at %u\n", tailer_ofs
);
2979 if (tailer
!= rec
.rec_len
+ sizeof(rec
)) {
2980 printf("ERROR: tailer does not match record! tailer=%u totalsize=%u\n",
2981 (unsigned int)tailer
, (unsigned int)(rec
.rec_len
+ sizeof(rec
)));
2986 static int tdb_dump_chain(struct tdb_context
*tdb
, int i
)
2988 tdb_off_t rec_ptr
, top
;
2990 top
= TDB_HASH_TOP(i
);
2992 if (tdb_lock(tdb
, i
, F_WRLCK
) != 0)
2995 if (tdb_ofs_read(tdb
, top
, &rec_ptr
) == -1)
2996 return tdb_unlock(tdb
, i
, F_WRLCK
);
2999 printf("hash=%d\n", i
);
3002 rec_ptr
= tdb_dump_record(tdb
, i
, rec_ptr
);
3005 return tdb_unlock(tdb
, i
, F_WRLCK
);
3008 void tdb_dump_all(struct tdb_context
*tdb
)
3011 for (i
=0;i
<tdb
->header
.hash_size
;i
++) {
3012 tdb_dump_chain(tdb
, i
);
3014 printf("freelist:\n");
3015 tdb_dump_chain(tdb
, -1);
3018 int tdb_printfreelist(struct tdb_context
*tdb
)
3021 long total_free
= 0;
3022 tdb_off_t offset
, rec_ptr
;
3023 struct list_struct rec
;
3025 if ((ret
= tdb_lock(tdb
, -1, F_WRLCK
)) != 0)
3028 offset
= FREELIST_TOP
;
3030 /* read in the freelist top */
3031 if (tdb_ofs_read(tdb
, offset
, &rec_ptr
) == -1) {
3032 tdb_unlock(tdb
, -1, F_WRLCK
);
3036 printf("freelist top=[0x%08x]\n", rec_ptr
);
3038 if (tdb
->methods
->tdb_read(tdb
, rec_ptr
, (char *)&rec
,
3039 sizeof(rec
), DOCONV()) == -1) {
3040 tdb_unlock(tdb
, -1, F_WRLCK
);
3044 if (rec
.magic
!= TDB_FREE_MAGIC
) {
3045 printf("bad magic 0x%08x in free list\n", rec
.magic
);
3046 tdb_unlock(tdb
, -1, F_WRLCK
);
3050 printf("entry offset=[0x%08x], rec.rec_len = [0x%08x (%d)] (end = 0x%08x)\n",
3051 rec_ptr
, rec
.rec_len
, rec
.rec_len
, rec_ptr
+ rec
.rec_len
);
3052 total_free
+= rec
.rec_len
;
3054 /* move to the next record */
3057 printf("total rec_len = [0x%08x (%d)]\n", (int)total_free
,
3060 return tdb_unlock(tdb
, -1, F_WRLCK
);
3066 non-blocking increment of the tdb sequence number if the tdb has been opened using
3069 void tdb_increment_seqnum_nonblock(struct tdb_context
*tdb
)
3073 if (!(tdb
->flags
& TDB_SEQNUM
)) {
3077 /* we ignore errors from this, as we have no sane way of
3080 tdb_ofs_read(tdb
, TDB_SEQNUM_OFS
, &seqnum
);
3082 tdb_ofs_write(tdb
, TDB_SEQNUM_OFS
, &seqnum
);
3086 increment the tdb sequence number if the tdb has been opened using
3089 static void tdb_increment_seqnum(struct tdb_context
*tdb
)
3091 if (!(tdb
->flags
& TDB_SEQNUM
)) {
3095 if (tdb_brlock(tdb
, TDB_SEQNUM_OFS
, F_WRLCK
, F_SETLKW
, 1, 1) != 0) {
3099 tdb_increment_seqnum_nonblock(tdb
);
3101 tdb_brlock(tdb
, TDB_SEQNUM_OFS
, F_UNLCK
, F_SETLKW
, 1, 1);
3104 static int tdb_key_compare(TDB_DATA key
, TDB_DATA data
, void *private_data
)
3106 return memcmp(data
.dptr
, key
.dptr
, data
.dsize
);
3109 /* Returns 0 on fail. On success, return offset of record, and fills
3111 static tdb_off_t
tdb_find(struct tdb_context
*tdb
, TDB_DATA key
, u32 hash
,
3112 struct list_struct
*r
)
3116 /* read in the hash top */
3117 if (tdb_ofs_read(tdb
, TDB_HASH_TOP(hash
), &rec_ptr
) == -1)
3120 /* keep looking until we find the right record */
3122 if (tdb_rec_read(tdb
, rec_ptr
, r
) == -1)
3125 if (!TDB_DEAD(r
) && hash
==r
->full_hash
3126 && key
.dsize
==r
->key_len
3127 && tdb_parse_data(tdb
, key
, rec_ptr
+ sizeof(*r
),
3128 r
->key_len
, tdb_key_compare
,
3134 return TDB_ERRCODE(TDB_ERR_NOEXIST
, 0);
3137 /* As tdb_find, but if you succeed, keep the lock */
3138 tdb_off_t
tdb_find_lock_hash(struct tdb_context
*tdb
, TDB_DATA key
, u32 hash
, int locktype
,
3139 struct list_struct
*rec
)
3143 if (tdb_lock(tdb
, BUCKET(hash
), locktype
) == -1)
3145 if (!(rec_ptr
= tdb_find(tdb
, key
, hash
, rec
)))
3146 tdb_unlock(tdb
, BUCKET(hash
), locktype
);
3151 /* update an entry in place - this only works if the new data size
3152 is <= the old data size and the key exists.
3153 on failure return -1.
3155 static int tdb_update_hash(struct tdb_context
*tdb
, TDB_DATA key
, u32 hash
, TDB_DATA dbuf
)
3157 struct list_struct rec
;
3161 if (!(rec_ptr
= tdb_find(tdb
, key
, hash
, &rec
)))
3164 /* must be long enough key, data and tailer */
3165 if (rec
.rec_len
< key
.dsize
+ dbuf
.dsize
+ sizeof(tdb_off_t
)) {
3166 tdb
->ecode
= TDB_SUCCESS
; /* Not really an error */
3170 if (tdb
->methods
->tdb_write(tdb
, rec_ptr
+ sizeof(rec
) + rec
.key_len
,
3171 dbuf
.dptr
, dbuf
.dsize
) == -1)
3174 if (dbuf
.dsize
!= rec
.data_len
) {
3176 rec
.data_len
= dbuf
.dsize
;
3177 return tdb_rec_write(tdb
, rec_ptr
, &rec
);
3183 /* find an entry in the database given a key */
3184 /* If an entry doesn't exist tdb_err will be set to
3185 * TDB_ERR_NOEXIST. If a key has no data attached
3186 * then the TDB_DATA will have zero length but
3187 * a non-zero pointer
3189 TDB_DATA
tdb_fetch(struct tdb_context
*tdb
, TDB_DATA key
)
3192 struct list_struct rec
;
3196 /* find which hash bucket it is in */
3197 hash
= tdb
->hash_fn(&key
);
3198 if (!(rec_ptr
= tdb_find_lock_hash(tdb
,key
,hash
,F_RDLCK
,&rec
)))
3201 ret
.dptr
= tdb_alloc_read(tdb
, rec_ptr
+ sizeof(rec
) + rec
.key_len
,
3203 ret
.dsize
= rec
.data_len
;
3204 tdb_unlock(tdb
, BUCKET(rec
.full_hash
), F_RDLCK
);
3209 * Find an entry in the database and hand the record's data to a parsing
3210 * function. The parsing function is executed under the chain read lock, so it
3211 * should be fast and should not block on other syscalls.
3213 * DONT CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS.
3215 * For mmapped tdb's that do not have a transaction open it points the parsing
3216 * function directly at the mmap area, it avoids the malloc/memcpy in this
3217 * case. If a transaction is open or no mmap is available, it has to do
3218 * malloc/read/parse/free.
3220 * This is interesting for all readers of potentially large data structures in
3221 * the tdb records, ldb indexes being one example.
3224 int tdb_parse_record(struct tdb_context
*tdb
, TDB_DATA key
,
3225 int (*parser
)(TDB_DATA key
, TDB_DATA data
,
3226 void *private_data
),
3230 struct list_struct rec
;
3234 /* find which hash bucket it is in */
3235 hash
= tdb
->hash_fn(&key
);
3237 if (!(rec_ptr
= tdb_find_lock_hash(tdb
,key
,hash
,F_RDLCK
,&rec
))) {
3238 return TDB_ERRCODE(TDB_ERR_NOEXIST
, 0);
3241 ret
= tdb_parse_data(tdb
, key
, rec_ptr
+ sizeof(rec
) + rec
.key_len
,
3242 rec
.data_len
, parser
, private_data
);
3244 tdb_unlock(tdb
, BUCKET(rec
.full_hash
), F_RDLCK
);
3249 /* check if an entry in the database exists
3251 note that 1 is returned if the key is found and 0 is returned if not found
3252 this doesn't match the conventions in the rest of this module, but is
3253 compatible with gdbm
3255 static int tdb_exists_hash(struct tdb_context
*tdb
, TDB_DATA key
, u32 hash
)
3257 struct list_struct rec
;
3259 if (tdb_find_lock_hash(tdb
, key
, hash
, F_RDLCK
, &rec
) == 0)
3261 tdb_unlock(tdb
, BUCKET(rec
.full_hash
), F_RDLCK
);
3265 int tdb_exists(struct tdb_context
*tdb
, TDB_DATA key
)
3267 u32 hash
= tdb
->hash_fn(&key
);
3268 return tdb_exists_hash(tdb
, key
, hash
);
3271 /* actually delete an entry in the database given the offset */
3272 int tdb_do_delete(struct tdb_context
*tdb
, tdb_off_t rec_ptr
, struct list_struct
*rec
)
3274 tdb_off_t last_ptr
, i
;
3275 struct list_struct lastrec
;
3277 if (tdb
->read_only
|| tdb
->traverse_read
) return -1;
3279 if (tdb_write_lock_record(tdb
, rec_ptr
) == -1) {
3280 /* Someone traversing here: mark it as dead */
3281 rec
->magic
= TDB_DEAD_MAGIC
;
3282 return tdb_rec_write(tdb
, rec_ptr
, rec
);
3284 if (tdb_write_unlock_record(tdb
, rec_ptr
) != 0)
3287 /* find previous record in hash chain */
3288 if (tdb_ofs_read(tdb
, TDB_HASH_TOP(rec
->full_hash
), &i
) == -1)
3290 for (last_ptr
= 0; i
!= rec_ptr
; last_ptr
= i
, i
= lastrec
.next
)
3291 if (tdb_rec_read(tdb
, i
, &lastrec
) == -1)
3294 /* unlink it: next ptr is at start of record. */
3296 last_ptr
= TDB_HASH_TOP(rec
->full_hash
);
3297 if (tdb_ofs_write(tdb
, last_ptr
, &rec
->next
) == -1)
3300 /* recover the space */
3301 if (tdb_free(tdb
, rec_ptr
, rec
) == -1)
3306 static int tdb_count_dead(struct tdb_context
*tdb
, u32 hash
)
3310 struct list_struct rec
;
3312 /* read in the hash top */
3313 if (tdb_ofs_read(tdb
, TDB_HASH_TOP(hash
), &rec_ptr
) == -1)
3317 if (tdb_rec_read(tdb
, rec_ptr
, &rec
) == -1)
3320 if (rec
.magic
== TDB_DEAD_MAGIC
) {
3329 * Purge all DEAD records from a hash chain
3331 static int tdb_purge_dead(struct tdb_context
*tdb
, u32 hash
)
3334 struct list_struct rec
;
3337 if (tdb_lock(tdb
, -1, F_WRLCK
) == -1) {
3341 /* read in the hash top */
3342 if (tdb_ofs_read(tdb
, TDB_HASH_TOP(hash
), &rec_ptr
) == -1)
3348 if (tdb_rec_read(tdb
, rec_ptr
, &rec
) == -1) {
3354 if (rec
.magic
== TDB_DEAD_MAGIC
3355 && tdb_do_delete(tdb
, rec_ptr
, &rec
) == -1) {
3362 tdb_unlock(tdb
, -1, F_WRLCK
);
3366 /* delete an entry in the database given a key */
3367 static int tdb_delete_hash(struct tdb_context
*tdb
, TDB_DATA key
, u32 hash
)
3370 struct list_struct rec
;
3373 if (tdb
->max_dead_records
!= 0) {
3376 * Allow for some dead records per hash chain, mainly for
3377 * tdb's with a very high create/delete rate like locking.tdb.
3380 if (tdb_lock(tdb
, BUCKET(hash
), F_WRLCK
) == -1)
3383 if (tdb_count_dead(tdb
, hash
) >= tdb
->max_dead_records
) {
3385 * Don't let the per-chain freelist grow too large,
3386 * delete all existing dead records
3388 tdb_purge_dead(tdb
, hash
);
3391 if (!(rec_ptr
= tdb_find(tdb
, key
, hash
, &rec
))) {
3392 tdb_unlock(tdb
, BUCKET(hash
), F_WRLCK
);
3397 * Just mark the record as dead.
3399 rec
.magic
= TDB_DEAD_MAGIC
;
3400 ret
= tdb_rec_write(tdb
, rec_ptr
, &rec
);
3403 if (!(rec_ptr
= tdb_find_lock_hash(tdb
, key
, hash
, F_WRLCK
,
3407 ret
= tdb_do_delete(tdb
, rec_ptr
, &rec
);
3411 tdb_increment_seqnum(tdb
);
3414 if (tdb_unlock(tdb
, BUCKET(rec
.full_hash
), F_WRLCK
) != 0)
3415 TDB_LOG((tdb
, TDB_DEBUG_WARNING
, "tdb_delete: WARNING tdb_unlock failed!\n"));
3419 int tdb_delete(struct tdb_context
*tdb
, TDB_DATA key
)
3421 u32 hash
= tdb
->hash_fn(&key
);
3422 return tdb_delete_hash(tdb
, key
, hash
);
3426 * See if we have a dead record around with enough space
3428 static tdb_off_t
tdb_find_dead(struct tdb_context
*tdb
, u32 hash
,
3429 struct list_struct
*r
, tdb_len_t length
)
3433 /* read in the hash top */
3434 if (tdb_ofs_read(tdb
, TDB_HASH_TOP(hash
), &rec_ptr
) == -1)
3437 /* keep looking until we find the right record */
3439 if (tdb_rec_read(tdb
, rec_ptr
, r
) == -1)
3442 if (TDB_DEAD(r
) && r
->rec_len
>= length
) {
3444 * First fit for simple coding, TODO: change to best
3454 /* store an element in the database, replacing any existing element
3457 return 0 on success, -1 on failure
3459 int tdb_store(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA dbuf
, int flag
)
3461 struct list_struct rec
;
3467 if (tdb
->read_only
|| tdb
->traverse_read
) {
3468 tdb
->ecode
= TDB_ERR_RDONLY
;
3472 /* find which hash bucket it is in */
3473 hash
= tdb
->hash_fn(&key
);
3474 if (tdb_lock(tdb
, BUCKET(hash
), F_WRLCK
) == -1)
3477 /* check for it existing, on insert. */
3478 if (flag
== TDB_INSERT
) {
3479 if (tdb_exists_hash(tdb
, key
, hash
)) {
3480 tdb
->ecode
= TDB_ERR_EXISTS
;
3484 /* first try in-place update, on modify or replace. */
3485 if (tdb_update_hash(tdb
, key
, hash
, dbuf
) == 0) {
3488 if (tdb
->ecode
== TDB_ERR_NOEXIST
&&
3489 flag
== TDB_MODIFY
) {
3490 /* if the record doesn't exist and we are in TDB_MODIFY mode then
3491 we should fail the store */
3495 /* reset the error code potentially set by the tdb_update() */
3496 tdb
->ecode
= TDB_SUCCESS
;
3498 /* delete any existing record - if it doesn't exist we don't
3499 care. Doing this first reduces fragmentation, and avoids
3500 coalescing with `allocated' block before it's updated. */
3501 if (flag
!= TDB_INSERT
)
3502 tdb_delete_hash(tdb
, key
, hash
);
3504 /* Copy key+value *before* allocating free space in case malloc
3505 fails and we are left with a dead spot in the tdb. */
3507 if (!(p
= (char *)malloc(key
.dsize
+ dbuf
.dsize
))) {
3508 tdb
->ecode
= TDB_ERR_OOM
;
3512 memcpy(p
, key
.dptr
, key
.dsize
);
3514 memcpy(p
+key
.dsize
, dbuf
.dptr
, dbuf
.dsize
);
3516 if (tdb
->max_dead_records
!= 0) {
3518 * Allow for some dead records per hash chain, look if we can
3519 * find one that can hold the new record. We need enough space
3520 * for key, data and tailer. If we find one, we don't have to
3521 * consult the central freelist.
3523 rec_ptr
= tdb_find_dead(
3525 key
.dsize
+ dbuf
.dsize
+ sizeof(tdb_off_t
));
3528 rec
.key_len
= key
.dsize
;
3529 rec
.data_len
= dbuf
.dsize
;
3530 rec
.full_hash
= hash
;
3531 rec
.magic
= TDB_MAGIC
;
3532 if (tdb_rec_write(tdb
, rec_ptr
, &rec
) == -1
3533 || tdb
->methods
->tdb_write(
3534 tdb
, rec_ptr
+ sizeof(rec
),
3535 p
, key
.dsize
+ dbuf
.dsize
) == -1) {
3543 * We have to allocate some space from the freelist, so this means we
3544 * have to lock it. Use the chance to purge all the DEAD records from
3545 * the hash chain under the freelist lock.
3548 if (tdb_lock(tdb
, -1, F_WRLCK
) == -1) {
3552 if ((tdb
->max_dead_records
!= 0)
3553 && (tdb_purge_dead(tdb
, hash
) == -1)) {
3554 tdb_unlock(tdb
, -1, F_WRLCK
);
3558 /* we have to allocate some space */
3559 rec_ptr
= tdb_allocate(tdb
, key
.dsize
+ dbuf
.dsize
, &rec
);
3561 tdb_unlock(tdb
, -1, F_WRLCK
);
3567 /* Read hash top into next ptr */
3568 if (tdb_ofs_read(tdb
, TDB_HASH_TOP(hash
), &rec
.next
) == -1)
3571 rec
.key_len
= key
.dsize
;
3572 rec
.data_len
= dbuf
.dsize
;
3573 rec
.full_hash
= hash
;
3574 rec
.magic
= TDB_MAGIC
;
3576 /* write out and point the top of the hash chain at it */
3577 if (tdb_rec_write(tdb
, rec_ptr
, &rec
) == -1
3578 || tdb
->methods
->tdb_write(tdb
, rec_ptr
+sizeof(rec
), p
, key
.dsize
+dbuf
.dsize
)==-1
3579 || tdb_ofs_write(tdb
, TDB_HASH_TOP(hash
), &rec_ptr
) == -1) {
3580 /* Need to tdb_unallocate() here */
3588 tdb_increment_seqnum(tdb
);
3592 tdb_unlock(tdb
, BUCKET(hash
), F_WRLCK
);
3597 /* Append to an entry. Create if not exist. */
3598 int tdb_append(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA new_dbuf
)
3604 /* find which hash bucket it is in */
3605 hash
= tdb
->hash_fn(&key
);
3606 if (tdb_lock(tdb
, BUCKET(hash
), F_WRLCK
) == -1)
3609 dbuf
= tdb_fetch(tdb
, key
);
3611 if (dbuf
.dptr
== NULL
) {
3612 dbuf
.dptr
= (unsigned char *)malloc(new_dbuf
.dsize
);
3614 unsigned char *new_dptr
= (unsigned char *)realloc(dbuf
.dptr
,
3615 dbuf
.dsize
+ new_dbuf
.dsize
);
3616 if (new_dptr
== NULL
) {
3619 dbuf
.dptr
= new_dptr
;
3622 if (dbuf
.dptr
== NULL
) {
3623 tdb
->ecode
= TDB_ERR_OOM
;
3627 memcpy(dbuf
.dptr
+ dbuf
.dsize
, new_dbuf
.dptr
, new_dbuf
.dsize
);
3628 dbuf
.dsize
+= new_dbuf
.dsize
;
3630 ret
= tdb_store(tdb
, key
, dbuf
, 0);
3633 tdb_unlock(tdb
, BUCKET(hash
), F_WRLCK
);
3634 SAFE_FREE(dbuf
.dptr
);
3640 return the name of the current tdb file
3641 useful for external logging functions
3643 const char *tdb_name(struct tdb_context
*tdb
)
3649 return the underlying file descriptor being used by tdb, or -1
3650 useful for external routines that want to check the device/inode
3653 int tdb_fd(struct tdb_context
*tdb
)
3659 return the current logging function
3660 useful for external tdb routines that wish to log tdb errors
3662 tdb_log_func
tdb_log_fn(struct tdb_context
*tdb
)
3664 return tdb
->log
.log_fn
;
3669 get the tdb sequence number. Only makes sense if the writers opened
3670 with TDB_SEQNUM set. Note that this sequence number will wrap quite
3671 quickly, so it should only be used for a 'has something changed'
3672 test, not for code that relies on the count of the number of changes
3673 made. If you want a counter then use a tdb record.
3675 The aim of this sequence number is to allow for a very lightweight
3676 test of a possible tdb change.
3678 int tdb_get_seqnum(struct tdb_context
*tdb
)
3682 tdb_ofs_read(tdb
, TDB_SEQNUM_OFS
, &seqnum
);
3686 int tdb_hash_size(struct tdb_context
*tdb
)
3688 return tdb
->header
.hash_size
;
3691 size_t tdb_map_size(struct tdb_context
*tdb
)
3693 return tdb
->map_size
;
3696 int tdb_get_flags(struct tdb_context
*tdb
)
3703 enable sequence number handling on an open tdb
3705 void tdb_enable_seqnum(struct tdb_context
*tdb
)
3707 tdb
->flags
|= TDB_SEQNUM
;
3712 /* all contexts, to ensure no double-opens (fcntl locks don't nest!) */
3713 static struct tdb_context
*tdbs
= NULL
;
3716 /* This is from a hash algorithm suggested by Rogier Wolff */
3717 static unsigned int default_tdb_hash(TDB_DATA
*key
)
3719 u32 value
; /* Used to compute the hash value. */
3720 u32 i
; /* Used to cycle through random values. */
3722 /* Set the initial value from the key size. */
3723 for (value
= 0, i
=0; i
< key
->dsize
; i
++)
3724 value
= value
* 256 + key
->dptr
[i
] + (value
>> 24) * 241;
3730 /* initialise a new database with a specified hash size */
3731 static int tdb_new_database(struct tdb_context
*tdb
, int hash_size
)
3733 struct tdb_header
*newdb
;
3736 /* We make it up in memory, then write it out if not internal */
3737 size
= sizeof(struct tdb_header
) + (hash_size
+1)*sizeof(tdb_off_t
);
3738 if (!(newdb
= (struct tdb_header
*)calloc(size
, 1)))
3739 return TDB_ERRCODE(TDB_ERR_OOM
, -1);
3741 /* Fill in the header */
3742 newdb
->version
= TDB_VERSION
;
3743 newdb
->hash_size
= hash_size
;
3744 if (tdb
->flags
& TDB_INTERNAL
) {
3745 tdb
->map_size
= size
;
3746 tdb
->map_ptr
= (char *)newdb
;
3747 memcpy(&tdb
->header
, newdb
, sizeof(tdb
->header
));
3748 /* Convert the `ondisk' version if asked. */
3752 if (lseek(tdb
->fd
, 0, SEEK_SET
) == -1)
3755 if (ftruncate(tdb
->fd
, 0) == -1)
3758 /* This creates an endian-converted header, as if read from disk */
3760 memcpy(&tdb
->header
, newdb
, sizeof(tdb
->header
));
3761 /* Don't endian-convert the magic food! */
3762 memcpy(newdb
->magic_food
, TDB_MAGIC_FOOD
, strlen(TDB_MAGIC_FOOD
)+1);
3763 if (write(tdb
->fd
, newdb
, size
) != size
) {
3776 static int tdb_already_open(dev_t device
,
3779 struct tdb_context
*i
;
3781 for (i
= tdbs
; i
; i
= i
->next
) {
3782 if (i
->device
== device
&& i
->inode
== ino
) {
3790 /* open the database, creating it if necessary
3792 The open_flags and mode are passed straight to the open call on the
3793 database file. A flags value of O_WRONLY is invalid. The hash size
3794 is advisory, use zero for a default value.
3796 Return is NULL on error, in which case errno is also set. Don't
3797 try to call tdb_error or tdb_errname, just do strerror(errno).
3799 @param name may be NULL for internal databases. */
3800 struct tdb_context
*tdb_open(const char *name
, int hash_size
, int tdb_flags
,
3801 int open_flags
, mode_t mode
)
3803 return tdb_open_ex(name
, hash_size
, tdb_flags
, open_flags
, mode
, NULL
, NULL
);
3806 /* a default logging function */
3807 static void null_log_fn(struct tdb_context
*tdb
, enum tdb_debug_level level
, const char *fmt
, ...) PRINTF_ATTRIBUTE(3, 4);
3808 static void null_log_fn(struct tdb_context
*tdb
, enum tdb_debug_level level
, const char *fmt
, ...)
3813 struct tdb_context
*tdb_open_ex(const char *name
, int hash_size
, int tdb_flags
,
3814 int open_flags
, mode_t mode
,
3815 const struct tdb_logging_context
*log_ctx
,
3816 tdb_hash_func hash_fn
)
3818 struct tdb_context
*tdb
;
3820 int rev
= 0, locked
= 0;
3824 if (!(tdb
= (struct tdb_context
*)calloc(1, sizeof *tdb
))) {
3825 /* Can't log this */
3832 tdb
->map_ptr
= NULL
;
3833 tdb
->flags
= tdb_flags
;
3834 tdb
->open_flags
= open_flags
;
3836 tdb
->log
= *log_ctx
;
3838 tdb
->log
.log_fn
= null_log_fn
;
3839 tdb
->log
.log_private
= NULL
;
3841 tdb
->hash_fn
= hash_fn
? hash_fn
: default_tdb_hash
;
3843 /* cache the page size */
3844 tdb
->page_size
= sysconf(_SC_PAGESIZE
);
3845 if (tdb
->page_size
<= 0) {
3846 tdb
->page_size
= 0x2000;
3849 if ((open_flags
& O_ACCMODE
) == O_WRONLY
) {
3850 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_open_ex: can't open tdb %s write-only\n",
3857 hash_size
= DEFAULT_HASH_SIZE
;
3858 if ((open_flags
& O_ACCMODE
) == O_RDONLY
) {
3860 /* read only databases don't do locking or clear if first */
3861 tdb
->flags
|= TDB_NOLOCK
;
3862 tdb
->flags
&= ~TDB_CLEAR_IF_FIRST
;
3865 /* internal databases don't mmap or lock, and start off cleared */
3866 if (tdb
->flags
& TDB_INTERNAL
) {
3867 tdb
->flags
|= (TDB_NOLOCK
| TDB_NOMMAP
);
3868 tdb
->flags
&= ~TDB_CLEAR_IF_FIRST
;
3869 if (tdb_new_database(tdb
, hash_size
) != 0) {
3870 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_open_ex: tdb_new_database failed!"));
3876 if ((tdb
->fd
= open(name
, open_flags
, mode
)) == -1) {
3877 TDB_LOG((tdb
, TDB_DEBUG_WARNING
, "tdb_open_ex: could not open file %s: %s\n",
3878 name
, strerror(errno
)));
3879 goto fail
; /* errno set by open(2) */
3882 /* ensure there is only one process initialising at once */
3883 if (tdb
->methods
->tdb_brlock(tdb
, GLOBAL_LOCK
, F_WRLCK
, F_SETLKW
, 0, 1) == -1) {
3884 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_open_ex: failed to get global lock on %s: %s\n",
3885 name
, strerror(errno
)));
3886 goto fail
; /* errno set by tdb_brlock */
3889 /* we need to zero database if we are the only one with it open */
3890 if ((tdb_flags
& TDB_CLEAR_IF_FIRST
) &&
3891 (locked
= (tdb
->methods
->tdb_brlock(tdb
, ACTIVE_LOCK
, F_WRLCK
, F_SETLK
, 0, 1) == 0))) {
3892 open_flags
|= O_CREAT
;
3893 if (ftruncate(tdb
->fd
, 0) == -1) {
3894 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_open_ex: "
3895 "failed to truncate %s: %s\n",
3896 name
, strerror(errno
)));
3897 goto fail
; /* errno set by ftruncate */
3901 if (read(tdb
->fd
, &tdb
->header
, sizeof(tdb
->header
)) != sizeof(tdb
->header
)
3902 || strcmp(tdb
->header
.magic_food
, TDB_MAGIC_FOOD
) != 0
3903 || (tdb
->header
.version
!= TDB_VERSION
3904 && !(rev
= (tdb
->header
.version
==TDB_BYTEREV(TDB_VERSION
))))) {
3905 /* its not a valid database - possibly initialise it */
3906 if (!(open_flags
& O_CREAT
) || tdb_new_database(tdb
, hash_size
) == -1) {
3907 errno
= EIO
; /* ie bad format or something */
3910 rev
= (tdb
->flags
& TDB_CONVERT
);
3912 vp
= (unsigned char *)&tdb
->header
.version
;
3913 vertest
= (((u32
)vp
[0]) << 24) | (((u32
)vp
[1]) << 16) |
3914 (((u32
)vp
[2]) << 8) | (u32
)vp
[3];
3915 tdb
->flags
|= (vertest
==TDB_VERSION
) ? TDB_BIGENDIAN
: 0;
3917 tdb
->flags
&= ~TDB_CONVERT
;
3919 tdb
->flags
|= TDB_CONVERT
;
3920 tdb_convert(&tdb
->header
, sizeof(tdb
->header
));
3922 if (fstat(tdb
->fd
, &st
) == -1)
3925 if (tdb
->header
.rwlocks
!= 0) {
3926 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_open_ex: spinlocks no longer supported\n"));
3930 /* Is it already in the open list? If so, fail. */
3931 if (tdb_already_open(st
.st_dev
, st
.st_ino
)) {
3932 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_open_ex: "
3933 "%s (%d,%d) is already open in this process\n",
3934 name
, (int)st
.st_dev
, (int)st
.st_ino
));
3939 if (!(tdb
->name
= (char *)strdup(name
))) {
3944 tdb
->map_size
= st
.st_size
;
3945 tdb
->device
= st
.st_dev
;
3946 tdb
->inode
= st
.st_ino
;
3947 tdb
->max_dead_records
= 0;
3950 if (tdb
->methods
->tdb_brlock(tdb
, ACTIVE_LOCK
, F_UNLCK
, F_SETLK
, 0, 1) == -1) {
3951 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_open_ex: "
3952 "failed to take ACTIVE_LOCK on %s: %s\n",
3953 name
, strerror(errno
)));
3959 /* We always need to do this if the CLEAR_IF_FIRST flag is set, even if
3960 we didn't get the initial exclusive lock as we need to let all other
3961 users know we're using it. */
3963 if (tdb_flags
& TDB_CLEAR_IF_FIRST
) {
3964 /* leave this lock in place to indicate it's in use */
3965 if (tdb
->methods
->tdb_brlock(tdb
, ACTIVE_LOCK
, F_RDLCK
, F_SETLKW
, 0, 1) == -1)
3969 /* if needed, run recovery */
3970 if (tdb_transaction_recover(tdb
) == -1) {
3975 /* Internal (memory-only) databases skip all the code above to
3976 * do with disk files, and resume here by releasing their
3977 * global lock and hooking into the active list. */
3978 if (tdb
->methods
->tdb_brlock(tdb
, GLOBAL_LOCK
, F_UNLCK
, F_SETLKW
, 0, 1) == -1)
3985 { int save_errno
= errno
;
3991 if (tdb
->flags
& TDB_INTERNAL
)
3992 SAFE_FREE(tdb
->map_ptr
);
3996 SAFE_FREE(tdb
->name
);
3998 if (close(tdb
->fd
) != 0)
3999 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_open_ex: failed to close tdb->fd on error!\n"));
4007 * Set the maximum number of dead records per hash chain
4010 void tdb_set_max_dead(struct tdb_context
*tdb
, int max_dead
)
4012 tdb
->max_dead_records
= max_dead
;
4018 * @returns -1 for error; 0 for success.
4020 int tdb_close(struct tdb_context
*tdb
)
4022 struct tdb_context
**i
;
4025 if (tdb
->transaction
) {
4026 tdb_transaction_cancel(tdb
);
4030 if (tdb
->flags
& TDB_INTERNAL
)
4031 SAFE_FREE(tdb
->map_ptr
);
4035 SAFE_FREE(tdb
->name
);
4037 ret
= close(tdb
->fd
);
4038 SAFE_FREE(tdb
->lockrecs
);
4040 /* Remove from contexts list */
4041 for (i
= &tdbs
; *i
; i
= &(*i
)->next
) {
4048 memset(tdb
, 0, sizeof(*tdb
));
4054 /* register a logging function */
4055 void tdb_set_logging_function(struct tdb_context
*tdb
,
4056 const struct tdb_logging_context
*log_ctx
)
4058 tdb
->log
= *log_ctx
;
4061 void *tdb_get_logging_private(struct tdb_context
*tdb
)
4063 return tdb
->log
.log_private
;
4066 /* reopen a tdb - this can be used after a fork to ensure that we have an independent
4067 seek pointer from our parent and to re-establish locks */
4068 int tdb_reopen(struct tdb_context
*tdb
)
4072 if (tdb
->flags
& TDB_INTERNAL
) {
4073 return 0; /* Nothing to do. */
4076 if (tdb
->num_locks
!= 0 || tdb
->global_lock
.count
) {
4077 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_reopen: reopen not allowed with locks held\n"));
4081 if (tdb
->transaction
!= 0) {
4082 TDB_LOG((tdb
, TDB_DEBUG_ERROR
, "tdb_reopen: reopen not allowed inside a transaction\n"));
4086 if (tdb_munmap(tdb
) != 0) {
4087 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_reopen: munmap failed (%s)\n", strerror(errno
)));
4090 if (close(tdb
->fd
) != 0)
4091 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_reopen: WARNING closing tdb->fd failed!\n"));
4092 tdb
->fd
= open(tdb
->name
, tdb
->open_flags
& ~(O_CREAT
|O_TRUNC
), 0);
4093 if (tdb
->fd
== -1) {
4094 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_reopen: open failed (%s)\n", strerror(errno
)));
4097 if ((tdb
->flags
& TDB_CLEAR_IF_FIRST
) &&
4098 (tdb
->methods
->tdb_brlock(tdb
, ACTIVE_LOCK
, F_RDLCK
, F_SETLKW
, 0, 1) == -1)) {
4099 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_reopen: failed to obtain active lock\n"));
4102 if (fstat(tdb
->fd
, &st
) != 0) {
4103 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_reopen: fstat failed (%s)\n", strerror(errno
)));
4106 if (st
.st_ino
!= tdb
->inode
|| st
.st_dev
!= tdb
->device
) {
4107 TDB_LOG((tdb
, TDB_DEBUG_FATAL
, "tdb_reopen: file dev/inode has changed!\n"));
4119 /* reopen all tdb's */
4120 int tdb_reopen_all(int parent_longlived
)
4122 struct tdb_context
*tdb
;
4124 for (tdb
=tdbs
; tdb
; tdb
= tdb
->next
) {
4126 * If the parent is longlived (ie. a
4127 * parent daemon architecture), we know
4128 * it will keep it's active lock on a
4129 * tdb opened with CLEAR_IF_FIRST. Thus
4130 * for child processes we don't have to
4131 * add an active lock. This is essential
4132 * to improve performance on systems that
4133 * keep POSIX locks as a non-scalable data
4134 * structure in the kernel.
4136 if (parent_longlived
) {
4137 /* Ensure no clear-if-first. */
4138 tdb
->flags
&= ~TDB_CLEAR_IF_FIRST
;
4141 if (tdb_reopen(tdb
) != 0)
4149 * Flush a database file from the page cache.
4151 int tdb_flush(struct tdb_context
*tdb
)
4154 return fsync(tdb
->fd
);