btrfs-progs: typo review of strings and comments
[platform/upstream/btrfs-progs.git] / cmds-inspect.c
index 1823584..dd7b9dd 100644 (file)
@@ -21,6 +21,7 @@
 #include <sys/ioctl.h>
 #include <errno.h>
 #include <getopt.h>
+#include <limits.h>
 
 #include "kerncompat.h"
 #include "ioctl.h"
@@ -30,6 +31,9 @@
 #include "disk-io.h"
 #include "commands.h"
 #include "btrfs-list.h"
+#include "cmds-inspect-dump-tree.h"
+#include "cmds-inspect-dump-super.h"
+#include "cmds-inspect-tree-stats.h"
 
 static const char * const inspect_cmd_group_usage[] = {
        "btrfs inspect-internal <command> <args>",
@@ -41,20 +45,16 @@ static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
        int ret;
        int i;
        struct btrfs_ioctl_ino_path_args ipa;
-       struct btrfs_data_container *fspath;
-
-       fspath = malloc(4096);
-       if (!fspath)
-               return -ENOMEM;
+       struct btrfs_data_container fspath[PATH_MAX];
 
        memset(fspath, 0, sizeof(*fspath));
        ipa.inum = inum;
-       ipa.size = 4096;
+       ipa.size = PATH_MAX;
        ipa.fspath = ptr_to_u64(fspath);
 
        ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa);
-       if (ret) {
-               printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
+       if (ret < 0) {
+               error("ino paths ioctl: %s", strerror(errno));
                goto out;
        }
 
@@ -78,11 +78,10 @@ static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
        }
 
 out:
-       free(fspath);
        return !!ret;
 }
 
-static const char * const cmd_inode_resolve_usage[] = {
+static const char * const cmd_inspect_inode_resolve_usage[] = {
        "btrfs inspect-internal inode-resolve [-v] <inode> <path>",
        "Get file system paths for the given inode",
        "",
@@ -90,7 +89,7 @@ static const char * const cmd_inode_resolve_usage[] = {
        NULL
 };
 
-static int cmd_inode_resolve(int argc, char **argv)
+static int cmd_inspect_inode_resolve(int argc, char **argv)
 {
        int fd;
        int verbose = 0;
@@ -108,18 +107,16 @@ static int cmd_inode_resolve(int argc, char **argv)
                        verbose = 1;
                        break;
                default:
-                       usage(cmd_inode_resolve_usage);
+                       usage(cmd_inspect_inode_resolve_usage);
                }
        }
 
        if (check_argc_exact(argc - optind, 2))
-               usage(cmd_inode_resolve_usage);
+               usage(cmd_inspect_inode_resolve_usage);
 
-       fd = open_file_or_dir(argv[optind+1], &dirstream);
-       if (fd < 0) {
-               fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
+       fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
+       if (fd < 0)
                return 1;
-       }
 
        ret = __ino_to_path_fd(arg_strtou64(argv[optind]), fd, verbose,
                               argv[optind+1]);
@@ -128,7 +125,7 @@ static int cmd_inode_resolve(int argc, char **argv)
 
 }
 
-static const char * const cmd_logical_resolve_usage[] = {
+static const char * const cmd_inspect_logical_resolve_usage[] = {
        "btrfs inspect-internal logical-resolve [-Pv] [-s bufsize] <logical> <path>",
        "Get file system paths for the given logical address",
        "-P          skip the path resolving and print the inodes instead",
@@ -139,7 +136,7 @@ static const char * const cmd_logical_resolve_usage[] = {
        NULL
 };
 
-static int cmd_logical_resolve(int argc, char **argv)
+static int cmd_inspect_logical_resolve(int argc, char **argv)
 {
        int ret;
        int fd;
@@ -171,12 +168,12 @@ static int cmd_logical_resolve(int argc, char **argv)
                        size = arg_strtou64(optarg);
                        break;
                default:
-                       usage(cmd_logical_resolve_usage);
+                       usage(cmd_inspect_logical_resolve_usage);
                }
        }
 
        if (check_argc_exact(argc - optind, 2))
-               usage(cmd_logical_resolve_usage);
+               usage(cmd_inspect_logical_resolve_usage);
 
        size = min(size, (u64)64 * 1024);
        inodes = malloc(size);
@@ -188,16 +185,15 @@ static int cmd_logical_resolve(int argc, char **argv)
        loi.size = size;
        loi.inodes = ptr_to_u64(inodes);
 
-       fd = open_file_or_dir(argv[optind+1], &dirstream);
+       fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
        if (fd < 0) {
-               fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind+1]);
                ret = 12;
                goto out;
        }
 
        ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
-       if (ret) {
-               printf("ioctl ret=%d, error: %s\n", ret, strerror(errno));
+       if (ret < 0) {
+               error("logical ino ioctl: %s", strerror(errno));
                goto out;
        }
 
@@ -238,10 +234,9 @@ static int cmd_logical_resolve(int argc, char **argv)
                                                name);
                                BUG_ON(ret >= bytes_left);
                                free(name);
