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 <dsterba@suse.com>
master
David Sterba 2016-01-12 13:35:50 +01:00
parent 633dc6f80f
commit ac4ec4d4f4
11 changed files with 24 additions and 20 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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;
}

View File

@ -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));

View File

@ -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)

View File

@ -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))

View File

@ -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));

View File

@ -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));

View File

@ -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;