]> git.hungrycats.org Git - linux/commitdiff
btrfs: convert raid56.c to use rbio->bioc->stripe_len
authorQu Wenruo <wqu@suse.com>
Wed, 21 Oct 2026 03:39:41 +0000 (14:09 +1030)
committerQu Wenruo <wqu@suse.com>
Wed, 21 Oct 2026 22:42:52 +0000 (09:12 +1030)
So that raid56 can support stripe length other than BTRFS_STRIPE_LEN.

There are several extra things to note:

- btrfs_raid_bio::stripe_npages is removed
  With stripe_len < BTRFS_STRIPE_LEN, there is no guarantee that a page
  won't cross multiple stripes.

  E.g. with 4K stripe_len and sectorsize, 2 data stripes, with 64K page
  size, one page will cover the full stripe.

  So stripe_npages no longer makes sense, just remove it.

- alloc_rbio_data_pages()/alloc_rbio_parity_pages() fallback
  For the same above reason, a page can contain both data and parity
  blocks.
  In that case we can no longer only allocate pages for data/parity.

  Add a fallback that if stripe_len is smaller than page size, always
  allocate all the pages.

  This is not the best solution, but should be good enough for now until
  the variable stripe_len is properly tested.

- steal_rbio() fallback
  If stripe_len is smaller than PAGE_SIZE, it's hard to only steal the
  data pages.

  Again skip the stealing if stripe_len is smaller than PAGE_SIZE.

Reviewed-by: Johannes Thusmhirn <johannes.thumshirn@wdc.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
fs/btrfs/raid56.c
fs/btrfs/raid56.h

