From: David Sterba <dsterba@suse.cz>
To: Qu Wenruo <wqu@suse.com>
Cc: linux-btrfs@vger.kernel.org
Subject: Re: [PATCH 1/2] btrfs: merge btrfs_read_dev_one_super() into btrfs_read_disk_super()
Date: Tue, 29 Apr 2025 09:22:27 +0200 [thread overview]
Message-ID: <20250429072227.GD18094@twin.jikos.cz> (raw)
In-Reply-To: <b251d716c8cd93ee8b9ee32fdd399bd0fff28669.1745802753.git.wqu@suse.com>
On Mon, Apr 28, 2025 at 10:45:51AM +0930, Qu Wenruo wrote:
> -struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev,
> - int copy_num, bool drop_cache)
> -{
> - if (btrfs_super_magic(super) != BTRFS_MAGIC) {
> - btrfs_release_disk_super(super);
> - return ERR_PTR(-ENODATA);
> - }
> -
> - if (btrfs_super_bytenr(super) != bytenr_orig) {
> - btrfs_release_disk_super(super);
> - return ERR_PTR(-EINVAL);
> - }
> -
> - return super;
> -}
> -
> -
> struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev)
> {
> struct btrfs_super_block *super, *latest = NULL;
> --- a/fs/btrfs/volumes.c
> +++ b/fs/btrfs/volumes.c
> @@ -1401,48 +1401,62 @@ void btrfs_release_disk_super(struct btrfs_super_block *super)
> put_page(page);
> }
>
> -static struct btrfs_super_block *btrfs_read_disk_super(struct block_device *bdev,
> - u64 bytenr, u64 bytenr_orig)
> +struct btrfs_super_block *btrfs_read_disk_super(struct block_device *bdev,
> + int copy_num, bool drop_cache)
> {
> - struct btrfs_super_block *disk_super;
> + struct btrfs_super_block *super;
> struct page *page;
> - void *p;
> - pgoff_t index;
> + u64 bytenr, bytenr_orig;
> + struct address_space *mapping = bdev->bd_mapping;
> + int ret;
>
> - /* make sure our super fits in the device */
> - if (bytenr + PAGE_SIZE >= bdev_nr_bytes(bdev))
> + bytenr_orig = btrfs_sb_offset(copy_num);
> + ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr);
> + if (ret == -ENOENT)
> + return ERR_PTR(-EINVAL);
> + else if (ret)
> + return ERR_PTR(ret);
> +
> + if (bytenr + BTRFS_SUPER_INFO_SIZE >= bdev_nr_bytes(bdev))
> return ERR_PTR(-EINVAL);
>
> - /* make sure our super fits in the page */
> - if (sizeof(*disk_super) > PAGE_SIZE)
> - return ERR_PTR(-EINVAL);
> + if (drop_cache) {
> + /* This should only be called with the primary sb. */
> + ASSERT(copy_num == 0);
>
> - /* make sure our super doesn't straddle pages on disk */
> - index = bytenr >> PAGE_SHIFT;
> - if ((bytenr + sizeof(*disk_super) - 1) >> PAGE_SHIFT != index)
> - return ERR_PTR(-EINVAL);
> -
> - /* pull in the page with our super */
> - page = read_cache_page_gfp(bdev->bd_mapping, index, GFP_KERNEL);
> + /*
> + * Drop the page of the primary superblock, so later read will
> + * always read from the device.
> + */
> + invalidate_inode_pages2_range(mapping,
> + bytenr >> PAGE_SHIFT,
> + (bytenr + BTRFS_SUPER_INFO_SIZE) >> PAGE_SHIFT);
> + }
>
> + page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS);
> if (IS_ERR(page))
> return ERR_CAST(page);
>
> - p = page_address(page);
> + super = page_address(page);
> + if (btrfs_super_magic(super) != BTRFS_MAGIC) {
> + btrfs_release_disk_super(super);
> + return ERR_PTR(-ENODATA);
This was in the other function but I think we should unify that to
-EINVAL, other filesystems do not distinguish between a failed magic
check and other errors.
next prev parent reply other threads:[~2025-04-29 7:22 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-28 1:15 [PATCH 0/2] btrfs: merge the two different super block read functions into one Qu Wenruo
2025-04-28 1:15 ` [PATCH 1/2] btrfs: merge btrfs_read_dev_one_super() into btrfs_read_disk_super() Qu Wenruo
2025-04-28 14:21 ` Johannes Thumshirn
2025-04-28 23:11 ` Qu Wenruo
2025-04-29 7:14 ` David Sterba
2025-04-29 7:22 ` David Sterba [this message]
2025-04-29 7:39 ` Qu Wenruo
2025-04-28 1:15 ` [PATCH 2/2] btrfs: get rid of btrfs_read_dev_super() Qu Wenruo
2025-04-29 7:17 ` David Sterba
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=20250429072227.GD18094@twin.jikos.cz \
--to=dsterba@suse.cz \
--cc=linux-btrfs@vger.kernel.org \
--cc=wqu@suse.com \
/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