]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/tdb1_lock.c
tdb2: log an error when calling tdb_store() on read-only TDB in tdb1 backend.
[ccan] / ccan / tdb2 / tdb1_lock.c
index 7cc17d7a7e1777a3384c92ef442b90af39630e1d..495e1d1bf6f2e6deec85dcac84458a9c6f189ba9 100644 (file)
@@ -162,13 +162,21 @@ static int tdb1_lock_list(struct tdb_context *tdb, int list, int ltype,
                check = !have_data_locks(tdb);
                ret = tdb1_nest_lock(tdb, lock_offset(list), ltype, waitflag);
 
-               if (ret == 0 && check && tdb1_needs_recovery(tdb)) {
-                       tdb1_nest_unlock(tdb, lock_offset(list), ltype);
+               if (ret == 0 && check) {
+                       tdb_bool_err berr = tdb1_needs_recovery(tdb);
 
-                       if (tdb1_lock_and_recover(tdb) == -1) {
+                       if (berr < 0) {
                                return -1;
                        }
-                       return tdb1_lock_list(tdb, list, ltype, waitflag);
+                       if (berr == true) {
+                               tdb1_nest_unlock(tdb, lock_offset(list), ltype);
+
+                               if (tdb1_lock_and_recover(tdb) == -1) {
+                                       return -1;
+                               }
+                               return tdb1_lock_list(tdb, list, ltype,
+                                                     waitflag);
+                       }
                }
        }
        return ret;
@@ -249,6 +257,7 @@ int tdb1_allrecord_lock(struct tdb_context *tdb, int ltype,
                       enum tdb_lock_flags flags, bool upgradable)
 {
        enum TDB_ERROR ecode;
+       tdb_bool_err berr;
 
        /* tdb_lock_gradual() doesn't know about tdb->tdb1.traverse_read. */
        if (tdb->tdb1.traverse_read && !(tdb->flags & TDB_NOLOCK)) {
@@ -306,14 +315,19 @@ int tdb1_allrecord_lock(struct tdb_context *tdb, int ltype,
        }
 
        /* FIXME: Temporary cast. */
-       tdb->file->allrecord_lock.owner = (void *)(struct tdb1_context *)tdb;
+       tdb->file->allrecord_lock.owner = (void *)(struct tdb_context *)tdb;
        tdb->file->allrecord_lock.count = 1;
        /* If it's upgradable, it's actually exclusive so we can treat
         * it as a write lock. */
        tdb->file->allrecord_lock.ltype = upgradable ? F_WRLCK : ltype;
        tdb->file->allrecord_lock.off = upgradable;
 
-       if (tdb1_needs_recovery(tdb)) {
+       berr = tdb1_needs_recovery(tdb);
+       if (berr < 0) {
+               return -1;
+       }
+
+       if (berr == true) {
                tdb1_allrecord_unlock(tdb, ltype);
                if (tdb1_lock_and_recover(tdb) == -1) {
                        return -1;
@@ -364,30 +378,6 @@ int tdb1_allrecord_unlock(struct tdb_context *tdb, int ltype)
        return 0;
 }
 
-/* lock entire database with write lock */
-int tdb1_lockall(struct tdb_context *tdb)
-{
-       return tdb1_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
-}
-
-/* unlock entire database with write lock */
-int tdb1_unlockall(struct tdb_context *tdb)
-{
-       return tdb1_allrecord_unlock(tdb, F_WRLCK);
-}
-
-/* lock entire database with read lock */
-int tdb1_lockall_read(struct tdb_context *tdb)
-{
-       return tdb1_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, false);
-}
-
-/* unlock entire database with read lock */
-int tdb1_unlockall_read(struct tdb_context *tdb)
-{
-       return tdb1_allrecord_unlock(tdb, F_RDLCK);
-}
-
 /* lock/unlock one hash chain. This is meant to be used to reduce
    contention - it cannot guarantee how many records will be locked */
 int tdb1_chainlock(struct tdb_context *tdb, TDB_DATA key)