btrfs-progs: add getters for ioctl search_header
[platform/upstream/btrfs-progs.git] / btrfs-corrupt-block.c
index 1a2aa23..66d93e5 100644 (file)
@@ -21,6 +21,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <getopt.h>
+#include <limits.h>
 
 #include "kerncompat.h"
 #include "ctree.h"
@@ -33,8 +34,8 @@
 
 #define FIELD_BUF_LEN 80
 
-struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr,
-                                    u32 blocksize, u64 copy)
+static struct extent_buffer *debug_corrupt_block(struct btrfs_root *root,
+               u64 bytenr, u32 blocksize, u64 copy)
 {
        int ret;
        struct extent_buffer *eb;
@@ -44,7 +45,7 @@ struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr,
        int num_copies;
        int mirror_num = 1;
 
-       eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
+       eb = btrfs_find_create_tree_block(root->fs_info, bytenr, blocksize);
        if (!eb)
                return NULL;
 
@@ -161,7 +162,7 @@ static int corrupt_keys_in_block(struct btrfs_root *root, u64 bytenr)
 {
        struct extent_buffer *eb;
 
-       eb = read_tree_block(root, bytenr, root->leafsize, 0);
+       eb = read_tree_block(root, bytenr, root->nodesize, 0);
        if (!extent_buffer_uptodate(eb))
                return -EIO;;
 
@@ -288,7 +289,7 @@ static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
                struct extent_buffer *next;
 
                next = read_tree_block(root, btrfs_node_blockptr(eb, i),
-                                      root->leafsize,
+                                      root->nodesize,
                                       btrfs_node_ptr_generation(eb, i));
                if (!extent_buffer_uptodate(next))
                        continue;
@@ -475,7 +476,7 @@ static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
        struct btrfs_trans_handle *trans;
        struct btrfs_dir_item *di;
        struct btrfs_path *path;
-       char *name;
+       char name[PATH_MAX];
        struct btrfs_key location;
        struct btrfs_disk_key disk_key;
        unsigned long name_ptr;
@@ -514,17 +515,11 @@ static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
        switch (corrupt_field) {
        case BTRFS_DIR_ITEM_NAME:
                name_len = btrfs_dir_name_len(path->nodes[0], di);
-               name = malloc(name_len);
-               if (!name) {
-                       ret = -ENOMEM;
-                       goto out;
-               }
                name_ptr = (unsigned long)(di + 1);
                read_extent_buffer(path->nodes[0], name, name_ptr, name_len);
                name[0]++;
                write_extent_buffer(path->nodes[0], name, name_ptr, name_len);
                btrfs_mark_buffer_dirty(path->nodes[0]);
-               free(name);
                goto out;
        case BTRFS_DIR_ITEM_LOCATION_OBJECTID:
                btrfs_dir_item_key_to_cpu(path->nodes[0], di, &location);
@@ -705,7 +700,7 @@ static int corrupt_metadata_block(struct btrfs_root *root, u64 block,
                return -EINVAL;
        }
 
-       eb = read_tree_block(root, block, root->leafsize, 0);
+       eb = read_tree_block(root, block, root->nodesize, 0);
        if (!extent_buffer_uptodate(eb)) {
                fprintf(stderr, "Couldn't read in tree block %s\n", field);
                return -EINVAL;
@@ -880,7 +875,7 @@ static int delete_csum(struct btrfs_root *root, u64 bytenr, u64 bytes)
  * If using COW, chunk recover will use the old item to recover,
  * which is still OK but we want to check the ability to rebuild chunk
  * not only restore the old ones */
-int corrupt_item_nocow(struct btrfs_trans_handle *trans,
+static int corrupt_item_nocow(struct btrfs_trans_handle *trans,
                       struct btrfs_root *root, struct btrfs_path *path,
                       int del)
 {
@@ -913,7 +908,7 @@ int corrupt_item_nocow(struct btrfs_trans_handle *trans,
        }
        return ret;
 }
-int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
+static int corrupt_chunk_tree(struct btrfs_trans_handle *trans,
                       struct btrfs_root *root)
 {
        int ret;
@@ -986,7 +981,7 @@ free_out:
        btrfs_free_path(path);
        return ret;
 }
-int find_chunk_offset(struct btrfs_root *root,
+static int find_chunk_offset(struct btrfs_root *root,
                      struct btrfs_path *path, u64 offset)
 {
        struct btrfs_key key;
@@ -1010,14 +1005,14 @@ int find_chunk_offset(struct btrfs_root *root,
                goto out;
        }
        if (ret < 0) {
-               fprintf(stderr, "Error searching chunk");
+               fprintf(stderr, "Error searching chunk\n");
                goto out;
        }
 out:
        return ret;
 
 }
-int main(int ac, char **av)
+int main(int argc, char **argv)
 {
        struct cache_tree root_cache;
        struct btrfs_key key;
@@ -1074,7 +1069,7 @@ int main(int ac, char **av)
                        { NULL, 0, NULL, 0 }
                };
 
-               c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:IDdr:C:",
+               c = getopt_long(argc, argv, "l:c:b:eEkuUi:f:x:m:K:IDdr:C:",
                                long_options, NULL);
                if (c < 0)
                        break;
@@ -1146,11 +1141,10 @@ int main(int ac, char **av)
                                print_usage(c != GETOPT_VAL_HELP);
                }
        }
-       set_argv0(av);
-       ac = ac - optind;
-       if (check_argc_min(ac, 1))
+       set_argv0(argv);
+       if (check_argc_min(argc - optind, 1))
                print_usage(1);
-       dev = av[optind];
+       dev = argv[optind];
 
        radix_tree_init();
        cache_tree_init(&root_cache);
@@ -1217,7 +1211,7 @@ int main(int ac, char **av)
        if (inode) {
                struct btrfs_trans_handle *trans;
 
-               if (!strlen(field))
+               if (*field == 0)
                        print_usage(1);
 
                trans = btrfs_start_transaction(root, 1);
@@ -1233,13 +1227,13 @@ int main(int ac, char **av)
                goto out_close;
        }
        if (metadata_block) {
-               if (!strlen(field))
+               if (*field == 0)
                        print_usage(1);
                ret = corrupt_metadata_block(root, metadata_block, field);
                goto out_close;
        }
        if (corrupt_di) {
-               if (!key.objectid || !strlen(field))
+               if (!key.objectid || *field == 0)
                        print_usage(1);
                ret = corrupt_dir_item(root, &key, field);
                goto out_close;
@@ -1276,7 +1270,7 @@ int main(int ac, char **av)
                goto out_close;
        }
        if (key.objectid || key.offset || key.type) {
-               if (!strlen(field))
+               if (*field == 0)
                        print_usage(1);
                ret = corrupt_key(root, &key, field);
                goto out_close;