return;
}
- if (tdb_brlock(tdb, F_WRLCK, TDB_SEQNUM_OFS, 1,
- TDB_LOCK_WAIT|TDB_LOCK_PROBE) != 0) {
+ if (tdb_nest_lock(tdb, TDB_SEQNUM_OFS, F_WRLCK,
+ TDB_LOCK_WAIT|TDB_LOCK_PROBE) != 0) {
return;
}
tdb_increment_seqnum_nonblock(tdb);
- tdb_brunlock(tdb, F_WRLCK, TDB_SEQNUM_OFS, 1);
+ tdb_nest_unlock(tdb, TDB_SEQNUM_OFS, F_WRLCK, false);
}
static int tdb_key_compare(TDB_DATA key, TDB_DATA data, void *private_data)
return rec_ptr;
}
+static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key);
/* update an entry in place - this only works if the new data size
is <= the old data size and the key exists.
if (!(rec_ptr = tdb_find(tdb, key, hash, &rec)))
return -1;
+ /* it could be an exact duplicate of what is there - this is
+ * surprisingly common (eg. with a ldb re-index). */
+ if (rec.key_len == key.dsize &&
+ rec.data_len == dbuf.dsize &&
+ rec.full_hash == hash) {
+ TDB_DATA data = _tdb_fetch(tdb, key);
+ if (data.dsize == dbuf.dsize &&
+ memcmp(data.dptr, dbuf.dptr, data.dsize) == 0) {
+ if (data.dptr) {
+ free(data.dptr);
+ }
+ return 0;
+ }
+ if (data.dptr) {
+ free(data.dptr);
+ }
+ }
+
+
/* must be long enough key, data and tailer */
if (rec.rec_len < key.dsize + dbuf.dsize + sizeof(tdb_off_t)) {
tdb->ecode = TDB_SUCCESS; /* Not really an error */
/* find which hash bucket it is in */
hash = tdb->hash_fn(&key);
- if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
+ if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec)))
return tdb_null;
- }
+
ret.dptr = tdb_alloc_read(tdb, rec_ptr + sizeof(rec) + rec.key_len,
rec.data_len);
ret.dsize = rec.data_len;
* function. The parsing function is executed under the chain read lock, so it
* should be fast and should not block on other syscalls.
*
- * DONT CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS.
+ * DON'T CALL OTHER TDB CALLS FROM THE PARSER, THIS MIGHT LEAD TO SEGFAULTS.
*
* For mmapped tdb's that do not have a transaction open it points the parsing
* function directly at the mmap area, it avoids the malloc/memcpy in this
hash = tdb->hash_fn(&key);
if (!(rec_ptr = tdb_find_lock_hash(tdb,key,hash,F_RDLCK,&rec))) {
- tdb_trace_1rec_ret(tdb, "tdb_parse_record", key,
- -TDB_ERR_NOEXIST);
+ tdb_trace_1rec_ret(tdb, "tdb_parse_record", key, -1);
tdb->ecode = TDB_ERR_NOEXIST;
return 0;
}
if (tdb->read_only || tdb->traverse_read) return -1;
- if (tdb->traverse_write != 0 ||
+ if (((tdb->traverse_write != 0) && (!TDB_DEAD(rec))) ||
tdb_write_lock_record(tdb, rec_ptr) == -1) {
/* Someone traversing here: mark it as dead */
rec->magic = TDB_DEAD_MAGIC;
}
static int _tdb_store(struct tdb_context *tdb, TDB_DATA key,
- TDB_DATA dbuf, int flag, uint32_t hash)
+ TDB_DATA dbuf, int flag, uint32_t hash)
{
struct tdb_record rec;
tdb_off_t rec_ptr;
}
/* store an element in the database, replacing any existing element
- with the same key
+ with the same key
return 0 on success, -1 on failure
*/
if (tdb->read_only || tdb->traverse_read) {
tdb->ecode = TDB_ERR_RDONLY;
- tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag,
- -TDB_ERR_RDONLY);
+ tdb_trace_2rec_flag_ret(tdb, "tdb_store", key, dbuf, flag, -1);
return -1;
}
return ret;
}
-
/* Append to an entry. Create if not exist. */
int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf)
{
tdb_off_t seqnum=0;
tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
- tdb_trace_ret(tdb, "tdb_get_seqnum", seqnum);
return seqnum;
}
void tdb_add_flags(struct tdb_context *tdb, unsigned flags)
{
+ if ((flags & TDB_ALLOW_NESTING) &&
+ (flags & TDB_DISALLOW_NESTING)) {
+ tdb->ecode = TDB_ERR_NESTING;
+ TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_add_flags: "
+ "allow_nesting and disallow_nesting are not allowed together!"));
+ return;
+ }
+
+ if (flags & TDB_ALLOW_NESTING) {
+ tdb->flags &= ~TDB_DISALLOW_NESTING;
+ }
+ if (flags & TDB_DISALLOW_NESTING) {
+ tdb->flags &= ~TDB_ALLOW_NESTING;
+ }
+
tdb->flags |= flags;
}
void tdb_remove_flags(struct tdb_context *tdb, unsigned flags)
{
+ if ((flags & TDB_ALLOW_NESTING) &&
+ (flags & TDB_DISALLOW_NESTING)) {
+ tdb->ecode = TDB_ERR_NESTING;
+ TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_remove_flags: "
+ "allow_nesting and disallow_nesting are not allowed together!"));
+ return;
+ }
+
+ if (flags & TDB_ALLOW_NESTING) {
+ tdb->flags |= TDB_DISALLOW_NESTING;
+ }
+ if (flags & TDB_DISALLOW_NESTING) {
+ tdb->flags |= TDB_ALLOW_NESTING;
+ }
+
tdb->flags &= ~flags;
}
/*
wipe the entire database, deleting all records. This can be done
- very fast by using a global lock. The entire data portion of the
+ very fast by using a allrecord lock. The entire data portion of the
file becomes a single entry in the freelist.
This code carefully steps around the recovery area, leaving it alone
return -1;
}
-
struct traverse_state {
bool error;
struct tdb_context *dest_db;
/*
traverse function for repacking
*/
-static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private)
+static int repack_traverse(struct tdb_context *tdb, TDB_DATA key, TDB_DATA data, void *private_data)
{
- struct traverse_state *state = (struct traverse_state *)private;
+ struct traverse_state *state = (struct traverse_state *)private_data;
if (tdb_store(state->dest_db, key, data, TDB_INSERT) != 0) {
state->error = true;
return -1;
tdb_trace(tdb, "tdb_repack");
if (tdb_transaction_start(tdb) != 0) {
- TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_repack: Failed to start transaction\n"));
+ TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to start transaction\n"));
return -1;
}
tmp_db = tdb_open("tmpdb", tdb_hash_size(tdb), TDB_INTERNAL, O_RDWR|O_CREAT, 0);
if (tmp_db == NULL) {
- TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_repack: Failed to create tmp_db\n"));
+ TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to create tmp_db\n"));
tdb_transaction_cancel(tdb);
return -1;
}
state.dest_db = tmp_db;
if (tdb_traverse_read(tdb, repack_traverse, &state) == -1) {
- TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_repack: Failed to traverse copying out\n"));
+ TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to traverse copying out\n"));
tdb_transaction_cancel(tdb);
tdb_close(tmp_db);
return -1;
}
if (state.error) {
- TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_repack: Error during traversal\n"));
+ TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Error during traversal\n"));
tdb_transaction_cancel(tdb);
tdb_close(tmp_db);
return -1;
}
if (tdb_wipe_all(tdb) != 0) {
- TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_repack: Failed to wipe database\n"));
+ TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to wipe database\n"));
tdb_transaction_cancel(tdb);
tdb_close(tmp_db);
return -1;
state.dest_db = tdb;
if (tdb_traverse_read(tmp_db, repack_traverse, &state) == -1) {
- TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_repack: Failed to traverse copying back\n"));
+ TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to traverse copying back\n"));
tdb_transaction_cancel(tdb);
tdb_close(tmp_db);
return -1;
}
if (state.error) {
- TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_repack: Error during second traversal\n"));
+ TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Error during second traversal\n"));
tdb_transaction_cancel(tdb);
tdb_close(tmp_db);
return -1;
tdb_close(tmp_db);
if (tdb_transaction_commit(tdb) != 0) {
- TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_repack: Failed to commit\n"));
+ TDB_LOG((tdb, TDB_DEBUG_FATAL, __location__ " Failed to commit\n"));
return -1;
}
static void tdb_trace_start(struct tdb_context *tdb)
{
tdb_off_t seqnum=0;
- char msg[sizeof(tdb_off_t) * 4];
+ char msg[sizeof(tdb_off_t) * 4 + 1];
tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);
- sprintf(msg, "%u ", seqnum);
+ snprintf(msg, sizeof(msg), "%u ", seqnum);
tdb_trace_write(tdb, msg);
}
static void tdb_trace_end_ret(struct tdb_context *tdb, int ret)
{
- char msg[sizeof(ret) * 4];
- sprintf(msg, " = %i\n", ret);
+ char msg[sizeof(ret) * 4 + 4];
+ snprintf(msg, sizeof(msg), " = %i\n", ret);
tdb_trace_write(tdb, msg);
}
return;
}
+ /* snprintf here is purely cargo-cult programming. */
p = msg;
- p += sprintf(p, " %zu:", rec.dsize);
+ p += snprintf(p, sizeof(msg), " %zu:", rec.dsize);
for (i = 0; i < rec.dsize; i++)
- p += sprintf(p, "%02x", rec.dptr[i]);
+ p += snprintf(p, 2, "%02x", rec.dptr[i]);
tdb_trace_write(tdb, msg);
}
void tdb_trace_seqnum(struct tdb_context *tdb, uint32_t seqnum, const char *op)
{
- char msg[sizeof(tdb_off_t) * 4];
+ char msg[sizeof(tdb_off_t) * 4 + 1];
- sprintf(msg, "%u ", seqnum);
+ snprintf(msg, sizeof(msg), "%u ", seqnum);
tdb_trace_write(tdb, msg);
tdb_trace_write(tdb, op);
tdb_trace_end(tdb);
{
char msg[128];
- sprintf(msg, "%s %u %#x %#x", op, hash_size, tdb_flags, open_flags);
+ snprintf(msg, sizeof(msg),
+ "%s %u 0x%x 0x%x", op, hash_size, tdb_flags, open_flags);
tdb_trace_start(tdb);
tdb_trace_write(tdb, msg);
tdb_trace_end(tdb);
TDB_DATA rec1, TDB_DATA rec2, unsigned flag,
int ret)
{
- char msg[sizeof(ret) * 4];
+ char msg[1 + sizeof(ret) * 4];
- sprintf(msg, " %#x", flag);
+ snprintf(msg, sizeof(msg), " %#x", flag);
tdb_trace_start(tdb);
tdb_trace_write(tdb, op);
tdb_trace_record(tdb, rec1);