Skip to content

Commit 29c36d7

Browse files
asjkdave
authored andcommitted
Btrfs: add btrfs_read_dev_one_super() to read one specific SB
This uses a chunk of code from btrfs_read_dev_super() and creates a function called btrfs_read_dev_one_super() so that next patch can use it for scratch superblock. Signed-off-by: Anand Jain <[email protected]> [renamed bufhead to bh] Signed-off-by: David Sterba <[email protected]>
1 parent d74a625 commit 29c36d7

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

fs/btrfs/disk-io.c

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3188,14 +3188,44 @@ static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
31883188
put_bh(bh);
31893189
}
31903190

3191+
int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num,
3192+
struct buffer_head **bh_ret)
3193+
{
3194+
struct buffer_head *bh;
3195+
struct btrfs_super_block *super;
3196+
u64 bytenr;
3197+
3198+
bytenr = btrfs_sb_offset(copy_num);
3199+
if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode))
3200+
return -EINVAL;
3201+
3202+
bh = __bread(bdev, bytenr / 4096, BTRFS_SUPER_INFO_SIZE);
3203+
/*
3204+
* If we fail to read from the underlying devices, as of now
3205+
* the best option we have is to mark it EIO.
3206+
*/
3207+
if (!bh)
3208+
return -EIO;
3209+
3210+
super = (struct btrfs_super_block *)bh->b_data;
3211+
if (btrfs_super_bytenr(super) != bytenr ||
3212+
btrfs_super_magic(super) != BTRFS_MAGIC) {
3213+
brelse(bh);
3214+
return -EINVAL;
3215+
}
3216+
3217+
*bh_ret = bh;
3218+
return 0;
3219+
}
3220+
3221+
31913222
struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
31923223
{
31933224
struct buffer_head *bh;
31943225
struct buffer_head *latest = NULL;
31953226
struct btrfs_super_block *super;
31963227
int i;
31973228
u64 transid = 0;
3198-
u64 bytenr;
31993229
int ret = -EINVAL;
32003230

32013231
/* we would like to check all the supers, but that would make
@@ -3204,28 +3234,11 @@ struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
32043234
* later supers, using BTRFS_SUPER_MIRROR_MAX instead
32053235
*/
32063236
for (i = 0; i < 1; i++) {
3207-
bytenr = btrfs_sb_offset(i);
3208-
if (bytenr + BTRFS_SUPER_INFO_SIZE >=
3209-
i_size_read(bdev->bd_inode))
3210-
break;
3211-
bh = __bread(bdev, bytenr / 4096,
3212-
BTRFS_SUPER_INFO_SIZE);
3213-
/*
3214-
* If we fail to read from the underlying devices, as of now
3215-
* the best option we have is to mark it EIO.
3216-
*/
3217-
if (!bh) {
3218-
ret = -EIO;
3237+
ret = btrfs_read_dev_one_super(bdev, i, &bh);
3238+
if (ret)
32193239
continue;
3220-
}
32213240

32223241
super = (struct btrfs_super_block *)bh->b_data;
3223-
if (btrfs_super_bytenr(super) != bytenr ||
3224-
btrfs_super_magic(super) != BTRFS_MAGIC) {
3225-
brelse(bh);
3226-
ret = -EINVAL;
3227-
continue;
3228-
}
32293242

32303243
if (!latest || btrfs_super_generation(super) > transid) {
32313244
brelse(latest);

fs/btrfs/disk-io.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ void close_ctree(struct btrfs_root *root);
6060
int write_ctree_super(struct btrfs_trans_handle *trans,
6161
struct btrfs_root *root, int max_mirrors);
6262
struct buffer_head *btrfs_read_dev_super(struct block_device *bdev);
63+
int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num,
64+
struct buffer_head **bh_ret);
6365
int btrfs_commit_super(struct btrfs_root *root);
6466
struct extent_buffer *btrfs_find_tree_block(struct btrfs_fs_info *fs_info,
6567
u64 bytenr);

0 commit comments

Comments
 (0)