]> git.hungrycats.org Git - linux/commitdiff
btrfs: add stripe_len < PAGE_SIZE support for alloc_rbio_*_pages()
authorQu Wenruo <wqu@suse.com>
Thu, 22 Oct 2026 09:39:29 +0000 (20:09 +1030)
committerDavid Sterba <dsterba@suse.com>
Fri, 30 Oct 2026 20:36:19 +0000 (21:36 +0100)
The functions alloc_rbio_parity_pages() and alloc_rbio_data_pages()
didn't support stripe_len < PAGE_SIZE cases, and fallback to
alloc_rbio_pages() instead.

That's mostly to avoid the case where both data and P/Q blocks can exist
in the same page.

However even in that case, it's not a big deal, the existing
infrastructures like index_stripe_sectors() can handle it correctly.

Update those functions to support stripe_len < PAGE_SIZE cases, with two
points to note:

- Extra attention needed for the round direction
  For alloc_rbio_data_pages() we need to round up, so the last data page
  gets allocated.

  For alloc_rbio_parity_pages() we need to round down, so the first
  P/Q page get allocated.

- Mixed page allocation
  We can call alloc_rbio_parity_pages() first, then
  alloc_rbio_data_pages().

  In that case, it's alloc_pages_bulk() saving us, which always skips
  allocated slot. Thus we're still safe.

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

index c81f7a0297e4aae0811ca3fcc7751d1db49e8970..7dd937fa0c262c9eb860ee4551fda3e058b9ba62 100644 (file)
@@ -1061,22 +1061,21 @@ static int alloc_rbio_pages(struct btrfs_raid_bio *rbio)
        return 0;
 }
 
-/* only allocate pages for p/q stripes */
+/*
+ * Allocate pages that cover P/Q blocks.
+ *
+ * For stripe_len < PAGE_SIZE case, we can allocate a page that cover both
+ * data and P/Q blocks.
+ */
 static int alloc_rbio_parity_pages(struct btrfs_raid_bio *rbio)
 {
-       int data_pages;
+       const unsigned int first_pq_page = round_down(rbio->nr_data *
+                                       rbio->bioc->stripe_len, PAGE_SIZE) >>
+                                       PAGE_SHIFT;
        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);
+       ret = btrfs_alloc_page_array(rbio->nr_pages - first_pq_page,
+                                    rbio->stripe_pages + first_pq_page, GFP_NOFS);
        if (ret < 0)
                return ret;
 
@@ -1653,13 +1652,10 @@ static void submit_read_wait_bio_list(struct btrfs_raid_bio *rbio,
 
 static int alloc_rbio_data_pages(struct btrfs_raid_bio *rbio)
 {
-       int data_pages;
+       const unsigned int data_pages = round_up(rbio->nr_data *
+                       rbio->bioc->stripe_len, PAGE_SIZE) >> PAGE_SHIFT;
        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;