From: Qu Wenruo <wqu@suse.com>
To: Christoph Hellwig <hch@lst.de>
Cc: linux-btrfs@vger.kernel.org
Subject: Re: [PATCH 7/8] btrfs: refactor getting the address of a stripe sector
Date: Thu, 10 Apr 2025 08:08:07 +0930 [thread overview]
Message-ID: <eab9f278-2c9e-43b2-8039-8cd5158ace4c@suse.com> (raw)
In-Reply-To: <20250409111055.3640328-8-hch@lst.de>
在 2025/4/9 20:40, Christoph Hellwig 写道:
> Add a helper to get the actual kernel address of a stripe instead of
> just the page and offset into it to simplify the code, and add another
> helper to add the memory backing a sector to a bio using the above
> helper.
>
> This nicely abstracts away the page + offset representation from almost
> all of the scrub code.
I love this change, and it should also be safe because all scrub pages
are allocated by ourselves, and are ensured not to be high mem.
Reviewed-by: Qu Wenruo <wqu@suse.com>
Thanks,
Qu>
> Signed-off-by: Christoph Hellwig <hch@lst.de>
> ---
> fs/btrfs/scrub.c | 81 +++++++++++++++---------------------------------
> 1 file changed, 25 insertions(+), 56 deletions(-)
>
> diff --git a/fs/btrfs/scrub.c b/fs/btrfs/scrub.c
> index 49021765c17b..d014b728eb0d 100644
> --- a/fs/btrfs/scrub.c
> +++ b/fs/btrfs/scrub.c
> @@ -579,20 +579,13 @@ static int fill_writer_pointer_gap(struct scrub_ctx *sctx, u64 physical)
> return ret;
> }
>
> -static struct page *scrub_stripe_get_page(struct scrub_stripe *stripe, int sector_nr)
> +static void *scrub_stripe_get_kaddr(struct scrub_stripe *stripe, int sector_nr)
> {
> struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
> - int page_index = (sector_nr << fs_info->sectorsize_bits) >> PAGE_SHIFT;
> + u32 offset = sector_nr << fs_info->sectorsize_bits;
>
> - return stripe->pages[page_index];
> -}
> -
> -static unsigned int scrub_stripe_get_page_offset(struct scrub_stripe *stripe,
> - int sector_nr)
> -{
> - struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
> -
> - return offset_in_page(sector_nr << fs_info->sectorsize_bits);
> + return page_address(stripe->pages[offset >> PAGE_SHIFT]) +
> + offset_in_page(offset);
> }
>
> static void scrub_verify_one_metadata(struct scrub_stripe *stripe, int sector_nr)
> @@ -600,19 +593,17 @@ static void scrub_verify_one_metadata(struct scrub_stripe *stripe, int sector_nr
> struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
> const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits;
> const u64 logical = stripe->logical + (sector_nr << fs_info->sectorsize_bits);
> - const struct page *first_page = scrub_stripe_get_page(stripe, sector_nr);
> - const unsigned int first_off = scrub_stripe_get_page_offset(stripe, sector_nr);
> + void *first_kaddr = scrub_stripe_get_kaddr(stripe, sector_nr);
> SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
> u8 on_disk_csum[BTRFS_CSUM_SIZE];
> u8 calculated_csum[BTRFS_CSUM_SIZE];
> - struct btrfs_header *header;
> + struct btrfs_header *header = first_kaddr;
>
> /*
> * Here we don't have a good way to attach the pages (and subpages)
> * to a dummy extent buffer, thus we have to directly grab the members
> * from pages.
> */
> - header = (struct btrfs_header *)(page_address(first_page) + first_off);
> memcpy(on_disk_csum, header->csum, fs_info->csum_size);
>
> if (logical != btrfs_stack_header_bytenr(header)) {
> @@ -648,14 +639,11 @@ static void scrub_verify_one_metadata(struct scrub_stripe *stripe, int sector_nr
> /* Now check tree block csum. */
> shash->tfm = fs_info->csum_shash;
> crypto_shash_init(shash);
> - crypto_shash_update(shash, page_address(first_page) + first_off +
> - BTRFS_CSUM_SIZE, fs_info->sectorsize - BTRFS_CSUM_SIZE);
> + crypto_shash_update(shash, first_kaddr + BTRFS_CSUM_SIZE,
> + fs_info->sectorsize - BTRFS_CSUM_SIZE);
>
> for (int i = sector_nr + 1; i < sector_nr + sectors_per_tree; i++) {
> - struct page *page = scrub_stripe_get_page(stripe, i);
> - unsigned int page_off = scrub_stripe_get_page_offset(stripe, i);
> -
> - crypto_shash_update(shash, page_address(page) + page_off,
> + crypto_shash_update(shash, scrub_stripe_get_kaddr(stripe, i),
> fs_info->sectorsize);
> }
>
> @@ -691,10 +679,8 @@ static void scrub_verify_one_sector(struct scrub_stripe *stripe, int sector_nr)
> struct btrfs_fs_info *fs_info = stripe->bg->fs_info;
> struct scrub_sector_verification *sector = &stripe->sectors[sector_nr];
> const u32 sectors_per_tree = fs_info->nodesize >> fs_info->sectorsize_bits;
> - struct page *page = scrub_stripe_get_page(stripe, sector_nr);
> - unsigned int pgoff = scrub_stripe_get_page_offset(stripe, sector_nr);
> + void *kaddr = scrub_stripe_get_kaddr(stripe, sector_nr);
> u8 csum_buf[BTRFS_CSUM_SIZE];
> - void *kaddr;
> int ret;
>
> ASSERT(sector_nr >= 0 && sector_nr < stripe->nr_sectors);
> @@ -738,9 +724,7 @@ static void scrub_verify_one_sector(struct scrub_stripe *stripe, int sector_nr)
> return;
> }
>
> - kaddr = kmap_local_page(page) + pgoff;
> ret = btrfs_check_sector_csum(fs_info, kaddr, csum_buf, sector->csum);
> - kunmap_local(kaddr);
> if (ret < 0) {
> set_bit(sector_nr, &stripe->csum_error_bitmap);
> set_bit(sector_nr, &stripe->error_bitmap);
> @@ -769,8 +753,7 @@ static int calc_sector_number(struct scrub_stripe *stripe, struct bio_vec *first
> int i;
>
> for (i = 0; i < stripe->nr_sectors; i++) {
> - if (scrub_stripe_get_page(stripe, i) == first_bvec->bv_page &&
> - scrub_stripe_get_page_offset(stripe, i) == first_bvec->bv_offset)
> + if (scrub_stripe_get_kaddr(stripe, i) == bvec_virt(first_bvec))
> break;
> }
> ASSERT(i < stripe->nr_sectors);
> @@ -817,6 +800,15 @@ static int calc_next_mirror(int mirror, int num_copies)
> return (mirror + 1 > num_copies) ? 1 : mirror + 1;
> }
>
> +static void scrub_bio_add_sector(struct btrfs_bio *bbio,
> + struct scrub_stripe *stripe, int sector_nr)
> +{
> + void *kaddr = scrub_stripe_get_kaddr(stripe, sector_nr);
> +
> + __bio_add_page(&bbio->bio, virt_to_page(kaddr),
> + bbio->fs_info->sectorsize, offset_in_page(kaddr));
> +}
> +
> static void scrub_stripe_submit_repair_read(struct scrub_stripe *stripe,
> int mirror, int blocksize, bool wait)
> {
> @@ -829,13 +821,6 @@ static void scrub_stripe_submit_repair_read(struct scrub_stripe *stripe,
> ASSERT(atomic_read(&stripe->pending_io) == 0);
>
> for_each_set_bit(i, &old_error_bitmap, stripe->nr_sectors) {
> - struct page *page;
> - int pgoff;
> - int ret;
> -
> - page = scrub_stripe_get_page(stripe, i);
> - pgoff = scrub_stripe_get_page_offset(stripe, i);
> -
> /* The current sector cannot be merged, submit the bio. */
> if (bbio && ((i > 0 && !test_bit(i - 1, &stripe->error_bitmap)) ||
> bbio->bio.bi_iter.bi_size >= blocksize)) {
> @@ -854,8 +839,7 @@ static void scrub_stripe_submit_repair_read(struct scrub_stripe *stripe,
> (i << fs_info->sectorsize_bits)) >> SECTOR_SHIFT;
> }
>
> - ret = bio_add_page(&bbio->bio, page, fs_info->sectorsize, pgoff);
> - ASSERT(ret == fs_info->sectorsize);
> + scrub_bio_add_sector(bbio, stripe, i);
> }
> if (bbio) {
> ASSERT(bbio->bio.bi_iter.bi_size);
> @@ -1202,10 +1186,6 @@ static void scrub_write_sectors(struct scrub_ctx *sctx, struct scrub_stripe *str
> int sector_nr;
>
> for_each_set_bit(sector_nr, &write_bitmap, stripe->nr_sectors) {
> - struct page *page = scrub_stripe_get_page(stripe, sector_nr);
> - unsigned int pgoff = scrub_stripe_get_page_offset(stripe, sector_nr);
> - int ret;
> -
> /* We should only writeback sectors covered by an extent. */
> ASSERT(test_bit(sector_nr, &stripe->extent_sector_bitmap));
>
> @@ -1221,8 +1201,7 @@ static void scrub_write_sectors(struct scrub_ctx *sctx, struct scrub_stripe *str
> (sector_nr << fs_info->sectorsize_bits)) >>
> SECTOR_SHIFT;
> }
> - ret = bio_add_page(&bbio->bio, page, fs_info->sectorsize, pgoff);
> - ASSERT(ret == fs_info->sectorsize);
> + scrub_bio_add_sector(bbio, stripe, sector_nr);
> }
> if (bbio)
> scrub_submit_write_bio(sctx, stripe, bbio, dev_replace);
> @@ -1675,9 +1654,6 @@ static void scrub_submit_extent_sector_read(struct scrub_stripe *stripe)
> atomic_inc(&stripe->pending_io);
>
> for_each_set_bit(i, &stripe->extent_sector_bitmap, stripe->nr_sectors) {
> - struct page *page = scrub_stripe_get_page(stripe, i);
> - unsigned int pgoff = scrub_stripe_get_page_offset(stripe, i);
> -
> /* We're beyond the chunk boundary, no need to read anymore. */
> if (i >= nr_sectors)
> break;
> @@ -1730,7 +1706,7 @@ static void scrub_submit_extent_sector_read(struct scrub_stripe *stripe)
> bbio->bio.bi_iter.bi_sector = logical >> SECTOR_SHIFT;
> }
>
> - __bio_add_page(&bbio->bio, page, fs_info->sectorsize, pgoff);
> + scrub_bio_add_sector(bbio, stripe, i);
> }
>
> if (bbio) {
> @@ -1768,15 +1744,8 @@ static void scrub_submit_initial_read(struct scrub_ctx *sctx,
>
> bbio->bio.bi_iter.bi_sector = stripe->logical >> SECTOR_SHIFT;
> /* Read the whole range inside the chunk boundary. */
> - for (unsigned int cur = 0; cur < nr_sectors; cur++) {
> - struct page *page = scrub_stripe_get_page(stripe, cur);
> - unsigned int pgoff = scrub_stripe_get_page_offset(stripe, cur);
> - int ret;
> -
> - ret = bio_add_page(&bbio->bio, page, fs_info->sectorsize, pgoff);
> - /* We should have allocated enough bio vectors. */
> - ASSERT(ret == fs_info->sectorsize);
> - }
> + for (unsigned int cur = 0; cur < nr_sectors; cur++)
> + scrub_bio_add_sector(bbio, stripe, cur);
> atomic_inc(&stripe->pending_io);
>
> /*
next prev parent reply other threads:[~2025-04-09 22:38 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-09 11:10 RFC: (almost) stop poking into bvec internals in btrfs Christoph Hellwig
2025-04-09 11:10 ` [PATCH 1/8] btrfs: remove the alignment checks in end_bbio_data_read Christoph Hellwig
2025-04-09 22:13 ` Qu Wenruo
2025-04-10 5:30 ` Christoph Hellwig
2025-04-10 5:39 ` Qu Wenruo
2025-04-09 11:10 ` [PATCH 2/8] btrfs: track the next file offset in struct btrfs_bio_ctrl Christoph Hellwig
2025-04-09 22:15 ` Qu Wenruo
2025-04-09 11:10 ` [PATCH 3/8] btrfs: pass a physical address to btrfs_repair_io_failure Christoph Hellwig
2025-04-09 22:19 ` Qu Wenruo
2025-04-10 5:31 ` Christoph Hellwig
2025-04-10 6:06 ` Johannes Thumshirn
2025-04-10 6:17 ` hch
2025-04-09 11:10 ` [PATCH 4/8] btrfs: move kmapping out of btrfs_check_sector_csum Christoph Hellwig
2025-04-10 6:16 ` Johannes Thumshirn
2025-04-16 4:51 ` Qu Wenruo
2025-04-09 11:10 ` [PATCH 5/8] btrfs: simplify bvec iteration in index_one_bio Christoph Hellwig
2025-04-18 2:09 ` Qu Wenruo
2025-04-09 11:10 ` [PATCH 6/8] btrfs: store a kernel virtual address in struct sector_ptr Christoph Hellwig
2025-04-09 22:34 ` Qu Wenruo
2025-04-10 5:34 ` Christoph Hellwig
2025-04-14 3:04 ` Qu Wenruo
2025-04-17 23:41 ` Qu Wenruo
2025-04-09 11:10 ` [PATCH 7/8] btrfs: refactor getting the address of a stripe sector Christoph Hellwig
2025-04-09 22:38 ` Qu Wenruo [this message]
2025-04-19 1:01 ` Qu Wenruo
2025-04-09 11:10 ` [PATCH 8/8] btrfs: use bvec_kmap_local in btrfs_decompress_buf2page Christoph Hellwig
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=eab9f278-2c9e-43b2-8039-8cd5158ace4c@suse.com \
--to=wqu@suse.com \
--cc=hch@lst.de \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox