]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/tdb1_transaction.c
tdb2: don't cancel transactions on lock failures in tdb1 backend.
[ccan] / ccan / tdb2 / tdb1_transaction.c
index 165fd7f972f5b74dd0d1de0271c97ec9e502ebbe..f0623025f95268f12a89febf9246475946e3fd8a 100644 (file)
     usual. This allows for smooth crash recovery with no administrator
     intervention.
 
-  - if TDB1_NOSYNC is passed to flags in tdb1_open then transactions are
+  - if TDB_NOSYNC is passed to flags in tdb1_open then transactions are
     still available, but no transaction recovery area is used and no
     fsync/msync calls are made.
 
-  - if TDB1_ALLOW_NESTING is passed to flags in tdb open, or added using
+  - if TDB_ALLOW_NESTING is passed to flags in tdb open, or added using
     tdb1_add_flags() transaction nesting is enabled.
-    It resets the TDB1_DISALLOW_NESTING flag, as both cannot be used together.
-    The default is that transaction nesting is allowed.
-    Note: this default may change in future versions of tdb.
+    The default is that transaction nesting is NOT allowed.
 
     Beware. when transactions are nested a transaction successfully
     completed with tdb1_transaction_commit() can be silently unrolled later.
-
-  - if TDB1_DISALLOW_NESTING is passed to flags in tdb open, or added using
-    tdb1_add_flags() transaction nesting is disabled.
-    It resets the TDB1_ALLOW_NESTING flag, as both cannot be used together.
-    An attempt create a nested transaction will fail with TDB_ERR_EINVAL.
-    The default is that transaction nesting is allowed.
-    Note: this default may change in future versions of tdb.
 */
 
 
