btrfs-progs: handle write errors in btrfs_add_to_fsid()

Add template for read/write error messages and use it for write of
superblock when adding a device. sbwrite() is wrapper around write that
makes sure the zoned devices are accessed correctly.

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2024-03-16 18:52:18 +01:00
parent f3ece218b6
commit 060f7d6c81
3 changed files with 9 additions and 1 deletions

View file

@ -208,7 +208,11 @@ int btrfs_add_to_fsid(struct btrfs_trans_handle *trans,
ret = sbwrite(fd, buf, BTRFS_SUPER_INFO_OFFSET);
/* Ensure super block was written to the device */
BUG_ON(ret != BTRFS_SUPER_INFO_SIZE);
if (ret != BTRFS_SUPER_INFO_SIZE) {
error_msg(ERROR_MSG_WRITE, "superblock when adding device: %m");
ret = -EIO;
goto out;
}
free(buf);
list_add(&device->dev_list, &fs_info->fs_devices->devices);
device->fs_devices = fs_info->fs_devices;

View file

@ -25,6 +25,8 @@ static const char *common_error_string[] = {
[ERROR_MSG_START_TRANS] = "failed to start transaction",
[ERROR_MSG_COMMIT_TRANS] = "failed to commit transaction",
[ERROR_MSG_UNEXPECTED] = "unexpected condition, probably corruption",
[ERROR_MSG_READ] = "write() failed",
[ERROR_MSG_WRITE] = "read() failed",
};
__attribute__ ((format (printf, 1, 2)))

View file

@ -191,6 +191,8 @@ enum common_error {
ERROR_MSG_START_TRANS,
ERROR_MSG_COMMIT_TRANS,
ERROR_MSG_UNEXPECTED,
ERROR_MSG_READ,
ERROR_MSG_WRITE,
};
__attribute__ ((format (printf, 2, 3)))