From e578b59bf612178d7727c303102049e62676c308 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Thu, 25 Oct 2018 14:10:54 +0200 Subject: [PATCH] btrfs-progs: convert strerror to implicit %m Similar to the changes where strerror(errno) was converted, continue with the remaining cases where the argument was stored in another variable. The savings in object size are about 4500 bytes: $ size btrfs.old btrfs.new text data bss dec hex filename 805055 24248 19748 849051 cf49b btrfs.old 804527 24248 19748 848523 cf28b btrfs.new Signed-off-by: David Sterba --- btrfs-corrupt-block.c | 12 ++--- btrfs-find-root.c | 4 +- btrfs-list.c | 5 +- btrfs-map-logical.c | 25 +++++----- btrfs-select-super.c | 3 +- btrfstune.c | 4 +- check/main.c | 61 +++++++++++++----------- check/mode-common.c | 8 ++-- check/mode-lowmem.c | 43 +++++++++++------ cmds-device.c | 4 +- cmds-fi-du.c | 9 ++-- cmds-filesystem.c | 7 +-- cmds-inspect-dump-tree.c | 15 +++--- cmds-inspect-tree-stats.c | 4 +- cmds-inspect.c | 6 ++- cmds-property.c | 10 ++-- cmds-qgroup.c | 4 +- cmds-receive.c | 98 +++++++++++++++++---------------------- cmds-replace.c | 3 +- cmds-rescue.c | 12 +++-- cmds-restore.c | 9 ++-- cmds-scrub.c | 93 +++++++++++++++++++++---------------- cmds-send.c | 51 ++++++++++---------- cmds-subvolume.c | 10 ++-- convert/main.c | 58 +++++++++++++---------- convert/source-ext2.c | 4 +- convert/source-reiserfs.c | 6 +-- image/main.c | 23 +++++---- mkfs/common.c | 7 +-- mkfs/main.c | 3 +- mkfs/rootdir.c | 20 ++++---- props.c | 9 ++-- qgroup.c | 5 +- send-stream.c | 3 +- send-utils.c | 3 +- utils.c | 18 ++++--- volumes.c | 25 +++++----- 37 files changed, 376 insertions(+), 308 deletions(-) diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c index 3add8e63b7..ba46792141 100644 --- a/btrfs-corrupt-block.c +++ b/btrfs-corrupt-block.c @@ -70,9 +70,9 @@ static int debug_corrupt_block(struct extent_buffer *eb, if (!copy || mirror_num == copy) { ret = read_extent_from_disk(eb, 0, eb->len); if (ret < 0) { - error("cannot read eb bytenr %llu: %s", - (unsigned long long)eb->dev_bytenr, - strerror(-ret)); + errno = -ret; + error("cannot read eb bytenr %llu: %m", + (unsigned long long)eb->dev_bytenr); return ret; } printf("corrupting %llu copy %d\n", eb->start, @@ -80,9 +80,9 @@ static int debug_corrupt_block(struct extent_buffer *eb, memset(eb->data, 0, eb->len); ret = write_extent_to_disk(eb); if (ret < 0) { - error("cannot write eb bytenr %llu: %s", - (unsigned long long)eb->dev_bytenr, - strerror(-ret)); + errno = -ret; + error("cannot write eb bytenr %llu: %m", + (unsigned long long)eb->dev_bytenr); return ret; } fsync(eb->fd); diff --git a/btrfs-find-root.c b/btrfs-find-root.c index e2d2e70c40..4419873a8c 100644 --- a/btrfs-find-root.c +++ b/btrfs-find-root.c @@ -204,8 +204,8 @@ int main(int argc, char **argv) &filter.match_gen, &filter.match_level); ret = btrfs_find_root_search(fs_info, &filter, &result, &found); if (ret < 0) { - fprintf(stderr, "Fail to search the tree root: %s\n", - strerror(-ret)); + errno = -ret; + fprintf(stderr, "Fail to search the tree root: %m\n"); goto out; } if (ret > 0) { diff --git a/btrfs-list.c b/btrfs-list.c index 90c98be11a..adda45aba3 100644 --- a/btrfs-list.c +++ b/btrfs-list.c @@ -515,8 +515,9 @@ static int add_root(struct root_lookup *root_lookup, ret = root_tree_insert(root_lookup, ri); if (ret < 0) { - error("failed to insert subvolume %llu to tree: %s", - (unsigned long long)root_id, strerror(-ret)); + errno = -ret; + error("failed to insert subvolume %llu to tree: %m", + (unsigned long long)root_id); exit(1); } return 0; diff --git a/btrfs-map-logical.c b/btrfs-map-logical.c index cad4afc320..57278fe14e 100644 --- a/btrfs-map-logical.c +++ b/btrfs-map-logical.c @@ -113,9 +113,10 @@ static int __print_mapping_info(struct btrfs_fs_info *fs_info, u64 logical, ret = btrfs_map_block(fs_info, READ, logical + cur_offset, &cur_len, &multi, mirror_num, NULL); if (ret) { + errno = -ret; fprintf(info_file, - "Error: fails to map mirror%d logical %llu: %s\n", - mirror_num, logical, strerror(-ret)); + "Error: fails to map mirror%d logical %llu: %m\n", + mirror_num, logical); return ret; } for (i = 0; i < multi->num_stripes; i++) { @@ -173,17 +174,18 @@ static int write_extent_content(struct btrfs_fs_info *fs_info, int out_fd, ret = read_extent_data(fs_info, buffer, logical + cur_offset, &cur_len, mirror); if (ret < 0) { + errno = -ret; fprintf(stderr, - "Failed to read extent at [%llu, %llu]: %s\n", - logical, logical + length, strerror(-ret)); + "Failed to read extent at [%llu, %llu]: %m\n", + logical, logical + length); return ret; } ret = write(out_fd, buffer, cur_len); if (ret < 0 || ret != cur_len) { if (ret > 0) ret = -EINTR; - fprintf(stderr, "output file write failed: %s\n", - strerror(-ret)); + errno = -ret; + fprintf(stderr, "output file write failed: %m\n"); return ret; } cur_offset += cur_len; @@ -293,8 +295,9 @@ int main(int argc, char **argv) /* First find the nearest extent */ ret = map_one_extent(root->fs_info, &cur_logical, &cur_len, 0); if (ret < 0) { - fprintf(stderr, "Failed to find extent at [%llu,%llu): %s\n", - cur_logical, cur_logical + cur_len, strerror(-ret)); + errno = -ret; + fprintf(stderr, "Failed to find extent at [%llu,%llu): %m\n", + cur_logical, cur_logical + cur_len); goto out_close_fd; } /* @@ -305,10 +308,10 @@ int main(int argc, char **argv) if (ret > 0) { ret = map_one_extent(root->fs_info, &cur_logical, &cur_len, 1); if (ret < 0) { + errno = -ret; fprintf(stderr, - "Failed to find extent at [%llu,%llu): %s\n", - cur_logical, cur_logical + cur_len, - strerror(-ret)); + "Failed to find extent at [%llu,%llu): %m\n", + cur_logical, cur_logical + cur_len); goto out_close_fd; } if (ret > 0) { diff --git a/btrfs-select-super.c b/btrfs-select-super.c index e021221e28..0764bf01d3 100644 --- a/btrfs-select-super.c +++ b/btrfs-select-super.c @@ -77,7 +77,8 @@ int main(int argc, char **argv) radix_tree_init(); if((ret = check_mounted(argv[optind])) < 0) { - error("cannot check mount status: %s", strerror(-ret)); + errno = -ret; + error("cannot check mount status: %m"); return ret; } else if(ret) { error("%s is currently mounted, aborting", argv[optind]); diff --git a/btrfstune.c b/btrfstune.c index eb13b7be65..9d89193767 100644 --- a/btrfstune.c +++ b/btrfstune.c @@ -477,8 +477,8 @@ int main(int argc, char *argv[]) ret = check_mounted_where(fd, device, NULL, 0, NULL, SBREAD_IGNORE_FSID_MISMATCH); if (ret < 0) { - error("could not check mount status of %s: %s", device, - strerror(-ret)); + errno = -ret; + error("could not check mount status of %s: %m", device); close(fd); return 1; } else if (ret) { diff --git a/check/main.c b/check/main.c index 79d222d5bb..5733afb480 100644 --- a/check/main.c +++ b/check/main.c @@ -2291,9 +2291,9 @@ static int repair_inode_nlinks(struct btrfs_trans_handle *trans, ret = reset_nlink(trans, root, path, rec); if (ret < 0) { + errno = -ret; fprintf(stderr, - "Failed to reset nlink for inode %llu: %s\n", - rec->ino, strerror(-ret)); + "Failed to reset nlink for inode %llu: %m\n", rec->ino); goto out; } @@ -3170,8 +3170,8 @@ static int repair_btree(struct btrfs_root *root, trans = btrfs_start_transaction(root, 1); if (IS_ERR(trans)) { ret = PTR_ERR(trans); - fprintf(stderr, "Error starting transaction: %s\n", - strerror(-ret)); + errno = -ret; + fprintf(stderr, "Error starting transaction: %m\n"); return ret; } btrfs_init_path(&path); @@ -3363,8 +3363,8 @@ static int check_fs_root(struct btrfs_root *root, root->root_key.objectid); ret = repair_btree(root, &corrupt_blocks); if (ret < 0) - fprintf(stderr, "Failed to repair btree: %s\n", - strerror(-ret)); + errno = -ret; + fprintf(stderr, "Failed to repair btree: %m\n"); if (!ret) printf("Btree for root %llu is fixed\n", root->root_key.objectid); @@ -5177,18 +5177,20 @@ static int process_extent_item(struct btrfs_root *root, case BTRFS_TREE_BLOCK_REF_KEY: ret = add_tree_backref(extent_cache, key.objectid, 0, offset, 0); - if (ret < 0) + if (ret < 0) { + errno = -ret; error( - "add_tree_backref failed (extent items tree block): %s", - strerror(-ret)); + "add_tree_backref failed (extent items tree block): %m"); + } break; case BTRFS_SHARED_BLOCK_REF_KEY: ret = add_tree_backref(extent_cache, key.objectid, offset, 0, 0); - if (ret < 0) + if (ret < 0) { + errno = -ret; error( - "add_tree_backref failed (extent items shared block): %s", - strerror(-ret)); + "add_tree_backref failed (extent items shared block): %m"); + } break; case BTRFS_EXTENT_DATA_REF_KEY: dref = (struct btrfs_extent_data_ref *)(&iref->offset); @@ -5420,16 +5422,18 @@ static int check_space_cache(struct btrfs_root *root) if (btrfs_fs_compat_ro(root->fs_info, FREE_SPACE_TREE)) { ret = exclude_super_stripes(root, cache); if (ret) { - fprintf(stderr, "could not exclude super stripes: %s\n", - strerror(-ret)); + errno = -ret; + fprintf(stderr, + "could not exclude super stripes: %m\n"); error++; continue; } ret = load_free_space_tree(root->fs_info, cache); free_excluded_extents(root, cache); if (ret < 0) { - fprintf(stderr, "could not load free space tree: %s\n", - strerror(-ret)); + errno = -ret; + fprintf(stderr, + "could not load free space tree: %m\n"); error++; continue; } @@ -6122,19 +6126,21 @@ static int run_next_block(struct btrfs_root *root, if (key.type == BTRFS_TREE_BLOCK_REF_KEY) { ret = add_tree_backref(extent_cache, key.objectid, 0, key.offset, 0); - if (ret < 0) + if (ret < 0) { + errno = -ret; error( - "add_tree_backref failed (leaf tree block): %s", - strerror(-ret)); + "add_tree_backref failed (leaf tree block): %m"); + } continue; } if (key.type == BTRFS_SHARED_BLOCK_REF_KEY) { ret = add_tree_backref(extent_cache, key.objectid, key.offset, 0, 0); - if (ret < 0) + if (ret < 0) { + errno = -ret; error( - "add_tree_backref failed (leaf shared block): %s", - strerror(-ret)); + "add_tree_backref failed (leaf shared block): %m"); + } continue; } if (key.type == BTRFS_EXTENT_DATA_REF_KEY) { @@ -6236,9 +6242,9 @@ static int run_next_block(struct btrfs_root *root, ret = add_tree_backref(extent_cache, ptr, parent, owner, 1); if (ret < 0) { + errno = -ret; error( - "add_tree_backref failed (non-leaf block): %s", - strerror(-ret)); + "add_tree_backref failed (non-leaf block): %m"); continue; } @@ -9678,8 +9684,8 @@ int cmd_check(int argc, char **argv) ret = check_mounted(argv[optind]); if (!force) { if (ret < 0) { - error("could not check mount status: %s", - strerror(-ret)); + errno = -ret; + error("could not check mount status: %m"); err |= !!ret; goto err_out; } else if (ret) { @@ -9856,7 +9862,8 @@ int cmd_check(int argc, char **argv) task_stop(ctx.info); if (ret < 0) { err = !!ret; - error("failed to repair root items: %s", strerror(-ret)); + errno = -ret; + error("failed to repair root items: %m"); goto close_out; } if (repair) { diff --git a/check/mode-common.c b/check/mode-common.c index 15e2bbd1f3..005be8a365 100644 --- a/check/mode-common.c +++ b/check/mode-common.c @@ -450,7 +450,8 @@ int link_inode_to_lostfound(struct btrfs_trans_handle *trans, BTRFS_FIRST_FREE_OBJECTID, &lost_found_ino, mode); if (ret < 0) { - error("failed to create '%s' dir: %s", dir_name, strerror(-ret)); + errno = -ret; + error("failed to create '%s' dir: %m", dir_name); goto out; } ret = btrfs_add_link(trans, root, ino, lost_found_ino, @@ -474,8 +475,9 @@ int link_inode_to_lostfound(struct btrfs_trans_handle *trans, name_len, filetype, NULL, 1, 0); } if (ret < 0) { - error("failed to link the inode %llu to %s dir: %s", - ino, dir_name, strerror(-ret)); + errno = -ret; + error("failed to link the inode %llu to %s dir: %m", + ino, dir_name); goto out; } diff --git a/check/mode-lowmem.c b/check/mode-lowmem.c index 427b2fe4a9..14bbc9ee6c 100644 --- a/check/mode-lowmem.c +++ b/check/mode-lowmem.c @@ -284,7 +284,8 @@ static int modify_block_groups_cache(struct btrfs_fs_info *fs_info, u64 flags, btrfs_init_path(&path); ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0); if (ret < 0) { - error("fail to search block groups due to %s", strerror(-ret)); + errno = -ret; + error("fail to search block groups due to %m"); goto out; } @@ -341,19 +342,22 @@ static int create_chunk_and_block_group(struct btrfs_fs_info *fs_info, trans = btrfs_start_transaction(root, 1); if (IS_ERR(trans)) { ret = PTR_ERR(trans); - error("error starting transaction %s", strerror(-ret)); + errno = -ret; + error("error starting transaction %m"); return ret; } ret = btrfs_alloc_chunk(trans, fs_info, start, nbytes, flags); if (ret) { - error("fail to allocate new chunk %s", strerror(-ret)); + errno = -ret; + error("fail to allocate new chunk %m"); goto out; } ret = btrfs_make_block_group(trans, fs_info, 0, flags, *start, *nbytes); if (ret) { - error("fail to make block group for chunk %llu %llu %s", - *start, *nbytes, strerror(-ret)); + errno = -ret; + error("fail to make block group for chunk %llu %llu %m", + *start, *nbytes); goto out; } out: @@ -521,8 +525,10 @@ static int avoid_extents_overwrite(struct btrfs_fs_info *fs_info) "Try to exclude all metadata blcoks and extents, it may be slow\n"); ret = exclude_metadata_blocks(fs_info); out: - if (ret) - error("failed to avoid extents overwrite %s", strerror(-ret)); + if (ret) { + errno = -ret; + error("failed to avoid extents overwrite %m"); + } return ret; } @@ -552,7 +558,8 @@ static int repair_block_accounting(struct btrfs_fs_info *fs_info) trans = btrfs_start_transaction(root, 1); if (IS_ERR(trans)) { ret = PTR_ERR(trans); - error("fail to start transaction %s", strerror(-ret)); + errno = -ret; + error("fail to start transaction: %m"); return ret; } @@ -629,7 +636,8 @@ static int repair_tree_block_ref(struct btrfs_root *root, if (IS_ERR(trans)) { ret = PTR_ERR(trans); trans = NULL; - error("fail to start transaction %s", strerror(-ret)); + errno = -ret; + error("fail to start transaction: %m"); goto out; } /* insert an extent item */ @@ -701,9 +709,10 @@ static int repair_tree_block_ref(struct btrfs_root *root, btrfs_commit_transaction(trans, extent_root); btrfs_release_path(&path); if (ret) { + errno = -ret; error( - "failed to repair tree block ref start %llu root %llu due to %s", - bytenr, root->objectid, strerror(-ret)); + "failed to repair tree block ref start %llu root %llu due to %m", + bytenr, root->objectid); } else { printf("Added one tree block ref start %llu %s %llu\n", bytenr, parent ? "parent" : "root", @@ -3006,7 +3015,8 @@ static int repair_extent_data_item(struct btrfs_root *root, if (IS_ERR(trans)) { ret = PTR_ERR(trans); trans = NULL; - error("fail to start transaction %s", strerror(-ret)); + errno = -ret; + error("fail to start transaction: %m"); goto out; } /* insert an extent item */ @@ -3820,7 +3830,8 @@ static int repair_extent_item(struct btrfs_root *root, struct btrfs_path *path, trans = btrfs_start_transaction(extent_root, 1); if (IS_ERR(trans)) { ret = PTR_ERR(trans); - error("fail to start transaction %s", strerror(-ret)); + errno = -ret; + error("fail to start transaction: %m"); /* nothing happened */ ret = 0; goto out; @@ -4281,7 +4292,8 @@ static int repair_chunk_item(struct btrfs_root *chunk_root, trans = btrfs_start_transaction(extent_root, 1); if (IS_ERR(trans)) { ret = PTR_ERR(trans); - error("fail to start transaction %s", strerror(-ret)); + errno = -ret; + error("fail to start transaction: %m"); return ret; } @@ -4316,7 +4328,8 @@ static int delete_extent_tree_item(struct btrfs_root *root, trans = btrfs_start_transaction(root, 1); if (IS_ERR(trans)) { ret = PTR_ERR(trans); - error("fail to start transaction %s", strerror(-ret)); + errno = -ret; + error("fail to start transaction: %m"); goto out; } btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]); diff --git a/cmds-device.c b/cmds-device.c index 2a05f70a76..d3102ba7c1 100644 --- a/cmds-device.c +++ b/cmds-device.c @@ -442,8 +442,8 @@ static int cmd_device_stats(int argc, char **argv) ret = get_fs_info(dev_path, &fi_args, &di_args); if (ret) { - error("getting device info for %s failed: %s", dev_path, - strerror(-ret)); + errno = -ret; + error("getting device info for %s failed: %m", dev_path); err = 1; goto out; } diff --git a/cmds-fi-du.c b/cmds-fi-du.c index 4e639f6dc2..496a55b87e 100644 --- a/cmds-fi-du.c +++ b/cmds-fi-du.c @@ -406,9 +406,10 @@ static int du_walk_dir(struct du_dir_ctxt *ctxt, struct rb_root *shared_extents) ret = 0; continue; } else if (ret) { + errno = -ret; fprintf(stderr, - "failed to walk dir/file: %s :%s\n", - entry->d_name, strerror(-ret)); + "failed to walk dir/file: %s : %m\n", + entry->d_name); break; } @@ -601,8 +602,8 @@ int cmd_filesystem_du(int argc, char **argv) for (i = optind; i < argc; i++) { ret = du_add_file(argv[i], AT_FDCWD, NULL, NULL, NULL, 1); if (ret) { - error("cannot check space of '%s': %s", argv[i], - strerror(-ret)); + errno = -ret; + error("cannot check space of '%s': %m", argv[i]); err = 1; } diff --git a/cmds-filesystem.c b/cmds-filesystem.c index 06c8311bdf..d1af21eeca 100644 --- a/cmds-filesystem.c +++ b/cmds-filesystem.c @@ -145,7 +145,8 @@ static int cmd_filesystem_df(int argc, char **argv) print_df(sargs, unit_mode); free(sargs); } else { - error("get_df failed %s", strerror(-ret)); + errno = -ret; + error("get_df failed: %m"); } close_file_or_dir(fd, dirstream); @@ -1054,8 +1055,8 @@ static int cmd_filesystem_defrag(int argc, char **argv) break; } if (ret) { - error("defrag failed on %s: %s", argv[i], - strerror(defrag_err)); + errno = defrag_err; + error("defrag failed on %s: %m", argv[i]); goto next; } } diff --git a/cmds-inspect-dump-tree.c b/cmds-inspect-dump-tree.c index d84d52ddbd..563b4e3780 100644 --- a/cmds-inspect-dump-tree.c +++ b/cmds-inspect-dump-tree.c @@ -313,12 +313,13 @@ int cmd_inspect_dump_tree(int argc, char **argv) ret = check_arg_type(argv[optind]); if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) { - if (ret < 0) - error("invalid argument %s: %s", argv[optind], - strerror(-ret)); - else + if (ret < 0) { + errno = -ret; + error("invalid argument %s: %m", argv[optind]); + } else { error("not a block device or regular file: %s", argv[optind]); + } goto out; } @@ -432,9 +433,9 @@ int cmd_inspect_dump_tree(int argc, char **argv) key.type = BTRFS_ROOT_ITEM_KEY; ret = btrfs_search_slot(NULL, tree_root_scan, &key, &path, 0, 0); if (ret < 0) { - error("cannot read ROOT_ITEM from tree %llu: %s", - (unsigned long long)tree_root_scan->root_key.objectid, - strerror(-ret)); + errno = -ret; + error("cannot read ROOT_ITEM from tree %llu: %m", + (unsigned long long)tree_root_scan->root_key.objectid); goto close_root; } while (1) { diff --git a/cmds-inspect-tree-stats.c b/cmds-inspect-tree-stats.c index dfa34c52ff..0921a8b0b7 100644 --- a/cmds-inspect-tree-stats.c +++ b/cmds-inspect-tree-stats.c @@ -454,8 +454,8 @@ int cmd_inspect_tree_stats(int argc, char **argv) ret = check_mounted(argv[optind]); if (ret < 0) { - warning("unable to check mount status of: %s", - strerror(-ret)); + errno = -ret; + warning("unable to check mount status of: %m"); } else if (ret) { warning("%s already mounted, results may be inaccurate", argv[optind]); diff --git a/cmds-inspect.c b/cmds-inspect.c index ac77a5ee48..efea0331b7 100644 --- a/cmds-inspect.c +++ b/cmds-inspect.c @@ -330,7 +330,8 @@ static int cmd_inspect_rootid(int argc, char **argv) ret = lookup_path_rootid(fd, &rootid); if (ret) { - error("failed to lookup root id: %s", strerror(-ret)); + errno = -ret; + error("failed to lookup root id: %m"); goto out; } @@ -565,7 +566,8 @@ static int print_min_dev_size(int fd, u64 devid) ret = add_dev_extent(&holes, last_pos, btrfs_search_header_offset(sh) - 1, 1); if (ret) { - error("add device extent: %s", strerror(-ret)); + errno = -ret; + error("add device extent: %m"); ret = 1; goto out; } diff --git a/cmds-property.c b/cmds-property.c index 03bafa0547..7b0a6ffa29 100644 --- a/cmds-property.c +++ b/cmds-property.c @@ -91,7 +91,8 @@ static int check_is_root(const char *object) ret = get_fsid(object, fsid, 0); if (ret < 0) { - error("get_fsid for %s failed: %s", object, strerror(-ret)); + errno = -ret; + error("get_fsid for %s failed: %m", object); goto out; } @@ -103,7 +104,8 @@ static int check_is_root(const char *object) ret = 1; goto out; } else if (ret < 0) { - error("get_fsid for %s failed: %s", tmp, strerror(-ret)); + errno = -ret; + error("get_fsid for %s failed: %m", tmp); goto out; } @@ -317,8 +319,8 @@ static void parse_args(int argc, char **argv, if (!*types) { ret = autodetect_object_types(*object, types); if (ret < 0) { - error("failed to detect object type: %s", - strerror(-ret)); + errno = -ret; + error("failed to detect object type: %m"); usage(usage_str); } if (!*types) { diff --git a/cmds-qgroup.c b/cmds-qgroup.c index 10859c820f..9b202ef091 100644 --- a/cmds-qgroup.c +++ b/cmds-qgroup.c @@ -386,8 +386,8 @@ static int cmd_qgroup_show(int argc, char **argv) if (filter_flag) { ret = lookup_path_rootid(fd, &qgroupid); if (ret < 0) { - error("cannot resolve rootid for %s: %s", - path, strerror(-ret)); + errno = -ret; + error("cannot resolve rootid for %s: %m", path); close_file_or_dir(fd, dirstream); goto out; } diff --git a/cmds-receive.c b/cmds-receive.c index 34d51ef3f3..3888149ae8 100644 --- a/cmds-receive.c +++ b/cmds-receive.c @@ -103,8 +103,7 @@ static int finish_subvol(struct btrfs_receive *rctx) O_RDONLY | O_NOATIME); if (subvol_fd < 0) { ret = -errno; - error("cannot open %s: %s", - rctx->cur_subvol_path, strerror(-ret)); + error("cannot open %s: %m", rctx->cur_subvol_path); goto out; } @@ -121,8 +120,7 @@ static int finish_subvol(struct btrfs_receive *rctx) ret = ioctl(subvol_fd, BTRFS_IOC_SET_RECEIVED_SUBVOL, &rs_args); if (ret < 0) { ret = -errno; - error("ioctl BTRFS_IOC_SET_RECEIVED_SUBVOL failed: %s", - strerror(-ret)); + error("ioctl BTRFS_IOC_SET_RECEIVED_SUBVOL failed: %m"); goto out; } rctx->cur_subvol.rtransid = rs_args.rtransid; @@ -130,8 +128,7 @@ static int finish_subvol(struct btrfs_receive *rctx) ret = ioctl(subvol_fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags); if (ret < 0) { ret = -errno; - error("ioctl BTRFS_IOC_SUBVOL_GETFLAGS failed: %s", - strerror(-ret)); + error("ioctl BTRFS_IOC_SUBVOL_GETFLAGS failed: %m"); goto out; } @@ -140,8 +137,7 @@ static int finish_subvol(struct btrfs_receive *rctx) ret = ioctl(subvol_fd, BTRFS_IOC_SUBVOL_SETFLAGS, &flags); if (ret < 0) { ret = -errno; - error("failed to make subvolume read only: %s", - strerror(-ret)); + error("failed to make subvolume read only: %m"); goto out; } @@ -215,7 +211,7 @@ static int process_subvol(const char *path, const u8 *uuid, u64 ctransid, ret = ioctl(rctx->dest_dir_fd, BTRFS_IOC_SUBVOL_CREATE, &args_v1); if (ret < 0) { ret = -errno; - error("creating subvolume %s failed: %s", path, strerror(-ret)); + error("creating subvolume %s failed: %m", path); goto out; } @@ -360,8 +356,7 @@ static int process_snapshot(const char *path, const u8 *uuid, u64 ctransid, if (args_v2.fd < 0) { ret = -errno; if (errno != ENOENT) - error("cannot open %s: %s", - parent_subvol->path, strerror(-ret)); + error("cannot open %s: %m", parent_subvol->path); else fprintf(stderr, "It seems that you have changed your default " @@ -375,8 +370,8 @@ static int process_snapshot(const char *path, const u8 *uuid, u64 ctransid, close(args_v2.fd); if (ret < 0) { ret = -errno; - error("creating snapshot %s -> %s failed: %s", - parent_subvol->path, path, strerror(-ret)); + error("creating snapshot %s -> %s failed: %m", + parent_subvol->path, path); goto out; } @@ -406,7 +401,7 @@ static int process_mkfile(const char *path, void *user) ret = creat(full_path, 0600); if (ret < 0) { ret = -errno; - error("mkfile %s failed: %s", path, strerror(-ret)); + error("mkfile %s failed: %m", path); goto out; } close(ret); @@ -434,7 +429,7 @@ static int process_mkdir(const char *path, void *user) ret = mkdir(full_path, 0700); if (ret < 0) { ret = -errno; - error("mkdir %s failed: %s", path, strerror(-ret)); + error("mkdir %s failed: %m", path); } out: @@ -460,7 +455,7 @@ static int process_mknod(const char *path, u64 mode, u64 dev, void *user) ret = mknod(full_path, mode & S_IFMT, dev); if (ret < 0) { ret = -errno; - error("mknod %s failed: %s", path, strerror(-ret)); + error("mknod %s failed: %m", path); } out: @@ -485,7 +480,7 @@ static int process_mkfifo(const char *path, void *user) ret = mkfifo(full_path, 0600); if (ret < 0) { ret = -errno; - error("mkfifo %s failed: %s", path, strerror(-ret)); + error("mkfifo %s failed: %m", path); } out: @@ -510,7 +505,7 @@ static int process_mksock(const char *path, void *user) ret = mknod(full_path, 0600 | S_IFSOCK, 0); if (ret < 0) { ret = -errno; - error("mknod %s failed: %s", path, strerror(-ret)); + error("mknod %s failed: %m", path); } out: @@ -535,8 +530,7 @@ static int process_symlink(const char *path, const char *lnk, void *user) ret = symlink(lnk, full_path); if (ret < 0) { ret = -errno; - error("symlink %s -> %s failed: %s", path, - lnk, strerror(-ret)); + error("symlink %s -> %s failed: %m", path, lnk); } out: @@ -568,8 +562,7 @@ static int process_rename(const char *from, const char *to, void *user) ret = rename(full_from, full_to); if (ret < 0) { ret = -errno; - error("rename %s -> %s failed: %s", from, - to, strerror(-ret)); + error("rename %s -> %s failed: %m", from, to); } out: @@ -601,7 +594,7 @@ static int process_link(const char *path, const char *lnk, void *user) ret = link(full_link_path, full_path); if (ret < 0) { ret = -errno; - error("link %s -> %s failed: %s", path, lnk, strerror(-ret)); + error("link %s -> %s failed: %m", path, lnk); } out: @@ -627,7 +620,7 @@ static int process_unlink(const char *path, void *user) ret = unlink(full_path); if (ret < 0) { ret = -errno; - error("unlink %s failed. %s", path, strerror(-ret)); + error("unlink %s failed: %m", path); } out: @@ -652,7 +645,7 @@ static int process_rmdir(const char *path, void *user) ret = rmdir(full_path); if (ret < 0) { ret = -errno; - error("rmdir %s failed: %s", path, strerror(-ret)); + error("rmdir %s failed: %m", path); } out: @@ -673,7 +666,7 @@ static int open_inode_for_write(struct btrfs_receive *rctx, const char *path) rctx->write_fd = open(path, O_RDWR); if (rctx->write_fd < 0) { ret = -errno; - error("cannot open %s: %s", path, strerror(-ret)); + error("cannot open %s: %m", path); goto out; } strncpy_null(rctx->write_path, path); @@ -716,8 +709,7 @@ static int process_write(const char *path, const void *data, u64 offset, offset + pos); if (w < 0) { ret = -errno; - error("writing to %s failed: %s", - path, strerror(-ret)); + error("writing to %s failed: %m", path); goto out; } pos += w; @@ -812,7 +804,7 @@ static int process_clone(const char *path, u64 offset, u64 len, clone_fd = openat(rctx->mnt_fd, full_clone_path, O_RDONLY | O_NOATIME); if (clone_fd < 0) { ret = -errno; - error("cannot open %s: %s", full_clone_path, strerror(-ret)); + error("cannot open %s: %m", full_clone_path); goto out; } @@ -823,8 +815,7 @@ static int process_clone(const char *path, u64 offset, u64 len, ret = ioctl(rctx->write_fd, BTRFS_IOC_CLONE_RANGE, &clone_args); if (ret < 0) { ret = -errno; - error("failed to clone extents to %s\n%s", - path, strerror(-ret)); + error("failed to clone extents to %s: %m", path); goto out; } @@ -878,8 +869,8 @@ static int process_set_xattr(const char *path, const char *name, ret = lsetxattr(full_path, name, data, len, 0); if (ret < 0) { ret = -errno; - error("lsetxattr %s %s=%.*s failed: %s", - path, name, len, (char*)data, strerror(-ret)); + error("lsetxattr %s %s=%.*s failed: %m", + path, name, len, (char*)data); goto out; } @@ -907,8 +898,7 @@ static int process_remove_xattr(const char *path, const char *name, void *user) ret = lremovexattr(full_path, name); if (ret < 0) { ret = -errno; - error("lremovexattr %s %s failed: %s", - path, name, strerror(-ret)); + error("lremovexattr %s %s failed: %m", path, name); goto out; } @@ -934,7 +924,7 @@ static int process_truncate(const char *path, u64 size, void *user) ret = truncate(full_path, size); if (ret < 0) { ret = -errno; - error("truncate %s failed: %s", path, strerror(-ret)); + error("truncate %s failed: %m", path); goto out; } @@ -960,7 +950,7 @@ static int process_chmod(const char *path, u64 mode, void *user) ret = chmod(full_path, mode); if (ret < 0) { ret = -errno; - error("chmod %s failed: %s", path, strerror(-ret)); + error("chmod %s failed: %m", path); goto out; } @@ -987,7 +977,7 @@ static int process_chown(const char *path, u64 uid, u64 gid, void *user) ret = lchown(full_path, uid, gid); if (ret < 0) { ret = -errno; - error("chown %s failed: %s", path, strerror(-ret)); + error("chown %s failed: %m", path); goto out; } @@ -1002,8 +992,7 @@ static int process_chown(const char *path, u64 uid, u64 gid, void *user) rctx->cached_capabilities_len = 0; if (ret < 0) { ret = -errno; - error("restoring capabilities %s: %s", - path, strerror(-ret)); + error("restoring capabilities %s: %m", path); goto out; } } @@ -1035,8 +1024,7 @@ static int process_utimes(const char *path, struct timespec *at, ret = utimensat(AT_FDCWD, full_path, tv, AT_SYMLINK_NOFOLLOW); if (ret < 0) { ret = -errno; - error("utimes %s failed: %s", - path, strerror(-ret)); + error("utimes %s failed: %m", path); goto out; } @@ -1096,14 +1084,14 @@ static int do_receive(struct btrfs_receive *rctx, const char *tomnt, dest_dir_full_path = realpath(tomnt, NULL); if (!dest_dir_full_path) { ret = -errno; - error("realpath(%s) failed: %s", tomnt, strerror(-ret)); + error("realpath(%s) failed: %m", tomnt); goto out; } rctx->dest_dir_fd = open(dest_dir_full_path, O_RDONLY | O_NOATIME); if (rctx->dest_dir_fd < 0) { ret = -errno; - error("cannot open destination directory %s: %s", - dest_dir_full_path, strerror(-ret)); + error("cannot open destination directory %s: %m", + dest_dir_full_path); goto out; } @@ -1112,8 +1100,9 @@ static int do_receive(struct btrfs_receive *rctx, const char *tomnt, } else { ret = find_mount_root(dest_dir_full_path, &rctx->root_path); if (ret < 0) { - error("failed to determine mount point for %s: %s", - dest_dir_full_path, strerror(-ret)); + errno = -ret; + error("failed to determine mount point for %s: %m", + dest_dir_full_path); ret = -EINVAL; goto out; } @@ -1127,7 +1116,7 @@ static int do_receive(struct btrfs_receive *rctx, const char *tomnt, rctx->mnt_fd = open(rctx->root_path, O_RDONLY | O_NOATIME); if (rctx->mnt_fd < 0) { ret = -errno; - error("cannot open %s: %s", rctx->root_path, strerror(-ret)); + error("cannot open %s: %m", rctx->root_path); goto out; } @@ -1158,14 +1147,12 @@ static int do_receive(struct btrfs_receive *rctx, const char *tomnt, if (rctx->dest_dir_chroot) { if (chroot(dest_dir_full_path)) { ret = -errno; - error("failed to chroot to %s: %s", - dest_dir_full_path, strerror(-ret)); + error("failed to chroot to %s: %m", dest_dir_full_path); goto out; } if (chdir("/")) { ret = -errno; - error("failed to chdir to / after chroot: %s", - strerror(-ret)); + error("failed to chdir to / after chroot: %m"); goto out; } fprintf(stderr, "Chroot to %s\n", dest_dir_full_path); @@ -1345,9 +1332,10 @@ int cmd_receive(int argc, char **argv) dump_args.full_subvol_path[1] = '\0'; ret = btrfs_read_and_process_send_stream(receive_fd, &btrfs_print_send_ops, &dump_args, 0, 0); - if (ret < 0) - error("failed to dump the send stream: %s", - strerror(-ret)); + if (ret < 0) { + errno = -ret; + error("failed to dump the send stream: %m"); + } } else { ret = do_receive(&rctx, tomnt, realmnt, receive_fd, max_errors); } diff --git a/cmds-replace.c b/cmds-replace.c index 1fa802845f..b30e6c781e 100644 --- a/cmds-replace.c +++ b/cmds-replace.c @@ -208,7 +208,8 @@ static int cmd_replace_start(int argc, char **argv) ret = get_fs_info(path, &fi_args, &di_args); if (ret) { - error("failed to get device info: %s", strerror(-ret)); + errno = -ret; + error("failed to get device info: %m"); free(di_args); goto leave_with_error; } diff --git a/cmds-rescue.c b/cmds-rescue.c index 38c4ab9b2e..2bc50c0841 100644 --- a/cmds-rescue.c +++ b/cmds-rescue.c @@ -77,7 +77,8 @@ static int cmd_rescue_chunk_recover(int argc, char *argv[]) ret = check_mounted(file); if (ret < 0) { - error("could not check mount status: %s", strerror(-ret)); + errno = -ret; + error("could not check mount status: %m"); return 1; } else if (ret) { error("the device is busy"); @@ -142,7 +143,8 @@ static int cmd_rescue_super_recover(int argc, char **argv) dname = argv[optind]; ret = check_mounted(dname); if (ret < 0) { - error("could not check mount status: %s", strerror(-ret)); + errno = -ret; + error("could not check mount status: %m"); return 1; } else if (ret) { error("the device is busy"); @@ -175,7 +177,8 @@ static int cmd_rescue_zero_log(int argc, char **argv) devname = argv[optind]; ret = check_mounted(devname); if (ret < 0) { - error("could not check mount status: %s", strerror(-ret)); + errno = -ret; + error("could not check mount status: %m"); goto out; } else if (ret) { error("%s is currently mounted", devname); @@ -226,7 +229,8 @@ static int cmd_rescue_fix_device_size(int argc, char **argv) devname = argv[optind]; ret = check_mounted(devname); if (ret < 0) { - error("could not check mount status: %s", strerror(-ret)); + errno = -ret; + error("could not check mount status: %m"); goto out; } else if (ret) { error("%s is currently mounted", devname); diff --git a/cmds-restore.c b/cmds-restore.c index d12c1a9240..7a763018fe 100644 --- a/cmds-restore.c +++ b/cmds-restore.c @@ -1542,8 +1542,8 @@ int cmd_restore(int argc, char **argv) } if ((ret = check_mounted(argv[optind])) < 0) { - fprintf(stderr, "Could not check mount status: %s\n", - strerror(-ret)); + errno = -ret; + fprintf(stderr, "Could not check mount status: %m\n"); return 1; } else if (ret) { fprintf(stderr, "%s is currently mounted. Aborting.\n", argv[optind]); @@ -1591,8 +1591,9 @@ int cmd_restore(int argc, char **argv) key.offset = (u64)-1; root = btrfs_read_fs_root(orig_root->fs_info, &key); if (IS_ERR(root)) { - fprintf(stderr, "fail to read root %llu: %s\n", - root_objectid, strerror(-PTR_ERR(root))); + errno = -PTR_ERR(root); + fprintf(stderr, "fail to read root %llu: %m\n", + root_objectid); root = orig_root; ret = 1; goto out; diff --git a/cmds-scrub.c b/cmds-scrub.c index 6b909f2046..13cb18a3e3 100644 --- a/cmds-scrub.c +++ b/cmds-scrub.c @@ -1206,9 +1206,9 @@ static int scrub_start(int argc, char **argv, int resume) ret = get_fs_info(path, &fi_args, &di_args); if (ret) { + errno = -ret; error_on(!do_quiet, - "getting dev info for scrub failed: %s", - strerror(-ret)); + "getting dev info for scrub failed: %m"); err = 1; goto out; } @@ -1221,13 +1221,14 @@ static int scrub_start(int argc, char **argv, int resume) uuid_unparse(fi_args.fsid, fsid); fdres = scrub_open_file_r(SCRUB_DATA_FILE, fsid); if (fdres < 0 && fdres != -ENOENT) { - warning_on(!do_quiet, "failed to open status file: %s", - strerror(-fdres)); + errno = -fdres; + warning_on(!do_quiet, "failed to open status file: %m"); } else if (fdres >= 0) { past_scrubs = scrub_read_file(fdres, !do_quiet); - if (IS_ERR(past_scrubs)) - warning_on(!do_quiet, "failed to read status file: %s", - strerror(-PTR_ERR(past_scrubs))); + if (IS_ERR(past_scrubs)) { + errno = -PTR_ERR(past_scrubs); + warning_on(!do_quiet, "failed to read status file: %m"); + } close(fdres); } @@ -1273,8 +1274,8 @@ static int scrub_start(int argc, char **argv, int resume) devid = di_args[i].devid; ret = pthread_mutex_init(&sp[i].progress_mutex, NULL); if (ret) { - error_on(!do_quiet, "pthread_mutex_init failed: %s", - strerror(ret)); + errno = ret; + error_on(!do_quiet, "pthread_mutex_init failed: %m"); err = 1; goto out; } @@ -1359,9 +1360,9 @@ static int scrub_start(int argc, char **argv, int resume) ret = scrub_write_progress(&spc_write_mutex, fsid, sp, fi_args.num_devices); if (ret) { + errno = -ret; warning_on(!do_quiet, - "failed to write the progress status file: %s. Status recording disabled", - strerror(-ret)); + "failed to write the progress status file: %m. Status recording disabled"); do_record = 0; } } @@ -1418,9 +1419,12 @@ static int scrub_start(int argc, char **argv, int resume) ret = pthread_create(&t_devs[i], NULL, scrub_one_dev, &sp[i]); if (ret) { - if (do_print) - error("creating scrub_one_dev[%llu] thread failed: %s", - devid, strerror(ret)); + if (do_print) { + errno = ret; + error( + "creating scrub_one_dev[%llu] thread failed: %m", + devid); + } err = 1; goto out; } @@ -1434,9 +1438,10 @@ static int scrub_start(int argc, char **argv, int resume) spc.fi = &fi_args; ret = pthread_create(&t_prog, NULL, scrub_progress_cycle, &spc); if (ret) { - if (do_print) - error("creating progress thread failed: %s", - strerror(ret)); + if (do_print) { + errno = ret; + error("creating progress thread failed: %m"); + } err = 1; goto out; } @@ -1448,9 +1453,12 @@ static int scrub_start(int argc, char **argv, int resume) devid = di_args[i].devid; ret = pthread_join(t_devs[i], NULL); if (ret) { - if (do_print) - error("pthread_join failed for scrub_one_dev[%llu]: %s", - devid, strerror(ret)); + if (do_print) { + errno = ret; + error( + "pthread_join failed for scrub_one_dev[%llu]: %m", + devid); + } ++err; continue; } @@ -1465,11 +1473,13 @@ static int scrub_start(int argc, char **argv, int resume) ++err; break; default: - if (do_print) - error("scrubbing %s failed for device id %lld: ret=%d, errno=%d (%s)", - path, devid, - sp[i].ret, sp[i].ioctl_errno, - strerror(sp[i].ioctl_errno)); + if (do_print) { + errno = sp[i].ioctl_errno; + error( + "scrubbing %s failed for device id %lld: ret=%d, errno=%d (%m)", + path, devid, sp[i].ret, + sp[i].ioctl_errno); + } ++err; continue; } @@ -1511,21 +1521,23 @@ static int scrub_start(int argc, char **argv, int resume) /* check for errors from the handling of the progress thread */ if (do_print && ret) { - error("progress thread handling failed: %s", - strerror(ret)); + errno = ret; + error("progress thread handling failed: %m"); } /* check for errors returned from the progress thread itself */ - if (do_print && terr && terr != PTHREAD_CANCELED) - error("recording progress failed: %s", - strerror(-PTR_ERR(terr))); + if (do_print && terr && terr != PTHREAD_CANCELED) { + errno = -PTR_ERR(terr); + error("recording progress failed: %m"); + } if (do_record) { ret = scrub_write_progress(&spc_write_mutex, fsid, sp, fi_args.num_devices); - if (ret && do_print) - error("failed to record the result: %s", - strerror(-ret)); + if (ret && do_print) { + errno = -ret; + error("failed to record the result: %m"); + } } scrub_handle_sigint_child(-1); @@ -1701,8 +1713,8 @@ static int cmd_scrub_status(int argc, char **argv) ret = get_fs_info(path, &fi_args, &di_args); if (ret) { - error("getting dev info for scrub failed: %s", - strerror(-ret)); + errno = -ret; + error("getting dev info for scrub failed: %m"); err = 1; goto out; } @@ -1729,8 +1741,8 @@ static int cmd_scrub_status(int argc, char **argv) close(fdres); fdres = scrub_open_file_r(SCRUB_DATA_FILE, fsid); if (fdres < 0 && fdres != -ENOENT) { - warning("failed to open status file: %s", - strerror(-fdres)); + errno = -fdres; + warning("failed to open status file: %m"); err = 1; goto out; } @@ -1738,9 +1750,10 @@ static int cmd_scrub_status(int argc, char **argv) if (fdres >= 0) { past_scrubs = scrub_read_file(fdres, 1); - if (IS_ERR(past_scrubs)) - warning("failed to read status: %s", - strerror(-PTR_ERR(past_scrubs))); + if (IS_ERR(past_scrubs)) { + errno = -PTR_ERR(past_scrubs); + warning("failed to read status: %m"); + } } in_progress = is_scrub_running_in_kernel(fdmnt, di_args, fi_args.num_devices); diff --git a/cmds-send.c b/cmds-send.c index 16b9f8d2bf..b4fa8a3db0 100644 --- a/cmds-send.c +++ b/cmds-send.c @@ -281,8 +281,7 @@ static void *read_sent_data(void *arg) NULL, SEND_BUFFER_SIZE, SPLICE_F_MORE); if (sbytes < 0) { ret = -errno; - error("failed to read stream from kernel: %s", - strerror(-ret)); + error("failed to read stream from kernel: %m"); goto out; } if (!sbytes) { @@ -312,14 +311,14 @@ static int do_send(struct btrfs_send *send, u64 parent_root_id, subvol_fd = openat(send->mnt_fd, subvol, O_RDONLY | O_NOATIME); if (subvol_fd < 0) { ret = -errno; - error("cannot open %s: %s", subvol, strerror(-ret)); + error("cannot open %s: %m", subvol); goto out; } ret = pipe(pipefd); if (ret < 0) { ret = -errno; - error("pipe failed: %s", strerror(-ret)); + error("pipe failed: %m"); goto out; } @@ -331,7 +330,8 @@ static int do_send(struct btrfs_send *send, u64 parent_root_id, ret = pthread_create(&t_read, NULL, read_sent_data, send); if (ret) { ret = -ret; - error("thread setup failed: %s", strerror(-ret)); + errno = -ret; + error("thread setup failed: %m"); goto out; } @@ -346,7 +346,7 @@ static int do_send(struct btrfs_send *send, u64 parent_root_id, ret = ioctl(subvol_fd, BTRFS_IOC_SEND, &io_send); if (ret < 0) { ret = -errno; - error("send ioctl failed with %d: %s", ret, strerror(-ret)); + error("send ioctl failed with %d: %m", ret); if (ret == -EINVAL && (!is_first_subvol || !is_last_subvol)) fprintf(stderr, "Try upgrading your kernel or don't use -e.\n"); @@ -364,7 +364,8 @@ static int do_send(struct btrfs_send *send, u64 parent_root_id, ret = pthread_join(t_read, &t_err); if (ret) { ret = -ret; - error("pthread_join failed: %s", strerror(-ret)); + errno = -ret; + error("pthread_join failed: %m"); goto out; } if (t_err) { @@ -395,8 +396,8 @@ static int init_root_path(struct btrfs_send *sctx, const char *subvol) ret = find_mount_root(subvol, &sctx->root_path); if (ret < 0) { - error("failed to determine mount point for %s: %s", - subvol, strerror(-ret)); + errno = -ret; + error("failed to determine mount point for %s: %m", subvol); ret = -EINVAL; goto out; } @@ -409,14 +410,14 @@ static int init_root_path(struct btrfs_send *sctx, const char *subvol) sctx->mnt_fd = open(sctx->root_path, O_RDONLY | O_NOATIME); if (sctx->mnt_fd < 0) { ret = -errno; - error("cannot open '%s': %s", sctx->root_path, strerror(-ret)); + error("cannot open '%s': %m", sctx->root_path); goto out; } ret = subvol_uuid_search_init(sctx->mnt_fd, &sctx->sus); if (ret < 0) { - error("failed to initialize subvol search: %s", - strerror(-ret)); + errno = -ret; + error("failed to initialize subvol search: %m"); goto out; } @@ -434,15 +435,14 @@ static int is_subvol_ro(struct btrfs_send *sctx, const char *subvol) fd = openat(sctx->mnt_fd, subvol, O_RDONLY | O_NOATIME); if (fd < 0) { ret = -errno; - error("cannot open %s: %s", subvol, strerror(-ret)); + error("cannot open %s: %m", subvol); goto out; } ret = ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags); if (ret < 0) { ret = -errno; - error("failed to get flags for subvolume %s: %s", - subvol, strerror(-ret)); + error("failed to get flags for subvolume %s: %m", subvol); goto out; } @@ -535,7 +535,7 @@ int cmd_send(int argc, char **argv) subvol = realpath(optarg, NULL); if (!subvol) { ret = -errno; - error("realpath %s failed: %s\n", optarg, strerror(-ret)); + error("realpath %s failed: %m\n", optarg); goto out; } @@ -554,7 +554,8 @@ int cmd_send(int argc, char **argv) ret = add_clone_source(&send, root_id); if (ret < 0) { - error("cannot add clone source: %s", strerror(-ret)); + errno = -ret; + error("cannot add clone source: %m"); goto out; } free(subvol); @@ -578,7 +579,7 @@ int cmd_send(int argc, char **argv) snapshot_parent = realpath(optarg, NULL); if (!snapshot_parent) { ret = -errno; - error("realpath %s failed: %s", optarg, strerror(-ret)); + error("realpath %s failed: %m", optarg); goto out; } @@ -629,7 +630,7 @@ int cmd_send(int argc, char **argv) send.dump_fd = tmpfd; if (send.dump_fd == -1) { ret = -errno; - error("cannot create '%s': %s", outname, strerror(-ret)); + error("cannot create '%s': %m", outname); goto out; } } @@ -664,7 +665,8 @@ int cmd_send(int argc, char **argv) ret = add_clone_source(&send, parent_root_id); if (ret < 0) { - error("cannot add clone source: %s", strerror(-ret)); + errno = -ret; + error("cannot add clone source: %m"); goto out; } } @@ -680,8 +682,8 @@ int cmd_send(int argc, char **argv) ret = find_mount_root(subvol, &mount_root); if (ret < 0) { - error("find_mount_root failed on %s: %s", subvol, - strerror(-ret)); + errno = -ret; + error("find_mount_root failed on %s: %m", subvol); goto out; } if (ret > 0) { @@ -724,7 +726,7 @@ int cmd_send(int argc, char **argv) subvol = realpath(subvol, NULL); if (!subvol) { ret = -errno; - error("realpath %s failed: %s", argv[i], strerror(-ret)); + error("realpath %s failed: %m", argv[i]); goto out; } @@ -759,7 +761,8 @@ int cmd_send(int argc, char **argv) /* done with this subvol, so add it to the clone sources */ ret = add_clone_source(&send, root_id); if (ret < 0) { - error("cannot add clone source: %s", strerror(-ret)); + errno = -ret; + error("cannot add clone source: %m"); goto out; } free_send_info(&send); diff --git a/cmds-subvolume.c b/cmds-subvolume.c index 5a446c1ae2..a8395aac6a 100644 --- a/cmds-subvolume.c +++ b/cmds-subvolume.c @@ -136,7 +136,8 @@ static int cmd_subvol_create(int argc, char **argv) retval = 1; /* failure */ res = test_isdir(dst); if (res < 0 && res != -ENOENT) { - error("cannot access %s: %s", dst, strerror(-res)); + errno = -res; + error("cannot access %s: %m", dst); goto out; } if (res >= 0) { @@ -337,8 +338,8 @@ static int cmd_subvol_delete(int argc, char **argv) } else if (commit_mode == COMMIT_AFTER) { res = get_fsid(dname, fsid, 0); if (res < 0) { - error("unable to get fsid for '%s': %s", - path, strerror(-res)); + errno = -res; + error("unable to get fsid for '%s': %m", path); error( "delete succeeded but commit may not be done in the end"); ret = 1; @@ -690,7 +691,8 @@ static int cmd_subvol_snapshot(int argc, char **argv) res = test_isdir(dst); if (res < 0 && res != -ENOENT) { - error("cannot access %s: %s", dst, strerror(-res)); + errno = -res; + error("cannot access %s: %m", dst); goto out; } if (res == 0) { diff --git a/convert/main.c b/convert/main.c index 52c6d4eec4..5ebc4482fb 100644 --- a/convert/main.c +++ b/convert/main.c @@ -318,10 +318,12 @@ static int create_image_file_range(struct btrfs_trans_handle *trans, if (datacsum) { ret = csum_disk_extent(trans, root, bytenr, len); - if (ret < 0) + if (ret < 0) { + errno = -ret; error( - "failed to calculate csum for bytenr %llu len %llu: %s", - bytenr, len, strerror(-ret)); + "failed to calculate csum for bytenr %llu len %llu: %m", + bytenr, len); + } } *ret_len = len; return ret; @@ -771,27 +773,31 @@ static int create_image(struct btrfs_root *root, ret = btrfs_find_free_objectid(trans, root, BTRFS_FIRST_FREE_OBJECTID, &ino); if (ret < 0) { - error("failed to find free objectid for root %llu: %s", - root->root_key.objectid, strerror(-ret)); + errno = -ret; + error("failed to find free objectid for root %llu: %m", + root->root_key.objectid); goto out; } ret = btrfs_new_inode(trans, root, ino, 0400 | S_IFREG); if (ret < 0) { - error("failed to create new inode for root %llu: %s", - root->root_key.objectid, strerror(-ret)); + errno = -ret; + error("failed to create new inode for root %llu: %m", + root->root_key.objectid); goto out; } ret = btrfs_change_inode_flags(trans, root, ino, flags); if (ret < 0) { - error("failed to change inode flag for ino %llu root %llu: %s", - ino, root->root_key.objectid, strerror(-ret)); + errno = -ret; + error("failed to change inode flag for ino %llu root %llu: %m", + ino, root->root_key.objectid); goto out; } ret = btrfs_add_link(trans, root, ino, BTRFS_FIRST_FREE_OBJECTID, name, strlen(name), BTRFS_FT_REG_FILE, NULL, 1, 0); if (ret < 0) { - error("failed to link ino %llu to '/%s' in root %llu: %s", - ino, name, root->root_key.objectid, strerror(-ret)); + errno = -ret; + error("failed to link ino %llu to '/%s' in root %llu: %m", + ino, name, root->root_key.objectid); goto out; } @@ -1158,7 +1164,8 @@ static int do_convert(const char *devname, u32 convert_flags, u32 nodesize, ret = make_convert_btrfs(fd, &mkfs_cfg, &cctx); if (ret) { - error("unable to create initial ctree: %s", strerror(-ret)); + errno = -ret; + error("unable to create initial ctree: %m"); goto fail; } @@ -1376,8 +1383,8 @@ static int check_convert_image(struct btrfs_root *image_root, u64 ino, * So we only need to check if ret < 0 */ if (ret < 0) { - error("failed to iterate file extents at offset 0: %s", - strerror(-ret)); + errno = -ret; + error("failed to iterate file extents at offset 0: %m"); btrfs_release_path(&path); return ret; } @@ -1581,8 +1588,8 @@ static int do_rollback(const char *devname) ret = -ENOENT; goto close_fs; } else if (ret < 0) { - error("failed to find source fs image subvolume: %s", - strerror(-ret)); + errno = -ret; + error("failed to find source fs image subvolume: %m"); goto close_fs; } @@ -1593,8 +1600,8 @@ static int do_rollback(const char *devname) image_root = btrfs_read_fs_root(fs_info, &key); if (IS_ERR(image_root)) { ret = PTR_ERR(image_root); - error("failed to open convert image subvolume: %s", - strerror(-ret)); + errno = -ret; + error("failed to open convert image subvolume: %m"); goto close_fs; } @@ -1609,8 +1616,8 @@ static int do_rollback(const char *devname) ret = PTR_ERR(dir); else ret = -ENOENT; - error("failed to locate file %s: %s", image_name, - strerror(-ret)); + errno = -ret; + error("failed to locate file %s: %m", image_name); goto close_fs; } btrfs_dir_item_key_to_cpu(path.nodes[0], dir, &key); @@ -1623,7 +1630,8 @@ static int do_rollback(const char *devname) if (ret < 0) { btrfs_release_path(&path); - error("unable to find inode %llu: %s", ino, strerror(-ret)); + errno = -ret; + error("unable to find inode %llu: %m", ino); goto close_fs; } inode_item = btrfs_item_ptr(path.nodes[0], path.slots[0], @@ -1665,8 +1673,9 @@ static int do_rollback(const char *devname) ret = -errno; else ret = -EIO; - error("failed to recover range [%llu, %llu): %s", - range->start, real_size, strerror(-ret)); + errno = -ret; + error("failed to recover range [%llu, %llu): %m", + range->start, real_size); goto free_mem; } ret = 0; @@ -1828,7 +1837,8 @@ int main(int argc, char *argv[]) file = argv[optind]; ret = check_mounted(file); if (ret < 0) { - error("could not check mount status: %s", strerror(-ret)); + errno = -ret; + error("could not check mount status: %m"); return 1; } else if (ret) { error("%s is mounted", file); diff --git a/convert/source-ext2.c b/convert/source-ext2.c index a2af1212f2..a136e56528 100644 --- a/convert/source-ext2.c +++ b/convert/source-ext2.c @@ -162,8 +162,8 @@ static int ext2_read_used_space(struct btrfs_convert_context *cctx) } ret = __ext2_add_one_block(fs, block_bitmap, i, used_tree); if (ret < 0) { - error("fail to build used space tree, %s", - strerror(-ret)); + errno = -ret; + error("fail to build used space tree, %m"); break; } blk_itr += EXT2_CLUSTERS_PER_GROUP(fs->super); diff --git a/convert/source-reiserfs.c b/convert/source-reiserfs.c index e0b3b685f7..7a70caddfa 100644 --- a/convert/source-reiserfs.c +++ b/convert/source-reiserfs.c @@ -493,10 +493,10 @@ static int reiserfs_copy_dirent(reiserfs_filsys_t fs, ret = reiserfs_copy_meta(fs, root, dirent_data->convert_flags, deh_dirid, deh_objectid, &type); if (ret) { + errno = -ret; error( - "an error occured while converting \"%.*s\", reiserfs key [%u %u]: %s", - (int)len, name, deh_dirid, deh_objectid, - strerror(-ret)); + "an error occured while converting \"%.*s\", reiserfs key [%u %u]: %m", + (int)len, name, deh_dirid, deh_objectid); return ret; } trans = btrfs_start_transaction(root, 1); diff --git a/image/main.c b/image/main.c index 5b0050a0d7..c680ab19de 100644 --- a/image/main.c +++ b/image/main.c @@ -499,7 +499,8 @@ static int write_buffers(struct metadump_struct *md, u64 *next) } if (err) { - error("one of the threads failed: %s", strerror(-err)); + errno = -err; + error("one of the threads failed: %m"); goto out; } @@ -690,10 +691,12 @@ static int flush_pending(struct metadump_struct *md, int done) } if (md->num_items >= ITEMS_PER_CLUSTER || done) { ret = write_buffers(md, &start); - if (ret) - error("unable to write buffers: %s", strerror(-ret)); - else + if (ret) { + errno = -ret; + error("unable to write buffers: %m"); + } else { meta_cluster_init(md, start); + } } pthread_mutex_unlock(&md->mutex); return ret; @@ -2377,10 +2380,12 @@ static int update_disk_super_on_device(struct btrfs_fs_info *info, ret = pwrite64(fp, buf, BTRFS_SUPER_INFO_SIZE, BTRFS_SUPER_INFO_OFFSET); if (ret != BTRFS_SUPER_INFO_SIZE) { - if (ret < 0) - error("cannot write superblock: %s", strerror(ret)); - else + if (ret < 0) { + errno = ret; + error("cannot write superblock: %m"); + } else { error("cannot write superblock"); + } ret = -EIO; goto out; } @@ -2535,8 +2540,8 @@ int main(int argc, char *argv[]) if (create) { ret = check_mounted(source); if (ret < 0) { - warning("unable to check mount status of: %s", - strerror(-ret)); + errno = -ret; + warning("unable to check mount status of: %m"); } else if (ret) { warning("%s already mounted, results may be inaccurate", source); diff --git a/mkfs/common.c b/mkfs/common.c index 0ace262b0e..f7e3badcf2 100644 --- a/mkfs/common.c +++ b/mkfs/common.c @@ -714,7 +714,8 @@ int test_dev_for_mkfs(const char *file, int force_overwrite) ret = is_swap_device(file); if (ret < 0) { - error("checking status of %s: %s", file, strerror(-ret)); + errno = -ret; + error("checking status of %s: %m", file); return 1; } if (ret == 1) { @@ -760,8 +761,8 @@ int test_status_for_mkfs(const char *file, bool force_overwrite) } ret = check_mounted(file); if (ret < 0) { - error("cannot check mount status of %s: %s", file, - strerror(-ret)); + errno = -ret; + error("cannot check mount status of %s: %m", file); return 1; } if (ret == 1) { diff --git a/mkfs/main.c b/mkfs/main.c index b76462a735..b6748f7fe8 100644 --- a/mkfs/main.c +++ b/mkfs/main.c @@ -1094,7 +1094,8 @@ int main(int argc, char **argv) ret = make_btrfs(fd, &mkfs_cfg); if (ret) { - error("error during mkfs: %s", strerror(-ret)); + errno = -ret; + error("error during mkfs: %m"); goto error; } diff --git a/mkfs/rootdir.c b/mkfs/rootdir.c index 74c46241d3..8384a15192 100644 --- a/mkfs/rootdir.c +++ b/mkfs/rootdir.c @@ -261,8 +261,9 @@ static int add_xattr_item(struct btrfs_trans_handle *trans, cur_name_len, cur_value, ret, objectid); if (ret) { - error("inserting a xattr item failed for %s: %s", - file_name, strerror(-ret)); + errno = -ret; + error("inserting a xattr item failed for %s: %m", + file_name); } cur_name = strtok(next_location, &delimiter); @@ -858,8 +859,8 @@ static int set_device_size(struct btrfs_fs_info *fs_info, trans = btrfs_start_transaction(chunk_root, 1); if (IS_ERR(trans)) { ret = PTR_ERR(trans); - error("failed to start transaction: %d (%s)", ret, - strerror(-ret)); + errno = -ret; + error("failed to start transaction: %d (%m)", ret); return ret; } key.objectid = BTRFS_DEV_ITEMS_OBJECTID; @@ -887,9 +888,10 @@ static int set_device_size(struct btrfs_fs_info *fs_info, * super->dev_item */ ret = btrfs_commit_transaction(trans, chunk_root); - if (ret < 0) - error("failed to commit current transaction: %d (%s)", - ret, strerror(-ret)); + if (ret < 0) { + errno = -ret; + error("failed to commit current transaction: %d (%m)", ret); + } btrfs_release_path(&path); return ret; @@ -924,8 +926,8 @@ int btrfs_mkfs_shrink_fs(struct btrfs_fs_info *fs_info, u64 *new_size_ret, ret = get_device_extent_end(fs_info, 1, &new_size); if (ret < 0) { - error("failed to get minimal device size: %d (%s)", - ret, strerror(-ret)); + errno = -ret; + error("failed to get minimal device size: %d (%m)", ret); return ret; } diff --git a/props.c b/props.c index e4edba06e5..efa11180d4 100644 --- a/props.c +++ b/props.c @@ -110,7 +110,7 @@ static int prop_compression(enum prop_object_type type, fd = open_file_or_dir3(object, &dirstream, open_flags); if (fd == -1) { ret = -errno; - error("failed to open %s: %s", object, strerror(-ret)); + error("failed to open %s: %m", object); goto out; } @@ -133,8 +133,8 @@ static int prop_compression(enum prop_object_type type, if (sret < 0) { ret = -errno; if (ret != -ENOATTR) - error("failed to %s compression for %s: %s", - value ? "set" : "get", object, strerror(-ret)); + error("failed to %s compression for %s: %m", + value ? "set" : "get", object); else ret = 0; goto out; @@ -150,8 +150,7 @@ static int prop_compression(enum prop_object_type type, sret = fgetxattr(fd, xattr_name, buf, len); if (sret < 0) { ret = -errno; - error("failed to get compression for %s: %s", - object, strerror(-ret)); + error("failed to get compression for %s: %m", object); goto out; } fprintf(stdout, "compression=%.*s\n", (int)len, buf); diff --git a/qgroup.c b/qgroup.c index 2cfd6dbdd0..a035579e1f 100644 --- a/qgroup.c +++ b/qgroup.c @@ -610,8 +610,9 @@ static struct btrfs_qgroup *get_or_add_qgroup( ret = qgroup_tree_insert(qgroup_lookup, bq); if (ret) { - error("failed to insert %llu into tree: %s", - (unsigned long long)bq->qgroupid, strerror(-ret)); + errno = -ret; + error("failed to insert %llu into tree: %m", + (unsigned long long)bq->qgroupid); free(bq); return ERR_PTR(ret); } diff --git a/send-stream.c b/send-stream.c index 78f2571ae4..3b8e39c948 100644 --- a/send-stream.c +++ b/send-stream.c @@ -61,8 +61,7 @@ static int read_buf(struct btrfs_send_stream *sctx, char *buf, size_t len) rbytes = read(sctx->fd, buf + pos, len - pos); if (rbytes < 0) { ret = -errno; - error("read from stream failed: %s", - strerror(-ret)); + error("read from stream failed: %m"); goto out; } if (rbytes == 0) { diff --git a/send-utils.c b/send-utils.c index 3ecbdea692..31ac9a7a42 100644 --- a/send-utils.c +++ b/send-utils.c @@ -40,8 +40,7 @@ static int btrfs_get_root_id_by_sub_path(int mnt_fd, const char *sub_path, subvol_fd = openat(mnt_fd, sub_path, O_RDONLY); if (subvol_fd < 0) { ret = -errno; - fprintf(stderr, "ERROR: open %s failed. %s\n", sub_path, - strerror(-ret)); + fprintf(stderr, "ERROR: open %s failed: %m\n", sub_path); return ret; } diff --git a/utils.c b/utils.c index 785c20225b..a310300829 100644 --- a/utils.c +++ b/utils.c @@ -357,7 +357,8 @@ int btrfs_prepare_device(int fd, const char *file, u64 *block_count_ret, ZERO_DEV_BYTES, block_count); if (ret < 0) { - error("failed to zero device '%s': %s", file, strerror(-ret)); + errno = -ret; + error("failed to zero device '%s': %m", file); return 1; } @@ -530,7 +531,8 @@ int get_btrfs_mount(const char *dev, char *mp, size_t mp_size) error("not a block device: %s", dev); ret = -EINVAL; } else { - error("cannot check %s: %s", dev, strerror(-ret)); + errno = -ret; + error("cannot check %s: %m", dev); } goto out; } @@ -1492,8 +1494,10 @@ u64 parse_qgroupid(const char *p) if (fd < 0) goto err; ret = lookup_path_rootid(fd, &id); - if (ret) - error("failed to lookup root id: %s", strerror(-ret)); + if (ret) { + errno = -ret; + error("failed to lookup root id: %m"); + } close(fd); if (ret < 0) goto err; @@ -1782,8 +1786,7 @@ int get_fsid(const char *path, u8 *fsid, int silent) if (fd < 0) { ret = -errno; if (!silent) - error("failed to open %s: %s", path, - strerror(-ret)); + error("failed to open %s: %m", path); goto out; } @@ -2000,7 +2003,8 @@ int btrfs_scan_devices(void) &num_devices, BTRFS_SUPER_INFO_OFFSET, SBREAD_DEFAULT); if (ret) { - error("cannot scan %s: %s", path, strerror(-ret)); + errno = -ret; + error("cannot scan %s: %m", path); close (fd); continue; } diff --git a/volumes.c b/volumes.c index f7a413b71d..30090ce5f8 100644 --- a/volumes.c +++ b/volumes.c @@ -1926,8 +1926,9 @@ static int read_one_chunk(struct btrfs_fs_info *fs_info, struct btrfs_key *key, } ret = insert_cache_extent(&map_tree->cache_tree, &map->ce); if (ret < 0) { - error("failed to add chunk map start=%llu len=%llu: %d (%s)", - map->ce.start, map->ce.size, ret, strerror(-ret)); + errno = -ret; + error("failed to add chunk map start=%llu len=%llu: %d (%m)", + map->ce.start, map->ce.size, ret); } return ret; @@ -2448,8 +2449,8 @@ int btrfs_fix_device_size(struct btrfs_fs_info *fs_info, trans = btrfs_start_transaction(chunk_root, 1); if (IS_ERR(trans)) { ret = PTR_ERR(trans); - error("error starting transaction: %d (%s)", - ret, strerror(-ret)); + errno = -ret; + error("error starting transaction: %d (%m)", ret); return ret; } @@ -2461,8 +2462,8 @@ int btrfs_fix_device_size(struct btrfs_fs_info *fs_info, goto err; } if (ret < 0) { - error("failed to search chunk root: %d (%s)", - ret, strerror(-ret)); + errno = -ret; + error("failed to search chunk root: %d (%m)", ret); goto err; } di = btrfs_item_ptr(path.nodes[0], path.slots[0], struct btrfs_dev_item); @@ -2470,8 +2471,8 @@ int btrfs_fix_device_size(struct btrfs_fs_info *fs_info, btrfs_mark_buffer_dirty(path.nodes[0]); ret = btrfs_commit_transaction(trans, chunk_root); if (ret < 0) { - error("failed to commit current transaction: %d (%s)", - ret, strerror(-ret)); + errno = -ret; + error("failed to commit current transaction: %d (%m)", ret); btrfs_release_path(&path); return ret; } @@ -2524,14 +2525,14 @@ int btrfs_fix_super_size(struct btrfs_fs_info *fs_info) trans = btrfs_start_transaction(fs_info->tree_root, 1); if (IS_ERR(trans)) { ret = PTR_ERR(trans); - error("error starting transaction: %d (%s)", - ret, strerror(-ret)); + errno = -ret; + error("error starting transaction: %d (%m)", ret); return ret; } ret = btrfs_commit_transaction(trans, fs_info->tree_root); if (ret < 0) { - error("failed to commit current transaction: %d (%s)", - ret, strerror(-ret)); + errno = -ret; + error("failed to commit current transaction: %d (%m)", ret); return ret; } printf("Fixed super total bytes, old size: %llu new size: %llu\n",