btrfs-progs: zoned: check SB zone existence properly

Currently, write_dev_supers() compares the superblock location vs the size
of the device to check if it can write the superblock. This is not correct
for a zoned device, whose superblock location is different than a regular
device.

Introduce check_sb_location() to check if the superblock zone exists for
the zoned case.

Running btrfs check can fail on a certain zoned device setup (e.g,
zone size = 128MB, device size = 16GB).

From generic/330:

  yes | btrfs check --repair --force /dev/nullb1
  [1/7] checking root items
  Fixed 0 roots.
  [2/7] checking extents
  ERROR: zoned: failed to read zone info of 4096 and 4097: Invalid argument
  ERROR: failed to write super block for devid 1: write error: Input/output error
  failed to write new super block err -5
  failed to repair damaged filesystem, aborting

This happens because write_dev_supers() is comparing the original
superblock location vs the device size to check if it can write out a
superblock copy or not.

For the above example, since the first copy location (64MB) < device size
(16GB), it tries to write out the copy. But, the copy must be written into
zone 4096 (512G / zone size (128M) = 4096), which is out of the device.

Signed-off-by: Naohiro Aota <naohiro.aota@wdc.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Naohiro Aota 2023-10-12 23:19:30 +09:00 committed by David Sterba
parent 58148d5209
commit 8816a65fec
3 changed files with 22 additions and 1 deletions

View file

@ -1998,6 +1998,13 @@ int btrfs_read_dev_super(int fd, struct btrfs_super_block *sb, u64 sb_bytenr,
return transid > 0 ? 0 : -1;
}
static bool check_sb_location(struct btrfs_device *device, u64 bytenr)
{
if (!device->zone_info)
return bytenr + BTRFS_SUPER_INFO_SIZE <= device->total_bytes;
return btrfs_sb_zone_exists(device, bytenr);
}
static int write_dev_supers(struct btrfs_fs_info *fs_info,
struct btrfs_super_block *sb,
struct btrfs_device *device)
@ -2048,7 +2055,7 @@ static int write_dev_supers(struct btrfs_fs_info *fs_info,
for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
bytenr = btrfs_sb_offset(i);
if (bytenr + BTRFS_SUPER_INFO_SIZE > device->total_bytes)
if (!check_sb_location(device, bytenr))
break;
btrfs_set_super_bytenr(sb, bytenr);

View file

@ -999,6 +999,14 @@ int btrfs_wipe_temporary_sb(struct btrfs_fs_devices *fs_devices)
return ret;
}
bool btrfs_sb_zone_exists(struct btrfs_device *device, u64 bytenr)
{
u32 zone_num = sb_bytenr_to_sb_zone(bytenr,
ilog2(device->zone_info->zone_size));
return zone_num + 1 <= device->zone_info->nr_zones - 1;
}
#endif
int btrfs_get_dev_zone_info_all_devices(struct btrfs_fs_info *fs_info)

View file

@ -153,6 +153,7 @@ int btrfs_reset_all_zones(int fd, struct btrfs_zoned_device_info *zinfo);
int zero_zone_blocks(int fd, struct btrfs_zoned_device_info *zinfo, off_t start,
size_t len);
int btrfs_wipe_temporary_sb(struct btrfs_fs_devices *fs_devices);
bool btrfs_sb_zone_exists(struct btrfs_device *device, u64 bytenr);
#else
@ -225,6 +226,11 @@ static inline bool zoned_profile_supported(u64 map_type, bool rst)
return false;
}
static inline bool btrfs_sb_zone_exists(struct btrfs_device *device, u64 bytenr)
{
return true;
}
#endif /* BTRFS_ZONED */
/*