X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb%2Ftdb.c;h=7317a3aa521554c898d12a5e5b8f57c7dafea36f;hp=a09d567c45dc4b74391f6dabdff1cbee9e90757e;hb=d9cbd7d4454ae35e4e2f6d18a9469bf26948e4b9;hpb=e55e729c188bc483d1986145014f538389e9d368 diff --git a/ccan/tdb/tdb.c b/ccan/tdb/tdb.c index a09d567c..7317a3aa 100644 --- a/ccan/tdb/tdb.c +++ b/ccan/tdb/tdb.c @@ -59,14 +59,14 @@ static void tdb_increment_seqnum(struct tdb_context *tdb) 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) @@ -190,9 +190,9 @@ static TDB_DATA _tdb_fetch(struct tdb_context *tdb, TDB_DATA key) /* 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; @@ -213,7 +213,7 @@ TDB_DATA tdb_fetch(struct tdb_context *tdb, TDB_DATA key) * 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 @@ -238,8 +238,7 @@ int tdb_parse_record(struct tdb_context *tdb, TDB_DATA key, 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; } @@ -287,7 +286,7 @@ int tdb_do_delete(struct tdb_context *tdb, tdb_off_t rec_ptr, struct tdb_record 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; @@ -468,7 +467,7 @@ static tdb_off_t tdb_find_dead(struct tdb_context *tdb, uint32_t hash, } 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; @@ -594,7 +593,7 @@ static int _tdb_store(struct tdb_context *tdb, TDB_DATA key, } /* 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 */ @@ -605,8 +604,7 @@ int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag) 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; } @@ -621,7 +619,6 @@ int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag) return ret; } - /* Append to an entry. Create if not exist. */ int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf) { @@ -714,7 +711,6 @@ int tdb_get_seqnum(struct tdb_context *tdb) tdb_off_t seqnum=0; tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum); - tdb_trace_ret(tdb, "tdb_get_seqnum", seqnum); return seqnum; } @@ -735,11 +731,41 @@ int tdb_get_flags(struct tdb_context *tdb) 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; } @@ -779,7 +805,7 @@ static int tdb_free_region(struct tdb_context *tdb, tdb_off_t offset, ssize_t le /* 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 @@ -870,7 +896,6 @@ failed: return -1; } - struct traverse_state { bool error; struct tdb_context *dest_db; @@ -879,9 +904,9 @@ struct traverse_state { /* 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; @@ -900,13 +925,13 @@ int tdb_repack(struct tdb_context *tdb) 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; } @@ -915,21 +940,21 @@ int tdb_repack(struct tdb_context *tdb) 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; @@ -939,14 +964,14 @@ int tdb_repack(struct tdb_context *tdb) 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; @@ -955,7 +980,7 @@ int tdb_repack(struct tdb_context *tdb) 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; } @@ -974,10 +999,10 @@ static void tdb_trace_write(struct tdb_context *tdb, const char *str) 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); } @@ -988,8 +1013,8 @@ static void tdb_trace_end(struct tdb_context *tdb) 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); } @@ -1004,10 +1029,11 @@ static void tdb_trace_record(struct tdb_context *tdb, TDB_DATA rec) 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); } @@ -1021,9 +1047,9 @@ void tdb_trace(struct tdb_context *tdb, const char *op) 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); @@ -1034,7 +1060,8 @@ void tdb_trace_open(struct tdb_context *tdb, const char *op, { 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); @@ -1089,9 +1116,9 @@ void tdb_trace_2rec_flag_ret(struct tdb_context *tdb, const char *op, 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);