-                               path_fd = open_file_or_dir(full_path, &dirs);
+                               path_fd = btrfs_open_dir(full_path, &dirs, 1);
                                if (path_fd < 0) {
-                                       fprintf(stderr, "ERROR: can't access "
-                                               "'%s'\n", full_path);
+                                       ret = -ENOENT;
                                        goto out;
                                }
                        }
@@ -260,13 +255,13 @@ out:
        return !!ret;
 }
 
-static const char * const cmd_subvolid_resolve_usage[] = {
+static const char * const cmd_inspect_subvolid_resolve_usage[] = {
        "btrfs inspect-internal subvolid-resolve <subvolid> <path>",
        "Get file system paths for the given subvolume ID.",
        NULL
 };
 
-static int cmd_subvolid_resolve(int argc, char **argv)
+static int cmd_inspect_subvolid_resolve(int argc, char **argv)
 {
        int ret;
        int fd = -1;
@@ -274,23 +269,23 @@ static int cmd_subvolid_resolve(int argc, char **argv)
        char path[PATH_MAX];
        DIR *dirstream = NULL;
 
-       if (check_argc_exact(argc, 3))
-               usage(cmd_subvolid_resolve_usage);
+       clean_args_no_options(argc, argv, cmd_inspect_subvolid_resolve_usage);
+
+       if (check_argc_exact(argc - optind, 2))
+               usage(cmd_inspect_subvolid_resolve_usage);
 
-       fd = open_file_or_dir(argv[2], &dirstream);
+       fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
        if (fd < 0) {
-               fprintf(stderr, "ERROR: can't access '%s'\n", argv[2]);
                ret = -ENOENT;
                goto out;
        }
 
-       subvol_id = arg_strtou64(argv[1]);
+       subvol_id = arg_strtou64(argv[optind]);
        ret = btrfs_subvolid_resolve(fd, path, sizeof(path), subvol_id);
 
        if (ret) {
-               fprintf(stderr,
-                       "%s: btrfs_subvolid_resolve(subvol_id %llu) failed with ret=%d\n",
-                       argv[0], (unsigned long long)subvol_id, ret);
+               error("resolving subvolid %llu error %d",
+                       (unsigned long long)subvol_id, ret);
                goto out;
        }
 
@@ -299,36 +294,36 @@ static int cmd_subvolid_resolve(int argc, char **argv)
 
 out:
        close_file_or_dir(fd, dirstream);
-       return ret ? 1 : 0;
+       return !!ret;
 }
 
-static const char* const cmd_rootid_usage[] = {
+static const char* const cmd_inspect_rootid_usage[] = {
        "btrfs inspect-internal rootid <path>",
        "Get tree ID of the containing subvolume of path.",
        NULL
 };
 
-static int cmd_rootid(int argc, char **argv)
+static int cmd_inspect_rootid(int argc, char **argv)
 {
        int ret;
        int fd = -1;
        u64 rootid;
        DIR *dirstream = NULL;
 
-       if (check_argc_exact(argc, 2))
-               usage(cmd_rootid_usage);
+       clean_args_no_options(argc, argv, cmd_inspect_rootid_usage);
+
+       if (check_argc_exact(argc - optind, 1))
+               usage(cmd_inspect_rootid_usage);
 
-       fd = open_file_or_dir(argv[1], &dirstream);
+       fd = btrfs_open_dir(argv[optind], &dirstream, 1);
        if (fd < 0) {
-               fprintf(stderr, "ERROR: can't access '%s'\n", argv[1]);
                ret = -ENOENT;
                goto out;
        }
 
        ret = lookup_ino_rootid(fd, &rootid);
        if (ret) {
-               fprintf(stderr, "%s: rootid failed with ret=%d\n",
-                       argv[0], ret);
+               error("rootid failed with ret=%d", ret);
                goto out;
        }
 
@@ -339,6 +334,14 @@ out:
        return !!ret;
 }
 
+static const char* const cmd_inspect_min_dev_size_usage[] = {
+       "btrfs inspect-internal min-dev-size [options] <path>",
+       "Get the minimum size the device can be shrunk to. The",
+       "device id 1 is used by default.",
+       "--id DEVID   specify the device id to query",
+       NULL
+};
+
 struct dev_extent_elem {
        u64 start;
        /* inclusive end */
@@ -412,7 +415,7 @@ static void adjust_dev_min_size(struct list_head *extents,
        /*
         * List of device extents is sorted by descending order of the extent's
         * end offset. If some extent goes beyond the computed minimum size,
-        * which initially matches the sum of the lenghts of all extents,
+        * which initially matches the sum of the lengths of all extents,
         * we need to check if the extent can be relocated to an hole in the
         * device between [0, *min_size[ (which is what the resize ioctl does).
         */
@@ -518,9 +521,7 @@ static int print_min_dev_size(int fd, u64 devid)
 
                ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
                if (ret < 0) {
-                       fprintf(stderr,
-                               "Error invoking tree search ioctl: %s\n",
-                               strerror(errno));
+                       error("tree search ioctl: %s", strerror(errno));
                        ret = 1;
                        goto out;
                }
@@ -536,32 +537,33 @@ static int print_min_dev_size(int fd, u64 devid)
                                                                  off);
                        off += sizeof(*sh);
                        extent = (struct btrfs_dev_extent *)(args.buf + off);
-                       off += sh->len;
+                       off += btrfs_search_header_len(sh);
 
-                       sk->min_objectid = sh->objectid;
-                       sk->min_type = sh->type;
-                       sk->min_offset = sh->offset + 1;
+                       sk->min_objectid = btrfs_search_header_objectid(sh);
+                       sk->min_type = btrfs_search_header_type(sh);
+                       sk->min_offset = btrfs_search_header_offset(sh) + 1;
 
-                       if (sh->objectid != devid ||
-                           sh->type != BTRFS_DEV_EXTENT_KEY)
+                       if (btrfs_search_header_objectid(sh) != devid ||
+                           btrfs_search_header_type(sh) != BTRFS_DEV_EXTENT_KEY)
                                continue;
 
                        len = btrfs_stack_dev_extent_length(extent);
                        min_size += len;
-                       ret = add_dev_extent(&extents, sh->offset,
-                                            sh->offset + len - 1, 0);
+                       ret = add_dev_extent(&extents,
+                               btrfs_search_header_offset(sh),
+                               btrfs_search_header_offset(sh) + len - 1, 0);
 
                        if (!ret && last_pos != (u64)-1 &&
-                           last_pos != sh->offset)
+                           last_pos != btrfs_search_header_offset(sh))
                                ret = add_dev_extent(&holes, last_pos,
-                                                    sh->offset - 1, 1);
+                                       btrfs_search_header_offset(sh) - 1, 1);
                        if (ret) {
-                               fprintf(stderr, "Error: %s\n", strerror(-ret));
+                               error("add device extent: %s", strerror(-ret));
                                ret = 1;
                                goto out;
                        }
 
-                       last_pos = sh->offset + len;
+                       last_pos = btrfs_search_header_offset(sh) + len;
                }
 
                if (sk->min_type != BTRFS_DEV_EXTENT_KEY ||
@@ -579,14 +581,6 @@ out:
        return ret;
 }
 
