]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
btrfs: scrub: factor out parity scrub code into a helper
authorQu Wenruo <wqu@suse.com>
Thu, 6 Nov 2025 09:32:15 +0000 (20:02 +1030)
committerDavid Sterba <dsterba@suse.com>
Mon, 24 Nov 2025 21:42:22 +0000 (22:42 +0100)
The function scrub_raid56_parity_stripe() is handling the parity stripe
by the following steps:

- Scrub each data stripes
  And make sure everything is fine in each data stripe

- Cache the data stripe into the raid bio

- Use the cached raid bio to scrub the target parity stripe

Extract the last two steps into a new helper,
scrub_raid56_cached_parity(), as a cleanup and make the error handling
more straightforward.

With the following minor cleanups:

- Use on-stack bio structure
  The bio is always empty thus we do not need any bio vector nor the
  block device. Thus there is no need to allocate a bio, the on-stack
  one is more than enough to cut it.

- Remove the unnecessary btrfs_put_bioc() call if btrfs_map_block()
  failed
  If btrfs_map_block() is failed, @bioc_ret will not be touched thus
  there is no need to call btrfs_put_bioc() in this case.

- Use a proper out: tag to do the cleanup
  Now the error cleanup is much shorter and simpler, just
  btrfs_bio_counter_dec() and bio_uninit().

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/scrub.c

index ad7084c323d08e80047b08f7da0b544bcd11a3aa..3029ef683fb990c8323a5bb0d2258c1cba432756 100644 (file)
@@ -2113,24 +2113,69 @@ static int should_cancel_scrub(const struct scrub_ctx *sctx)
        return 0;
 }
 
+static int scrub_raid56_cached_parity(struct scrub_ctx *sctx,
+                                     struct btrfs_device *scrub_dev,
+                                     struct btrfs_chunk_map *map,
+                                     u64 full_stripe_start,
+                                     unsigned long *extent_bitmap)
+{
+       DECLARE_COMPLETION_ONSTACK(io_done);
+       struct btrfs_fs_info *fs_info = sctx->fs_info;
+       struct btrfs_io_context *bioc = NULL;
+       struct btrfs_raid_bio *rbio;
+       struct bio bio;
+       const int data_stripes = nr_data_stripes(map);
+       u64 length = btrfs_stripe_nr_to_offset(data_stripes);
+       int ret;
+
+       bio_init(&bio, NULL, NULL, 0, REQ_OP_READ);
+       bio.bi_iter.bi_sector = full_stripe_start >> SECTOR_SHIFT;
+       bio.bi_private = &io_done;
+       bio.bi_end_io = raid56_scrub_wait_endio;
+
+       btrfs_bio_counter_inc_blocked(fs_info);
+       ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, full_stripe_start,
+                             &length, &bioc, NULL, NULL);
+       if (ret < 0)
+               goto out;
+       /* For RAID56 write there must be an @bioc allocated. */
+       ASSERT(bioc);
+       rbio = raid56_parity_alloc_scrub_rbio(&bio, bioc, scrub_dev, extent_bitmap,
+                               BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits);
+       btrfs_put_bioc(bioc);
+       if (!rbio) {
+               ret = -ENOMEM;
+               goto out;
+       }
+       /* Use the recovered stripes as cache to avoid read them from disk again. */
+       for (int i = 0; i < data_stripes; i++) {
+               struct scrub_stripe *stripe = &sctx->raid56_data_stripes[i];
+
+               raid56_parity_cache_data_folios(rbio, stripe->folios,
+                               full_stripe_start + (i << BTRFS_STRIPE_LEN_SHIFT));
+       }
+       raid56_parity_submit_scrub_rbio(rbio);
+       wait_for_completion_io(&io_done);
+       ret = blk_status_to_errno(bio.bi_status);
+out:
+       btrfs_bio_counter_dec(fs_info);
+       bio_uninit(&bio);
+       return ret;
+}
+
 static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
                                      struct btrfs_device *scrub_dev,
                                      struct btrfs_block_group *bg,
                                      struct btrfs_chunk_map *map,
                                      u64 full_stripe_start)
 {
-       DECLARE_COMPLETION_ONSTACK(io_done);
        struct btrfs_fs_info *fs_info = sctx->fs_info;
-       struct btrfs_raid_bio *rbio;
-       struct btrfs_io_context *bioc = NULL;
        struct btrfs_path extent_path = { 0 };
        struct btrfs_path csum_path = { 0 };
-       struct bio *bio;
        struct scrub_stripe *stripe;
        bool all_empty = true;
        const int data_stripes = nr_data_stripes(map);
        unsigned long extent_bitmap = 0;
-       u64 length = btrfs_stripe_nr_to_offset(data_stripes);
        int ret;
 
        ASSERT(sctx->raid56_data_stripes);
@@ -2252,42 +2297,8 @@ static int scrub_raid56_parity_stripe(struct scrub_ctx *sctx,
        }
 
        /* Now we can check and regenerate the P/Q stripe. */
-       bio = bio_alloc(NULL, 1, REQ_OP_READ, GFP_NOFS);
-       bio->bi_iter.bi_sector = full_stripe_start >> SECTOR_SHIFT;
-       bio->bi_private = &io_done;
-       bio->bi_end_io = raid56_scrub_wait_endio;
-
-       btrfs_bio_counter_inc_blocked(fs_info);
-       ret = btrfs_map_block(fs_info, BTRFS_MAP_WRITE, full_stripe_start,
-                             &length, &bioc, NULL, NULL);
-       if (ret < 0) {
-               bio_put(bio);
-               btrfs_put_bioc(bioc);
-               btrfs_bio_counter_dec(fs_info);
-               goto out;
-       }
-       rbio = raid56_parity_alloc_scrub_rbio(bio, bioc, scrub_dev, &extent_bitmap,
-                               BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits);
-       btrfs_put_bioc(bioc);
-       if (!rbio) {
-               ret = -ENOMEM;
-               bio_put(bio);
-               btrfs_bio_counter_dec(fs_info);
-               goto out;
-       }
-       /* Use the recovered stripes as cache to avoid read them from disk again. */
-       for (int i = 0; i < data_stripes; i++) {
-               stripe = &sctx->raid56_data_stripes[i];
-
-               raid56_parity_cache_data_folios(rbio, stripe->folios,
-                               full_stripe_start + (i << BTRFS_STRIPE_LEN_SHIFT));
-       }
-       raid56_parity_submit_scrub_rbio(rbio);
-       wait_for_completion_io(&io_done);
-       ret = blk_status_to_errno(bio->bi_status);
-       bio_put(bio);
-       btrfs_bio_counter_dec(fs_info);
-
+       ret = scrub_raid56_cached_parity(sctx, scrub_dev, map, full_stripe_start,
+                                        &extent_bitmap);
 out:
        btrfs_release_path(&extent_path);
        btrfs_release_path(&csum_path);