tdb: fix tdb_check() on read-only TDBs to actually work.
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 13 Sep 2010 10:28:23 +0000 (19:58 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 7 Oct 2010 04:35:56 +0000 (15:05 +1030)
Commit bc1c82ea137 "Fix tdb_check() to work with read-only tdb databases."
claimed to do this, but tdb_lockall_read() fails on read-only databases.

Also make sure we can still do tdb_check() inside a transaction (weird,
but we previously allowed it so don't break the API).

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
lib/tdb/common/check.c

index 3be8a0c48b3b130b5687b718b534c8056113cb19..3ac2eb51053943b17ae0b440a7dcda08ce128f39 100644 (file)
@@ -326,9 +326,17 @@ int tdb_check(struct tdb_context *tdb,
        struct tdb_record rec;
        bool found_recovery = false;
        tdb_len_t dead;
-
-       if (tdb_lockall_read(tdb) == -1)
-               return -1;
+       bool locked;
+
+       /* Read-only databases use no locking at all: it's best-effort.
+        * We may have a write lock already, so skip that case too. */
+       if (tdb->read_only || tdb->allrecord_lock.count != 0) {
+               locked = false;
+       } else {
+               if (tdb_lockall_read(tdb) == -1)
+                       return -1;
+               locked = true;
+       }
 
        /* Make sure we know true size of the underlying file. */
        tdb->methods->tdb_oob(tdb, tdb->map_size + 1, 1);
@@ -443,12 +451,16 @@ int tdb_check(struct tdb_context *tdb,
        }
 
        free(hashes);
-       tdb_unlockall_read(tdb);
+       if (locked) {
+               tdb_unlockall_read(tdb);
+       }
        return 0;
 
 free:
        free(hashes);
 unlock:
-       tdb_unlockall_read(tdb);
+       if (locked) {
+               tdb_unlockall_read(tdb);
+       }
        return -1;
 }