btrfs-progs: update CHANGES for v4.16
[platform/upstream/btrfs-progs.git] / cmds-inspect.c
index 25e8c01..afd7fe4 100644 (file)
@@ -31,9 +31,7 @@
 #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"
+#include "help.h"
 
 static const char * const inspect_cmd_group_usage[] = {
        "btrfs inspect-internal <command> <args>",
@@ -54,7 +52,7 @@ static int __ino_to_path_fd(u64 inum, int fd, int verbose, const char *prepend)
 
        ret = ioctl(fd, BTRFS_IOC_INO_PATHS, &ipa);
        if (ret < 0) {
-               error("ino paths ioctl: %s", strerror(errno));
+               error("ino paths ioctl: %m");
                goto out;
        }
 
@@ -96,7 +94,6 @@ static int cmd_inspect_inode_resolve(int argc, char **argv)
        int ret;
        DIR *dirstream = NULL;
 
-       optind = 1;
        while (1) {
                int c = getopt(argc, argv, "v");
                if (c < 0)
@@ -147,11 +144,10 @@ static int cmd_inspect_logical_resolve(int argc, char **argv)
        struct btrfs_ioctl_logical_ino_args loi;
        struct btrfs_data_container *inodes;
        u64 size = 4096;
-       char full_path[4096];
+       char full_path[PATH_MAX];
        char *path_ptr;
        DIR *dirstream = NULL;
 
-       optind = 1;
        while (1) {
                int c = getopt(argc, argv, "Pvs:");
                if (c < 0)
@@ -175,7 +171,7 @@ static int cmd_inspect_logical_resolve(int argc, char **argv)
        if (check_argc_exact(argc - optind, 2))
                usage(cmd_inspect_logical_resolve_usage);
 
-       size = min(size, (u64)64 * 1024);
+       size = min(size, (u64)SZ_64K);
        inodes = malloc(size);
        if (!inodes)
                return 1;
@@ -193,7 +189,7 @@ static int cmd_inspect_logical_resolve(int argc, char **argv)
 
        ret = ioctl(fd, BTRFS_IOC_LOGICAL_INO, &loi);
        if (ret < 0) {
-               error("logical ino ioctl: %s", strerror(errno));
+               error("logical ino ioctl: %m");
                goto out;
        }
 
@@ -209,7 +205,10 @@ static int cmd_inspect_logical_resolve(int argc, char **argv)
        ret = snprintf(full_path, bytes_left, "%s/", argv[optind+1]);
        path_ptr = full_path + ret;
        bytes_left -= ret + 1;
-       BUG_ON(bytes_left < 0);
+       if (bytes_left < 0) {
+               error("path buffer too small: %d bytes", bytes_left);
+               goto out;
+       }
 
        for (i = 0; i < inodes->elem_cnt; i += 3) {
                u64 inum = inodes->val[i];
@@ -232,8 +231,12 @@ static int cmd_inspect_logical_resolve(int argc, char **argv)
                                path_ptr[-1] = '/';
                                ret = snprintf(path_ptr, bytes_left, "%s",
                                                name);
-                               BUG_ON(ret >= bytes_left);
                                free(name);
+                               if (ret >= bytes_left) {
+                                       error("path buffer too small: %d bytes",
+                                                       bytes_left - ret);
+                                       goto out;
+                               }
                                path_fd = btrfs_open_dir(full_path, &dirs, 1);
                                if (path_fd < 0) {
                                        ret = -ENOENT;
@@ -315,15 +318,15 @@ static int cmd_inspect_rootid(int argc, char **argv)
        if (check_argc_exact(argc - optind, 1))
                usage(cmd_inspect_rootid_usage);
 
-       fd = btrfs_open_dir(argv[optind], &dirstream, 1);
+       fd = btrfs_open_file_or_dir(argv[optind], &dirstream, 1);
        if (fd < 0) {
                ret = -ENOENT;
                goto out;
        }
 
-       ret = lookup_ino_rootid(fd, &rootid);
+       ret = lookup_path_rootid(fd, &rootid);
        if (ret) {
-               error("rootid failed with ret=%d", ret);
+               error("failed to lookup root id: %s", strerror(-ret));
                goto out;
        }
 
@@ -415,7 +418,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).
         */
@@ -481,7 +484,7 @@ static void adjust_dev_min_size(struct list_head *extents,
                 * chunk tree, so often this can lead to the need of allocating
                 * a new system chunk too, which has a maximum size of 32Mb.
                 */
-               *min_size += 32 * 1024 * 1024;
+               *min_size += SZ_32M;
        }
 }
 
@@ -495,7 +498,7 @@ static int print_min_dev_size(int fd, u64 devid)
         * possibility of deprecating/removing it has been discussed, so we
         * ignore it here.
         */
-       u64 min_size = 1 * 1024 * 1024ull;
+       u64 min_size = SZ_1M;
        struct btrfs_ioctl_search_args args;
        struct btrfs_ioctl_search_key *sk = &args.key;
        u64 last_pos = (u64)-1;
@@ -521,7 +524,7 @@ static int print_min_dev_size(int fd, u64 devid)
 
                ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &args);
                if (ret < 0) {
-                       error("tree search ioctl: %s", strerror(errno));
+                       error("tree search ioctl: %m");
                        ret = 1;
                        goto out;
                }