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 <dsterba@suse.com>
This commit is contained in:
David Sterba 2023-12-08 22:50:57 +01:00
parent 4576029dfd
commit 87dba20daf
3 changed files with 19 additions and 7 deletions

View File

@ -500,6 +500,7 @@ u64 device_get_zone_size(int fd, const char *name)
/* /sys/fs/btrfs/FSID/devices/NAME/queue/chunk_sectors */ /* /sys/fs/btrfs/FSID/devices/NAME/queue/chunk_sectors */
queue_fd = sysfs_open_fsid_file(fd, queue); queue_fd = sysfs_open_fsid_file(fd, queue);
if (queue_fd < 0) { if (queue_fd < 0) {
queue_fd = -1;
ret = 0; ret = 0;
break; break;
} }

View File

@ -42,7 +42,8 @@ static int sysfs_open_fsid_file_flags(int fd, const char *filename, int flags)
if (ret < 0) if (ret < 0)
return ret; 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) 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); ret = path_cat_out(path, "/sys/fs/btrfs", name);
if (ret < 0) if (ret < 0)
return ret; return ret;
return open(path, flags); ret = open(path, flags);
return (ret < 0 ? -errno : ret);
} }
int sysfs_open_file(const char *name) int sysfs_open_file(const char *name)
@ -101,7 +103,8 @@ int sysfs_open_fsid_dir(int fd, const char *dirname)
if (ret < 0) if (ret < 0)
return ret; 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 sysfs_read_file(int fd, char *buf, size_t size)
{ {
int ret;
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
memset(buf, 0, size); 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 sysfs_write_file(int fd, const char *buf, size_t size)
{ {
int ret;
lseek(fd, 0, SEEK_SET); 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) 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. */ /* Raw value in any numeric format should work, followed by a newline. */
errno = 0; errno = 0;
*value = strtoull(str, NULL, 0); *value = strtoull(str, NULL, 0);
ret = -errno;
out: out:
close(fd); close(fd);
return ret; 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. */ /* Raw value in any numeric format should work, followed by a newline. */
errno = 0; errno = 0;
*value = strtoull(str, NULL, 0); *value = strtoull(str, NULL, 0);
ret = -errno;
out: out:
close(fd); close(fd);
return ret; return ret;

View File

@ -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"); sysfs_fd = sysfs_open_fsid_file(fd, "exclusive_operation");
if (sysfs_fd < 0) { if (sysfs_fd < 0) {
if (errno == ENOENT) if (sysfs_fd == -ENOENT)
return 0; return 0;
return -errno; return sysfs_fd;
} }
exclop = get_fs_exclop(fd); exclop = get_fs_exclop(fd);