-static const char* const cmd_inspect_min_dev_size_usage[] = {
-       "btrfs inspect-internal min-dev-size [options] <path>",
-       "Get the minimum size the device can be shrunk to. The",
-       "device id 1 is used by default.",
-       "--id DEVID   specify the device id to query",
-       NULL
-};
-
 static int cmd_inspect_min_dev_size(int argc, char **argv)
 {
        int ret;
@@ -617,17 +611,15 @@ static int cmd_inspect_min_dev_size(int argc, char **argv)
        if (check_argc_exact(argc - optind, 1))
                usage(cmd_inspect_min_dev_size_usage);
 
-       fd = open_file_or_dir(argv[optind], &dirstream);
+       fd = btrfs_open_dir(argv[optind], &dirstream, 1);
        if (fd < 0) {
-               fprintf(stderr, "ERROR: can't access '%s'\n", argv[optind]);
                ret = -ENOENT;
                goto out;
        }
 
        ret = print_min_dev_size(fd, devid);
-out:
        close_file_or_dir(fd, dirstream);
-
+out:
        return !!ret;
 }
 
@@ -636,15 +628,22 @@ static const char inspect_cmd_group_info[] =
 
 const struct cmd_group inspect_cmd_group = {
        inspect_cmd_group_usage, inspect_cmd_group_info, {
-               { "inode-resolve", cmd_inode_resolve, cmd_inode_resolve_usage,
-                       NULL, 0 },
-               { "logical-resolve", cmd_logical_resolve,
-                       cmd_logical_resolve_usage, NULL, 0 },
-               { "subvolid-resolve", cmd_subvolid_resolve,
-                       cmd_subvolid_resolve_usage, NULL, 0 },
-               { "rootid", cmd_rootid, cmd_rootid_usage, NULL, 0 },
+               { "inode-resolve", cmd_inspect_inode_resolve,
+                       cmd_inspect_inode_resolve_usage, NULL, 0 },
+               { "logical-resolve", cmd_inspect_logical_resolve,
+                       cmd_inspect_logical_resolve_usage, NULL, 0 },
+               { "subvolid-resolve", cmd_inspect_subvolid_resolve,
+                       cmd_inspect_subvolid_resolve_usage, NULL, 0 },
+               { "rootid", cmd_inspect_rootid, cmd_inspect_rootid_usage, NULL,
+                       0 },
                { "min-dev-size", cmd_inspect_min_dev_size,
                        cmd_inspect_min_dev_size_usage, NULL, 0 },
+               { "dump-tree", cmd_inspect_dump_tree,
+                               cmd_inspect_dump_tree_usage, NULL, 0 },
+               { "dump-super", cmd_inspect_dump_super,
+                               cmd_inspect_dump_super_usage, NULL, 0 },
+               { "tree-stats", cmd_inspect_tree_stats,
+                               cmd_inspect_tree_stats_usage, NULL, 0 },
                NULL_CMD_STRUCT
        }
 };