From ac4ec4d4f44e67fc3d1fd806cf7acbe2485cedee Mon Sep 17 00:00:00 2001 From: David Sterba Date: Tue, 12 Jan 2016 13:35:50 +0100 Subject: btrfs-progs: check for negative return value from ioctl Handle only negative values returned by ioctl syscalls, with exception of the device remove. It returns positive values that are handled later. Signed-off-by: David Sterba --- btrfs-list.c | 6 +++--- cmds-device.c | 4 ++++ cmds-fi-usage.c | 4 ++-- cmds-filesystem.c | 4 ++-- cmds-inspect.c | 4 ++-- cmds-receive.c | 2 +- cmds-replace.c | 8 ++++---- cmds-send.c | 2 +- send-test.c | 2 +- send-utils.c | 4 ++-- utils.c | 4 ++-- 11 files changed, 24 insertions(+), 20 deletions(-) diff --git a/btrfs-list.c b/btrfs-list.c index f084b32..2da54bf 100644 --- a/btrfs-list.c +++ b/btrfs-list.c @@ -643,7 +643,7 @@ static int lookup_ino_path(int fd, struct root_info *ri) args.objectid = ri->dir_id; ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args); - if (ret) { + if (ret < 0) { if (errno == ENOENT) { ri->ref_tree = 0; return -ENOENT; @@ -699,7 +699,7 @@ static u64 find_root_gen(int fd) /* this ioctl fills in ino_args->treeid */ ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_args); - if (ret) { + if (ret < 0) { fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n", (unsigned long long)BTRFS_FIRST_FREE_OBJECTID, strerror(errno)); @@ -786,7 +786,7 @@ static char *__ino_resolve(int fd, u64 dirid) args.objectid = dirid; ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args); - if (ret) { + if (ret < 0) { fprintf(stderr, "ERROR: Failed to lookup path for dirid %llu - %s\n", (unsigned long long)dirid, strerror(errno)); return ERR_PTR(ret); diff --git a/cmds-device.c b/cmds-device.c index f69d024..50c1c5d 100644 --- a/cmds-device.c +++ b/cmds-device.c @@ -166,6 +166,10 @@ static int _cmd_device_remove(int argc, char **argv, } memset(&arg, 0, sizeof(arg)); strncpy_null(arg.name, argv[i]); + /* + * Positive values are from BTRFS_ERROR_DEV_*, + * otherwise it's a generic error, one of errnos + */ res = ioctl(fdmnt, BTRFS_IOC_RM_DEV, &arg); if (res) { const char *msg; diff --git a/cmds-fi-usage.c b/cmds-fi-usage.c index 356ed04..c91c2ca 100644 --- a/cmds-fi-usage.c +++ b/cmds-fi-usage.c @@ -240,7 +240,7 @@ static struct btrfs_ioctl_space_args *load_space_info(int fd, char *path) sargs->total_spaces = 0; ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs); - if (ret) { + if (ret < 0) { error("cannot get space info on '%s': %s", path, strerror(errno)); free(sargs); @@ -266,7 +266,7 @@ static struct btrfs_ioctl_space_args *load_space_info(int fd, char *path) sargs->total_spaces = 0; ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs); - if (ret) { + if (ret < 0) { error("cannot get space info with %u slots: %s", count, strerror(errno)); free(sargs); diff --git a/cmds-filesystem.c b/cmds-filesystem.c index 29ed7cb..4c6e856 100644 --- a/cmds-filesystem.c +++ b/cmds-filesystem.c @@ -141,7 +141,7 @@ static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret) sargs->total_spaces = 0; ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs); - if (ret) { + if (ret < 0) { error("cannot get space info: %s\n", strerror(errno)); free(sargs); return -errno; @@ -162,7 +162,7 @@ static int get_df(int fd, struct btrfs_ioctl_space_args **sargs_ret) sargs->space_slots = count; sargs->total_spaces = 0; ret = ioctl(fd, BTRFS_IOC_SPACE_INFO, sargs); - if (ret) { + if (ret < 0) { error("cannot get space info with %llu slots: %s", count, strerror(errno)); free(sargs); diff --git a/cmds-inspect.c b/cmds-inspect.c index 932765b..7fa4881 100644 --- a/cmds-inspect.c +++ b/cmds-inspect.c @@ -50,7 +50,7 @@ static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend) ipa.fspath = ptr_to_u64(fspath); ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa); - if (ret) { + if (ret < 0) { printf("ioctl ret=%d, error: %s\n", ret, strerror(errno)); goto out; } @@ -189,7 +189,7 @@ static int cmd_inspect_logical_resolve(int argc, char **argv) } ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi); - if (ret) { + if (ret < 0) { printf("ioctl ret=%d, error: %s\n", ret, strerror(errno)); goto out; } diff --git a/cmds-receive.c b/cmds-receive.c index 6d50cd3..094a607 100644 --- a/cmds-receive.c +++ b/cmds-receive.c @@ -771,7 +771,7 @@ static int process_clone(const char *path, u64 offset, u64 len, clone_args.src_length = len; clone_args.dest_offset = offset; ret = ioctl(r->write_fd, BTRFS_IOC_CLONE_RANGE, &clone_args); - if (ret) { + if (ret < 0) { ret = -errno; error("failed to clone extents to %s\n%s\n", path, strerror(-ret)); diff --git a/cmds-replace.c b/cmds-replace.c index 1ff028a..6036e2f 100644 --- a/cmds-replace.c +++ b/cmds-replace.c @@ -166,7 +166,7 @@ static int cmd_replace_start(int argc, char **argv) status_args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS; status_args.result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT; ret = ioctl(fdmnt, BTRFS_IOC_DEV_REPLACE, &status_args); - if (ret) { + if (ret < 0) { fprintf(stderr, "ERROR: ioctl(DEV_REPLACE_STATUS) failed on \"%s\": %s", path, strerror(errno)); @@ -276,7 +276,7 @@ static int cmd_replace_start(int argc, char **argv) start_args.result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT; ret = ioctl(fdmnt, BTRFS_IOC_DEV_REPLACE, &start_args); if (do_not_background) { - if (ret) { + if (ret < 0) { fprintf(stderr, "ERROR: ioctl(DEV_REPLACE_START) failed on \"%s\": %s", path, strerror(errno)); @@ -372,7 +372,7 @@ static int print_replace_status(int fd, const char *path, int once) args.cmd = BTRFS_IOCTL_DEV_REPLACE_CMD_STATUS; args.result = BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT; ret = ioctl(fd, BTRFS_IOC_DEV_REPLACE, &args); - if (ret) { + if (ret < 0) { fprintf(stderr, "ERROR: ioctl(DEV_REPLACE_STATUS) failed on \"%s\": %s", path, strerror(errno)); if (args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT) @@ -522,7 +522,7 @@ static int cmd_replace_cancel(int argc, char **argv) ret = ioctl(fd, BTRFS_IOC_DEV_REPLACE, &args); e = errno; close_file_or_dir(fd, dirstream); - if (ret) { + if (ret < 0) { fprintf(stderr, "ERROR: ioctl(DEV_REPLACE_CANCEL) failed on \"%s\": %s", path, strerror(e)); if (args.result != BTRFS_IOCTL_DEV_REPLACE_RESULT_NO_RESULT) diff --git a/cmds-send.c b/cmds-send.c index 0dd493a..478ace1 100644 --- a/cmds-send.c +++ b/cmds-send.c @@ -293,7 +293,7 @@ static int do_send(struct btrfs_send *send, u64 parent_root_id, if (!is_last_subvol) io_send.flags |= BTRFS_SEND_FLAG_OMIT_END_CMD; ret = ioctl(subvol_fd, BTRFS_IOC_SEND, &io_send); - if (ret) { + if (ret < 0) { ret = -errno; error("send ioctl failed with %d: %s", ret, strerror(-ret)); if (ret == -EINVAL && (!is_first_subvol || !is_last_subvol)) diff --git a/send-test.c b/send-test.c index af8229e..4645b89 100644 --- a/send-test.c +++ b/send-test.c @@ -420,7 +420,7 @@ int main(int argc, char **argv) io_send.flags = BTRFS_SEND_FLAG_NO_FILE_DATA; ret = ioctl(subvol_fd, BTRFS_IOC_SEND, &io_send); - if (ret) { + if (ret < 0) { ret = errno; fprintf(stderr, "ERROR: send ioctl failed with %d: %s\n", ret, strerror(ret)); diff --git a/send-utils.c b/send-utils.c index 7dbb0b8..3c369b8 100644 --- a/send-utils.c +++ b/send-utils.c @@ -265,7 +265,7 @@ static int btrfs_subvolid_resolve_sub(int fd, char *path, size_t *path_len, search_arg.key.max_transid = (u64)-1; search_arg.key.nr_items = 1; ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &search_arg); - if (ret) { + if (ret < 0) { fprintf(stderr, "ioctl(BTRFS_IOC_TREE_SEARCH, subvol_id %llu) ret=%d, error: %s\n", (unsigned long long)subvol_id, ret, strerror(errno)); @@ -302,7 +302,7 @@ static int btrfs_subvolid_resolve_sub(int fd, char *path, size_t *path_len, ino_lookup_arg.objectid = btrfs_stack_root_ref_dirid(backref_item); ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &ino_lookup_arg); - if (ret) { + if (ret < 0) { fprintf(stderr, "ioctl(BTRFS_IOC_INO_LOOKUP) ret=%d, error: %s\n", ret, strerror(errno)); diff --git a/utils.c b/utils.c index 0339894..74bb066 100644 --- a/utils.c +++ b/utils.c @@ -2077,7 +2077,7 @@ int get_device_info(int fd, u64 devid, memset(&di_args->uuid, '\0', sizeof(di_args->uuid)); ret = ioctl(fd, BTRFS_IOC_DEV_INFO, di_args); - return ret ? -errno : 0; + return ret < 0 ? -errno : 0; } static u64 find_max_device_id(struct btrfs_ioctl_search_args *search_args, @@ -2700,7 +2700,7 @@ int lookup_ino_rootid(int fd, u64 *rootid) args.objectid = BTRFS_FIRST_FREE_OBJECTID; ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &args); - if (ret) { + if (ret < 0) { fprintf(stderr, "ERROR: Failed to lookup root id - %s\n", strerror(errno)); return ret; -- cgit v1.2.1