X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb%2Ftransaction.c;h=a6cffb35d5e7e83943c027b70070b05cf2ae1802;hp=3d34ac84f57d877982a86987732a26ed5fa2be87;hb=88541fe7eb0542150669312675ddc00e9684de66;hpb=acf1a21e440b2908df0ce57c48dab1aca9e23cad diff --git a/ccan/tdb/transaction.c b/ccan/tdb/transaction.c index 3d34ac84..a6cffb35 100644 --- a/ccan/tdb/transaction.c +++ b/ccan/tdb/transaction.c @@ -135,9 +135,6 @@ struct tdb_transaction { bool prepared; tdb_off_t magic_offset; - /* set when the OPEN_LOCK has been taken */ - bool open_lock_taken; - /* old file size before transaction */ tdb_len_t old_map_size; @@ -418,6 +415,9 @@ static int transaction_brlock(struct tdb_context *tdb, int rw_type, tdb_off_t offset, size_t len, enum tdb_lock_flags flags) { + /* FIXME: We actually grab the open lock during a transaction. */ + if (offset == OPEN_LOCK) + return tdb_brlock(tdb, rw_type, offset, len, flags); return 0; } @@ -504,33 +504,12 @@ int _tdb_transaction_cancel(struct tdb_context *tdb, int ltype) } } - if (tdb->transaction->open_lock_taken) { - tdb_brunlock(tdb, F_WRLCK, OPEN_LOCK, 1); - tdb->transaction->open_lock_taken = false; - } - - /* remove any global lock created during the transaction */ - if (tdb->allrecord_lock.count != 0) { - tdb_brunlock(tdb, tdb->allrecord_lock.ltype, - FREELIST_TOP, 4*tdb->header.hash_size); - tdb->allrecord_lock.count = 0; - } - - /* remove any locks created during the transaction */ - if (tdb->num_locks != 0) { - for (i=0;inum_lockrecs;i++) { - tdb_brunlock(tdb, tdb->lockrecs[i].ltype, - tdb->lockrecs[i].off, 1); - } - tdb->num_locks = 0; - tdb->num_lockrecs = 0; - SAFE_FREE(tdb->lockrecs); - } + /* This also removes the OPEN_LOCK, if we have it. */ + tdb_release_extra_locks(tdb); /* restore the normal io methods */ tdb->methods = tdb->transaction->io_methods; - tdb_brunlock(tdb, ltype, FREELIST_TOP, 0); tdb_transaction_unlock(tdb, F_WRLCK); SAFE_FREE(tdb->transaction->hash_heads); SAFE_FREE(tdb->transaction); @@ -564,7 +543,7 @@ int tdb_transaction_start(struct tdb_context *tdb) return 0; } - if (tdb->num_locks != 0 || tdb->allrecord_lock.count) { + if (tdb_have_extra_locks(tdb)) { /* the caller must not have any locks when starting a transaction as otherwise we'll be screwed by lack of nested locks in posix */ @@ -603,10 +582,9 @@ int tdb_transaction_start(struct tdb_context *tdb) /* get a read lock from the freelist to the end of file. This is upgraded to a write lock during the commit */ - if (tdb_brlock(tdb, F_RDLCK, FREELIST_TOP, 0, TDB_LOCK_WAIT) == -1) { + if (tdb_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, true) == -1) { TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_start: failed to get hash locks\n")); - tdb->ecode = TDB_ERR_LOCK; - goto fail; + goto fail_allrecord_lock; } /* setup a copy of the hash table heads so the hash scan in @@ -639,7 +617,8 @@ int tdb_transaction_start(struct tdb_context *tdb) return 0; fail: - tdb_brunlock(tdb, F_RDLCK, FREELIST_TOP, 0); + tdb_allrecord_unlock(tdb, F_RDLCK, false); +fail_allrecord_lock: tdb_transaction_unlock(tdb, F_WRLCK); SAFE_FREE(tdb->transaction->blocks); SAFE_FREE(tdb->transaction->hash_heads); @@ -944,7 +923,7 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb) /* if there are any locks pending then the caller has not nested their locks properly, so fail the transaction */ - if (tdb->num_locks || tdb->allrecord_lock.count) { + if (tdb_have_extra_locks(tdb)) { tdb->ecode = TDB_ERR_LOCK; TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: locks pending on commit\n")); _tdb_transaction_cancel(tdb, F_RDLCK); @@ -952,24 +931,20 @@ static int _tdb_transaction_prepare_commit(struct tdb_context *tdb) } /* upgrade the main transaction lock region to a write lock */ - if (tdb_brlock_upgrade(tdb, FREELIST_TOP, 0) == -1) { + if (tdb_allrecord_upgrade(tdb) == -1) { TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: failed to upgrade hash locks\n")); - tdb->ecode = TDB_ERR_LOCK; _tdb_transaction_cancel(tdb, F_RDLCK); return -1; } /* get the open lock - this prevents new users attaching to the database during the commit */ - if (tdb_brlock(tdb, F_WRLCK, OPEN_LOCK, 1, TDB_LOCK_WAIT) == -1) { + if (tdb_nest_lock(tdb, OPEN_LOCK, F_WRLCK, TDB_LOCK_WAIT) == -1) { TDB_LOG((tdb, TDB_DEBUG_ERROR, "tdb_transaction_prepare_commit: failed to get open lock\n")); - tdb->ecode = TDB_ERR_LOCK; _tdb_transaction_cancel(tdb, F_WRLCK); return -1; } - tdb->transaction->open_lock_taken = true; - if (!(tdb->flags & TDB_NOSYNC)) { /* write the recovery data to the end of the file */ if (transaction_setup_recovery(tdb, &tdb->transaction->magic_offset) == -1) {