From 87dba20daf84e6f6b0cbc77bc2f231660918be11 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Fri, 8 Dec 2023 22:50:57 +0100 Subject: [PATCH] btrfs-progs: change all sysfs helpers to return errno To be consistent with the rest of the code the sysfs helper should return the -errno instead of passing -1 from various syscalls. Update callers that relied on -1 as the invalid file descriptor. Signed-off-by: David Sterba --- common/device-utils.c | 1 + common/sysfs-utils.c | 21 ++++++++++++++++----- common/utils.c | 4 ++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/common/device-utils.c b/common/device-utils.c index 680fba6f..f86120af 100644 --- a/common/device-utils.c +++ b/common/device-utils.c @@ -500,6 +500,7 @@ u64 device_get_zone_size(int fd, const char *name) /* /sys/fs/btrfs/FSID/devices/NAME/queue/chunk_sectors */ queue_fd = sysfs_open_fsid_file(fd, queue); if (queue_fd < 0) { + queue_fd = -1; ret = 0; break; } diff --git a/common/sysfs-utils.c b/common/sysfs-utils.c index 4c616cb8..d7634987 100644 --- a/common/sysfs-utils.c +++ b/common/sysfs-utils.c @@ -42,7 +42,8 @@ static int sysfs_open_fsid_file_flags(int fd, const char *filename, int flags) if (ret < 0) return ret; - return open(sysfs_file, flags); + ret = open(sysfs_file, flags); + return (ret < 0 ? -errno : ret); } int sysfs_open_fsid_file(int fd, const char *filename) @@ -67,7 +68,8 @@ static int sysfs_open_file_flags(const char *name, int flags) ret = path_cat_out(path, "/sys/fs/btrfs", name); if (ret < 0) return ret; - return open(path, flags); + ret = open(path, flags); + return (ret < 0 ? -errno : ret); } int sysfs_open_file(const char *name) @@ -101,7 +103,8 @@ int sysfs_open_fsid_dir(int fd, const char *dirname) if (ret < 0) return ret; - return open(sysfs_file, O_DIRECTORY | O_RDONLY); + ret = open(sysfs_file, O_DIRECTORY | O_RDONLY); + return (ret < 0 ? -errno : ret); } /* @@ -109,15 +112,21 @@ int sysfs_open_fsid_dir(int fd, const char *dirname) */ int sysfs_read_file(int fd, char *buf, size_t size) { + int ret; + lseek(fd, 0, SEEK_SET); memset(buf, 0, size); - return read(fd, buf, size); + ret = read(fd, buf, size); + return (ret < 0 ? -errno : ret); } int sysfs_write_file(int fd, const char *buf, size_t size) { + int ret; + lseek(fd, 0, SEEK_SET); - return write(fd, buf, size); + ret = write(fd, buf, size); + return (ret < 0 ? -errno : ret); } int sysfs_read_file_u64(const char *name, u64 *value) @@ -136,6 +145,7 @@ int sysfs_read_file_u64(const char *name, u64 *value) /* Raw value in any numeric format should work, followed by a newline. */ errno = 0; *value = strtoull(str, NULL, 0); + ret = -errno; out: close(fd); return ret; @@ -172,6 +182,7 @@ int sysfs_read_fsid_file_u64(int fd, const char *name, u64 *value) /* Raw value in any numeric format should work, followed by a newline. */ errno = 0; *value = strtoull(str, NULL, 0); + ret = -errno; out: close(fd); return ret; diff --git a/common/utils.c b/common/utils.c index 035f2705..62f0e3f4 100644 --- a/common/utils.c +++ b/common/utils.c @@ -1293,9 +1293,9 @@ int check_running_fs_exclop(int fd, enum exclusive_operation start, bool enqueue sysfs_fd = sysfs_open_fsid_file(fd, "exclusive_operation"); if (sysfs_fd < 0) { - if (errno == ENOENT) + if (sysfs_fd == -ENOENT) return 0; - return -errno; + return sysfs_fd; } exclop = get_fs_exclop(fd);