From 060f7d6c81f98a7322b5414405109d7d735d6b8a Mon Sep 17 00:00:00 2001 From: David Sterba Date: Sat, 16 Mar 2024 18:52:18 +0100 Subject: [PATCH] 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 --- common/device-scan.c | 6 +++++- common/messages.c | 2 ++ common/messages.h | 2 ++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/common/device-scan.c b/common/device-scan.c index c04c2388..39669ae1 100644 --- a/common/device-scan.c +++ b/common/device-scan.c @@ -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; diff --git a/common/messages.c b/common/messages.c index 2970122a..a349965b 100644 --- a/common/messages.c +++ b/common/messages.c @@ -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))) diff --git a/common/messages.h b/common/messages.h index 511b949c..d6d4044d 100644 --- a/common/messages.h +++ b/common/messages.h @@ -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)))