]> git.hungrycats.org Git - btrfs-progs/commitdiff
btrfs-progs: image: cleanup pending ordered works after an error
authorQu Wenruo <wqu@suse.com>
Sun, 25 Oct 2026 08:41:37 +0000 (19:11 +1030)
committerQu Wenruo <wqu@suse.com>
Mon, 26 Oct 2026 21:38:10 +0000 (08:08 +1030)
The function flush_pending() can queue at most ITEMS_PER_CLUSTER
async_works before submitting them to write_buffers().

But if we hit an critical error, e.g. read_tree_block() hit a bad tree
block, and there is already pending async_works, we rely on the last
flush_pending() call inside create_metadump() to do the cleanup.

However that final flush_pending() will hit the same corrupted tree
block and fail halfway, without really dispatch the existing async
works.

Fix the problem by:

- Avoid calling the final flush_pending() if we had already hit an error

- Manually cleanup any pending async works inside metadump_destroy()

Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
image/image-create.c

index d172fcd3897af01499bd65a62c0c9d08f8904468..1fd288d2b56667bbe79c1e0329cff7763b78df8f 100644 (file)
@@ -124,6 +124,14 @@ static void metadump_destroy(struct metadump_struct *md, int num_threads)
        pthread_cond_destroy(&md->cond);
        pthread_mutex_destroy(&md->mutex);
 
+       while (!list_empty(&md->ordered)) {
+               struct async_work *async = list_first_entry(&md->ordered,
+                               struct async_work, ordered);
+
+               list_del_init(&async->ordered);
+               free(async->buffer);
+               free(async);
+       }
        while ((n = rb_first(&md->name_tree))) {
                struct name *name;
 
@@ -828,13 +836,14 @@ int create_metadump(const char *input, FILE *out, int num_threads,
 
        ret = copy_space_cache(root, &metadump, &path);
 out:
-       ret = flush_pending(&metadump, 1);
-       if (ret) {
-               if (!err)
-                       err = ret;
-               error("failed to flush pending data: %d", ret);
+       if (!ret) {
+               ret = flush_pending(&metadump, 1);
+               if (ret)
+                       error("failed to flush pending data: %d", ret);
        }
 
+       if (ret && !err)
+               err = ret;
        metadump_destroy(&metadump, num_threads);
 
        btrfs_release_path(&path);