btrfs-progs: do not BUG_ON if btrfs_add_to_fsid succeeded to write superblock

Commit 8ef9313cf2 ("btrfs-progs: zoned: implement log-structured
superblock") changed to write BTRFS_SUPER_INFO_SIZE bytes to device.
The before num of bytes to be written is sectorsize.
It causes mkfs.btrfs failed on my 16k pagesize kvm:

  $ /usr/bin/mkfs.btrfs -s 16k -f -mraid0 /dev/vdb2 /dev/vdb3
  btrfs-progs v5.12
  See http://btrfs.wiki.kernel.org for more information.

  ERROR: superblock magic doesn't match
  ERROR: superblock magic doesn't match
  common/device-scan.c:195: btrfs_add_to_fsid: BUG_ON `ret != sectorsize`
  triggered, value 1
  /usr/bin/mkfs.btrfs(btrfs_add_to_fsid+0x274)[0xaaab4fe8a5fc]
  /usr/bin/mkfs.btrfs(main+0x1188)[0xaaab4fe4dc8c]
  /usr/lib/libc.so.6(__libc_start_main+0xe8)[0xffff7223c538]
  /usr/bin/mkfs.btrfs(+0xc558)[0xaaab4fe4c558]

  [1]    225842 abort (core dumped)  /usr/bin/mkfs.btrfs -s 16k -f -mraid0
  /dev/vdb2 /dev/vdb3

btrfs_add_to_fsid() now always calls sbwrite() to write
BTRFS_SUPER_INFO_SIZE bytes to device, so change condition of
the BUG_ON().
Also add comments for sbread() and sbwrite().

Signed-off-by: Su Yue <l@damenly.su>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Su Yue 2021-05-11 12:25:01 +08:00 committed by David Sterba
parent d4b4baf312
commit 80a86f1b47
2 changed files with 20 additions and 2 deletions

View File

@ -192,8 +192,8 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,
memcpy(&dev_item->uuid, device->uuid, BTRFS_UUID_SIZE);
ret = sbwrite(fd, buf, BTRFS_SUPER_INFO_OFFSET);
BUG_ON(ret != sectorsize);
/* Ensure super block was written to the device */
BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
free(buf);
list_add(&device->dev_list, &fs_info->fs_devices->devices);
device->fs_devices = fs_info->fs_devices;

View File

@ -51,11 +51,29 @@ int btrfs_check_zoned_mode(struct btrfs_fs_info *fs_info);
#ifdef BTRFS_ZONED
size_t btrfs_sb_io(int fd, void *buf, off_t offset, int rw);
/*
* Read BTRFS_SUPER_INFO_SIZE bytes from fd to buf
*
* @fd fd of the device to be read from
* @buf: buffer contains a super block
* @offset: offset of the superblock
*
* Return count of bytes successfully read.
*/
static inline size_t sbread(int fd, void *buf, off_t offset)
{
return btrfs_sb_io(fd, buf, offset, READ);
}
/*
* Write BTRFS_SUPER_INFO_SIZE bytes from buf to fd
*
* @fd fd of the device to be written to
* @buf: buffer contains a super block
* @offset: offset of the superblock
*
* Return count of bytes successfully written.
*/
static inline size_t sbwrite(int fd, void *buf, off_t offset)
{
return btrfs_sb_io(fd, buf, offset, WRITE);