index 5ec31299f1c301347b39d28f52d9d0052f4168d5..080be467c6fcb9d2fa57f64c2a92d500c232ef49 100644 (file)
@@ -395,8 +395,9 @@ static bool is_data_stripe_page(struct btrfs_raid_bio *rbio, int page_nr)
        const int sector_nr = page_nr_to_sector_nr(rbio, page_nr);
 
        /*
-        * We have ensured PAGE_SIZE is aligned with sectorsize, thus
-        * we won't have a page which is half data half parity.
+        * The caller has ensured stripe_len >= PAGE_SIZE, and PAGE_SIZE is
+        * aligned with sectorsize, thus we won't have a page which is half
+        * data half parity.
         *
         * Thus if the first sector of the page belongs to data stripes, then
         * the full page belongs to data stripes.
@@ -417,6 +418,9 @@ static void steal_rbio(struct btrfs_raid_bio *src, struct btrfs_raid_bio *dest)
 
        if (!test_bit(RBIO_CACHE_READY_BIT, &src->flags))
                return;
+       /* Data and P/Q can be mixed into a page, skip stealing. */
+       if (src->bioc->stripe_len < PAGE_SIZE)
+               return;
 
        for (i = 0; i < dest->nr_pages; i++) {
                struct page *p = src->stripe_pages[i];
@@ -624,12 +628,13 @@ static void cache_rbio(struct btrfs_raid_bio *rbio)
 static int rbio_is_full(struct btrfs_raid_bio *rbio)
 {
        unsigned long size = rbio->bio_list_bytes;
+       const u32 stripe_len = rbio->bioc->stripe_len;
        int ret = 1;
 
        spin_lock(&rbio->bio_list_lock);
-       if (size != rbio->nr_data * BTRFS_STRIPE_LEN)
+       if (size != rbio->nr_data * stripe_len)
                ret = 0;
-       BUG_ON(size > rbio->nr_data * BTRFS_STRIPE_LEN);
+       BUG_ON(size > rbio->nr_data * stripe_len);
        spin_unlock(&rbio->bio_list_lock);
 
        return ret;
@@ -1041,11 +1046,12 @@ static phys_addr_t sector_paddr_in_rbio(struct btrfs_raid_bio *rbio,
 static struct btrfs_raid_bio *alloc_rbio(struct btrfs_fs_info *fs_info,
                                         struct btrfs_io_context *bioc)
 {
+       const u32 stripe_len = bioc->stripe_len;
        const unsigned int real_stripes = bioc->num_stripes - bioc->replace_nr_stripes;
-       const unsigned int stripe_npages = BTRFS_STRIPE_LEN >> PAGE_SHIFT;
-       const unsigned int num_pages = stripe_npages * real_stripes;
+       const unsigned int num_pages = round_up(stripe_len * real_stripes, PAGE_SIZE) >>
+                                      PAGE_SHIFT;
        const unsigned int stripe_nsectors =
-               BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits;
+               stripe_len >> fs_info->sectorsize_bits;
        const unsigned int num_sectors = stripe_nsectors * real_stripes;
        const unsigned int step = min(fs_info->sectorsize, PAGE_SIZE);
        const unsigned int sector_nsteps = fs_info->sectorsize / step;
@@ -1105,7 +1111,6 @@ static struct btrfs_raid_bio *alloc_rbio(struct btrfs_fs_info *fs_info,
        rbio->nr_pages = num_pages;
        rbio->nr_sectors = num_sectors;
        rbio->real_stripes = real_stripes;
-       rbio->stripe_npages = stripe_npages;
        rbio->stripe_nsectors = stripe_nsectors;
        rbio->sector_nsteps = sector_nsteps;
        refcount_set(&rbio->refs, 1);
@@ -1134,9 +1139,17 @@ static int alloc_rbio_pages(struct btrfs_raid_bio *rbio)
 /* only allocate pages for p/q stripes */
 static int alloc_rbio_parity_pages(struct btrfs_raid_bio *rbio)
 {
-       const int data_pages = rbio->nr_data * rbio->stripe_npages;
+       int data_pages;
        int ret;
 
+       /*
+        * Page size larger than stripe size, we can not only allocate
+        * pages for P/Q stripes.
+        */
+       if (rbio->bioc->stripe_len < PAGE_SIZE)
+               return alloc_rbio_pages(rbio);
+
+       data_pages = rbio->nr_data * (rbio->bioc->stripe_len >> PAGE_SHIFT);
        ret = btrfs_alloc_page_array(rbio->nr_pages - data_pages,
                                     rbio->stripe_pages + data_pages, GFP_NOFS);
        if (ret < 0)
@@ -1274,7 +1287,7 @@ static int rbio_add_io_paddrs(struct btrfs_raid_bio *rbio, struct bio_list *bio_
 
        /* put a new bio on the list */
        bio = bio_alloc(stripe->dev->bdev,
-                       max(BTRFS_STRIPE_LEN >> PAGE_SHIFT, 1),
+                       max(rbio->bioc->stripe_len >> PAGE_SHIFT, 1),
                        op, GFP_NOFS);
        bio->bi_iter.bi_sector = disk_start >> SECTOR_SHIFT;
        bio->bi_private = rbio;
@@ -1715,9 +1728,13 @@ static void submit_read_wait_bio_list(struct btrfs_raid_bio *rbio,
 
 static int alloc_rbio_data_pages(struct btrfs_raid_bio *rbio)
 {
-       const int data_pages = rbio->nr_data * rbio->stripe_npages;
+       int data_pages;
        int ret;
 
+       if (rbio->bioc->stripe_len < PAGE_SIZE)
+               return alloc_rbio_pages(rbio);
+
+       data_pages = rbio->nr_data * (rbio->bioc->stripe_len >> PAGE_SHIFT);
        ret = btrfs_alloc_page_array(data_pages, rbio->stripe_pages, GFP_NOFS);
        if (ret < 0)
                return ret;
@@ -1804,7 +1821,7 @@ static void rbio_add_bio(struct btrfs_raid_bio *rbio, struct bio *orig_bio)
 
        ASSERT_RBIO_LOGICAL(orig_logical >= full_stripe_start &&
                            orig_logical + orig_len <= full_stripe_start +
-                           rbio->nr_data * BTRFS_STRIPE_LEN,
+                           rbio->nr_data * rbio->bioc->stripe_len,
                            rbio, orig_logical);
 
        bio_list_add(&rbio->bio_list, orig_bio);
@@ -3012,6 +3029,7 @@ void raid56_parity_cache_data_folios(struct btrfs_raid_bio *rbio,
        struct btrfs_fs_info *fs_info = rbio->bioc->fs_info;
        const u64 offset_in_full_stripe = data_logical -
                                          rbio->bioc->full_stripe_logical;
+       const u32 stripe_len = rbio->bioc->stripe_len;
        int ret;
 
        /*
@@ -3027,19 +3045,20 @@ void raid56_parity_cache_data_folios(struct btrfs_raid_bio *rbio,
                return;
 
        /* data_logical must be at stripe boundary and inside the full stripe. */
-       ASSERT(IS_ALIGNED(offset_in_full_stripe, BTRFS_STRIPE_LEN));
-       ASSERT(offset_in_full_stripe < (rbio->nr_data << BTRFS_STRIPE_LEN_SHIFT));
+       ASSERT(IS_ALIGNED(offset_in_full_stripe, stripe_len));
+       ASSERT(offset_in_full_stripe < (rbio->nr_data << ilog2(stripe_len)));
 
        for (unsigned int cur_off = offset_in_full_stripe;
-            cur_off < offset_in_full_stripe + BTRFS_STRIPE_LEN;
+            cur_off < offset_in_full_stripe + stripe_len;
             cur_off += PAGE_SIZE) {
                const unsigned int pindex = cur_off >> PAGE_SHIFT;
+               const u32 cur_len = min(stripe_len, PAGE_SIZE);
 
-               ASSERT(cur_off - offset_in_full_stripe + PAGE_SIZE <= BTRFS_STRIPE_LEN);
-               memcpy_to_page(rbio->stripe_pages[pindex], 0,
-                              vaddr + cur_off - offset_in_full_stripe, PAGE_SIZE);
+               ASSERT(cur_off - offset_in_full_stripe + cur_len <= stripe_len);
+               memcpy_to_page(rbio->stripe_pages[pindex], offset_in_page(cur_off),
+                              vaddr + cur_off - offset_in_full_stripe, cur_len);
        }
        bitmap_set(rbio->stripe_uptodate_bitmap,
                   offset_in_full_stripe >> fs_info->sectorsize_bits,
-                  BTRFS_STRIPE_LEN >> fs_info->sectorsize_bits);
+                  stripe_len >> fs_info->sectorsize_bits);
 }
index 8542648199f1a485371e3622c74aff028293b0a0..8700910b8dc998cbef0d5e376cb3f4b9f8a764be 100644 (file)
@@ -60,8 +60,6 @@ enum btrfs_rbio_ops {
  * stripe_pages[] array stores all the pages covering the full stripe, including
  * data and P/Q pages.
  * stripe_pages[0] is the first page of the first data stripe.
- * stripe_pages[BTRFS_STRIPE_LEN / PAGE_SIZE] is the first page of the second
- * data stripe.
  *
  * Some pointers inside stripe_pages[] can be NULL, e.g. for a full stripe write
  * (the bio covers all data stripes) there is no need to allocate pages for
@@ -81,7 +79,7 @@ enum btrfs_rbio_ops {
  *
  * - sector_nr
  *   Starts from 0 (representing the first sector of the stripe), ends
- *   at BTRFS_STRIPE_LEN / sectorsize - 1.
+ *   at stripe_len / sectorsize - 1.
  *
  * - step_nr
  *   A step is min(sector_size, PAGE_SIZE).
@@ -153,9 +151,6 @@ struct btrfs_raid_bio {
        /* Number of all stripes (including P/Q) */
        u8 real_stripes;
 
-       /* How many pages there are for each stripe */
-       u8 stripe_npages;
-
        /* How many sectors there are for each stripe */
        u8 stripe_nsectors;