@@ -387,7 +378,7 @@ static void transaction1_next_hash_chain(struct tdb1_context *tdb, uint32_t *cha
 */
 static int transaction1_oob(struct tdb1_context *tdb, tdb1_off_t len, int probe)
 {
-       if (len <= tdb->map_size) {
+       if (len <= tdb->file->map_size) {
                return 0;
        }
        tdb->last_error = TDB_ERR_IO;
@@ -427,7 +418,7 @@ static const struct tdb1_methods transaction1_methods = {
 static int _tdb1_transaction_start(struct tdb1_context *tdb)
 {
        /* some sanity checks */
-       if (tdb->read_only || (tdb->flags & TDB1_INTERNAL) || tdb->traverse_read) {
+       if (tdb->read_only || (tdb->flags & TDB_INTERNAL) || tdb->traverse_read) {
                tdb->last_error = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
                                        "tdb1_transaction_start: cannot start a"
                                        " transaction on a read-only or"
@@ -437,7 +428,7 @@ static int _tdb1_transaction_start(struct tdb1_context *tdb)
 
        /* cope with nested tdb1_transaction_start() calls */
        if (tdb->transaction != NULL) {
-               if (!(tdb->flags & TDB1_ALLOW_NESTING)) {
+               if (!(tdb->flags & TDB_ALLOW_NESTING)) {
                        tdb->last_error = TDB_ERR_EINVAL;
                        return -1;
                }
@@ -478,7 +469,7 @@ static int _tdb1_transaction_start(struct tdb1_context *tdb)
        /* get the transaction write lock. This is a blocking lock. As
           discussed with Volker, there are a number of ways we could
           make this async, which we will probably do in the future */
-       if (tdb1_transaction_lock(tdb, F_WRLCK, TDB1_LOCK_WAIT) == -1) {
+       if (tdb1_transaction_lock(tdb, F_WRLCK, TDB_LOCK_WAIT) == -1) {
                SAFE_FREE(tdb->transaction->blocks);
                SAFE_FREE(tdb->transaction);
                return -1;
@@ -486,9 +477,12 @@ static int _tdb1_transaction_start(struct tdb1_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 (tdb1_allrecord_lock(tdb, F_RDLCK, TDB1_LOCK_WAIT, true) == -1) {
-               tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
-                          "tdb1_transaction_start: failed to get hash locks");
+       if (tdb1_allrecord_lock(tdb, F_RDLCK, TDB_LOCK_WAIT, true) == -1) {
+               if (errno != EAGAIN && errno != EINTR) {
+                       tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
+                                  "tdb1_transaction_start:"
+                                  " failed to get hash locks");
+               }
                goto fail_allrecord_lock;
        }
 
@@ -509,8 +503,8 @@ static int _tdb1_transaction_start(struct tdb1_context *tdb)
 
        /* make sure we know about any file expansions already done by
           anyone else */
-       tdb->methods->tdb1_oob(tdb, tdb->map_size + 1, 1);
-       tdb->transaction->old_map_size = tdb->map_size;
+       tdb->methods->tdb1_oob(tdb, tdb->file->map_size + 1, 1);
+       tdb->transaction->old_map_size = tdb->file->map_size;
 
        /* finally hook the io methods, replacing them with
           transaction specific methods */
@@ -539,23 +533,23 @@ int tdb1_transaction_start(struct tdb1_context *tdb)
 */
 static int transaction1_sync(struct tdb1_context *tdb, tdb1_off_t offset, tdb1_len_t length)
 {
-       if (tdb->flags & TDB1_NOSYNC) {
+       if (tdb->flags & TDB_NOSYNC) {
                return 0;
        }
 
 #if HAVE_FDATASYNC
-       if (fdatasync(tdb->fd) != 0) {
+       if (fdatasync(tdb->file->fd) != 0) {
 #else
-       if (fsync(tdb->fd) != 0) {
+       if (fsync(tdb->file->fd) != 0) {
 #endif
                tdb->last_error = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
                                        "tdb1_transaction: fsync failed");
                return -1;
        }
 #if HAVE_MMAP
-       if (tdb->map_ptr) {
+       if (tdb->file->map_ptr) {
                tdb1_off_t moffset = offset & ~(tdb->page_size-1);
-               if (msync(moffset + (char *)tdb->map_ptr,
+               if (msync(moffset + (char *)tdb->file->map_ptr,
                          length + (offset - moffset), MS_SYNC) != 0) {
                        tdb->last_error = tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
                                                "tdb1_transaction:"
@@ -586,7 +580,7 @@ static int _tdb1_transaction_cancel(struct tdb1_context *tdb)
                return 0;
        }
 
-       tdb->map_size = tdb->transaction->old_map_size;
+       tdb->file->map_size = tdb->transaction->old_map_size;
 
        /* free all the transaction blocks */
        for (i=0;i<tdb->transaction->num_blocks;i++) {
@@ -733,11 +727,11 @@ static int tdb1_recovery_allocate(struct tdb1_context *tdb,
 
        /* round up to a multiple of page size */
        *recovery_max_size = TDB1_ALIGN(sizeof(rec) + *recovery_size, tdb->page_size) - sizeof(rec);
-       *recovery_offset = tdb->map_size;
+       *recovery_offset = tdb->file->map_size;
        recovery_head = *recovery_offset;
 
        if (methods->tdb1_expand_file(tdb, tdb->transaction->old_map_size,
-                                    (tdb->map_size - tdb->transaction->old_map_size) +
+                                    (tdb->file->map_size - tdb->transaction->old_map_size) +
                                     sizeof(rec) + *recovery_max_size) == -1) {
                tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
                           "tdb1_recovery_allocate:"
@@ -746,11 +740,11 @@ static int tdb1_recovery_allocate(struct tdb1_context *tdb,
        }
 
        /* remap the file (if using mmap) */
-       methods->tdb1_oob(tdb, tdb->map_size + 1, 1);
+       methods->tdb1_oob(tdb, tdb->file->map_size + 1, 1);
 
        /* we have to reset the old map size so that we don't try to expand the file
           again in the transaction commit, which would destroy the recovery area */
-       tdb->transaction->old_map_size = tdb->map_size;
+       tdb->transaction->old_map_size = tdb->file->map_size;
 
        /* write the recovery header offset and sync - we can sync without a race here
           as the magic ptr in the recovery record has not been set */
@@ -964,30 +958,31 @@ static int _tdb1_transaction_prepare_commit(struct tdb1_context *tdb)
 
        /* upgrade the main transaction lock region to a write lock */
        if (tdb1_allrecord_upgrade(tdb) == -1) {
-               tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
-                          "tdb1_transaction_prepare_commit:"
-                          " failed to upgrade hash locks");
-               _tdb1_transaction_cancel(tdb);
+               if (errno != EAGAIN && errno != EINTR) {
+                       tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
+                                  "tdb1_transaction_prepare_commit:"
+                                  " failed to upgrade hash locks");
+               }
                return -1;
        }
 
        /* get the open lock - this prevents new users attaching to the database
           during the commit */
-       if (tdb1_nest_lock(tdb, TDB1_OPEN_LOCK, F_WRLCK, TDB1_LOCK_WAIT) == -1) {
-               tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
-                          "tdb1_transaction_prepare_commit:"
-                          " failed to get open lock");
-               _tdb1_transaction_cancel(tdb);
+       if (tdb1_nest_lock(tdb, TDB1_OPEN_LOCK, F_WRLCK, TDB_LOCK_WAIT) == -1) {
+               if (errno != EAGAIN && errno != EINTR) {
+                       tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
+                                  "tdb1_transaction_prepare_commit:"
+                                  " failed to get open lock");
+               }
                return -1;
        }
 
-       if (!(tdb->flags & TDB1_NOSYNC)) {
+       if (!(tdb->flags & TDB_NOSYNC)) {
                /* write the recovery data to the end of the file */
                if (transaction1_setup_recovery(tdb, &tdb->transaction->magic_offset) == -1) {
                        tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
                                   "tdb1_transaction_prepare_commit:"
                                   " failed to setup recovery data");
-                       _tdb1_transaction_cancel(tdb);
                        return -1;
                }
        }
@@ -995,18 +990,17 @@ static int _tdb1_transaction_prepare_commit(struct tdb1_context *tdb)
        tdb->transaction->prepared = true;
 
        /* expand the file to the new size if needed */
-       if (tdb->map_size != tdb->transaction->old_map_size) {
+       if (tdb->file->map_size != tdb->transaction->old_map_size) {
                if (methods->tdb1_expand_file(tdb, tdb->transaction->old_map_size,
-                                            tdb->map_size -
+                                            tdb->file->map_size -
                                             tdb->transaction->old_map_size) == -1) {
                        tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
                                   "tdb1_transaction_prepare_commit:"
                                   " expansion failed");
-                       _tdb1_transaction_cancel(tdb);
                        return -1;
                }
-               tdb->map_size = tdb->transaction->old_map_size;
-               methods->tdb1_oob(tdb, tdb->map_size + 1, 1);
+               tdb->file->map_size = tdb->transaction->old_map_size;
+               methods->tdb1_oob(tdb, tdb->file->map_size + 1, 1);
        }
 
        /* Keep the open lock until the actual commit */
@@ -1082,8 +1076,10 @@ int tdb1_transaction_commit(struct tdb1_context *tdb)
 
        if (!tdb->transaction->prepared) {
                int ret = _tdb1_transaction_prepare_commit(tdb);
-               if (ret)
+               if (ret) {
+                       _tdb1_transaction_cancel(tdb);
                        return ret;
+               }
        }
 
        methods = tdb->transaction->io_methods;
@@ -1132,7 +1128,7 @@ int tdb1_transaction_commit(struct tdb1_context *tdb)
        tdb->transaction->num_blocks = 0;
 
        /* ensure the new data is on disk */
-       if (transaction1_sync(tdb, 0, tdb->map_size) == -1) {
+       if (transaction1_sync(tdb, 0, tdb->file->map_size) == -1) {
                return -1;
        }
 
@@ -1251,7 +1247,7 @@ int tdb1_transaction_recover(struct tdb1_context *tdb)
 
        free(data);
 
-       if (transaction1_sync(tdb, 0, tdb->map_size) == -1) {
+       if (transaction1_sync(tdb, 0, tdb->file->map_size) == -1) {
                tdb_logerr(tdb, tdb->last_error, TDB_LOG_ERROR,
                           "tdb1_transaction_recover: failed to sync recovery");
                return -1;