X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftdb2%2Flock.c;h=a71c95f6e5cc9479288deae43581361019361c38;hp=7d4311c8e568b8125d72d2e91ec3dd0c8b08d4b9;hb=926996e88c32445c874ff9c4f47f159db6b45995;hpb=ceb2f3eacbad917ee990afbc3dd800bfb0607887 diff --git a/ccan/tdb2/lock.c b/ccan/tdb2/lock.c index 7d4311c8..a71c95f6 100644 --- a/ccan/tdb2/lock.c +++ b/ccan/tdb2/lock.c @@ -30,7 +30,7 @@ #include /* If we were threaded, we could wait for unlock, but we're not, so fail. */ -static enum TDB_ERROR owner_conflict(struct tdb_context *tdb, const char *call) +enum TDB_ERROR owner_conflict(struct tdb_context *tdb, const char *call) { return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR, "%s: lock owned by another tdb in this process.", @@ -38,8 +38,7 @@ static enum TDB_ERROR owner_conflict(struct tdb_context *tdb, const char *call) } /* If we fork, we no longer really own locks. */ -static bool check_lock_pid(struct tdb_context *tdb, - const char *call, bool log) +bool check_lock_pid(struct tdb_context *tdb, const char *call, bool log) { /* No locks? No problem! */ if (tdb->file->allrecord_lock.count == 0 @@ -416,7 +415,7 @@ enum TDB_ERROR tdb_nest_lock(struct tdb_context *tdb, tdb_brunlock(tdb, ltype, offset, 1); if (berr < 0) - return berr; + return TDB_OFF_TO_ERR(berr); ecode = tdb_lock_and_recover(tdb); if (ecode == TDB_SUCCESS) { ecode = tdb_brlock(tdb, ltype, offset, 1, @@ -614,7 +613,7 @@ again: tdb_allrecord_unlock(tdb, ltype); if (berr < 0) - return berr; + return TDB_OFF_TO_ERR(berr); ecode = tdb_lock_and_recover(tdb); if (ecode != TDB_SUCCESS) { return ecode; @@ -787,6 +786,11 @@ enum TDB_ERROR tdb_unlock_hashes(struct tdb_context *tdb, return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR, "tdb_unlock_hashes RO allrecord!"); } + if (tdb->file->allrecord_lock.owner != tdb) { + return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR, + "tdb_unlock_hashes:" + " not locked by us!"); + } return TDB_SUCCESS; } @@ -817,6 +821,10 @@ enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off, if (!check_lock_pid(tdb, "tdb_lock_free_bucket", true)) return TDB_ERR_LOCK; + if (tdb->file->allrecord_lock.owner != tdb) { + return owner_conflict(tdb, "tdb_lock_free_bucket"); + } + if (tdb->file->allrecord_lock.ltype == F_WRLCK) return 0; return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,