#include <fcntl.h>
#include <unistd.h>
#include <getopt.h>
+#include <limits.h>
#include "kerncompat.h"
#include "ctree.h"
#define FIELD_BUF_LEN 80
-static struct extent_buffer *debug_corrupt_block(struct btrfs_root *root,
- u64 bytenr, u32 blocksize, u64 copy)
+static int debug_corrupt_block(struct extent_buffer *eb,
+ struct btrfs_root *root, u64 bytenr, u32 blocksize, u64 copy)
{
int ret;
- struct extent_buffer *eb;
u64 length;
struct btrfs_multi_bio *multi = NULL;
struct btrfs_device *device;
int num_copies;
int mirror_num = 1;
- eb = btrfs_find_create_tree_block(root, bytenr, blocksize);
- if (!eb)
- return NULL;
-
length = blocksize;
while (1) {
ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
eb->start, &length, &multi,
mirror_num, NULL);
- BUG_ON(ret);
+ if (ret) {
+ error("cannot map block %llu length %llu mirror %d: %d",
+ (unsigned long long)eb->start,
+ (unsigned long long)length,
+ mirror_num, ret);
+ return ret;
+ }
device = multi->stripes[0].dev;
eb->fd = device->fd;
device->total_ios++;
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));
+ return ret;
+ }
printf("corrupting %llu copy %d\n", eb->start,
mirror_num);
memset(eb->data, 0, eb->len);
- write_extent_to_disk(eb);
+ ret = write_extent_to_disk(eb);
+ if (ret < 0) {
+ error("cannot write eb bytenr %llu: %s",
+ (unsigned long long)eb->dev_bytenr,
+ strerror(-ret));
+ return ret;
+ }
fsync(eb->fd);
}
if (mirror_num > num_copies)
break;
}
- return eb;
+
+ return 0;
}
static void print_usage(int ret)
if (nr == 0)
return;
- slot = rand() % nr;
- bad_slot = rand() % nr;
+ slot = rand_range(nr);
+ bad_slot = rand_range(nr);
if (bad_slot == slot)
return;
{
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;;
struct btrfs_path *path;
int ret;
int slot;
- int should_del = rand() % 3;
+ int should_del = rand_range(3);
path = btrfs_alloc_path();
if (!path)
break;
if (key.type != BTRFS_EXTENT_ITEM_KEY &&
+ key.type != BTRFS_METADATA_ITEM_KEY &&
key.type != BTRFS_TREE_BLOCK_REF_KEY &&
key.type != BTRFS_EXTENT_DATA_REF_KEY &&
key.type != BTRFS_EXTENT_REF_V0_KEY &&
struct extent_buffer *eb)
{
u32 nr = btrfs_header_nritems(eb);
- u32 victim = rand() % nr;
+ u32 victim = rand_range(nr);
u64 objectid;
struct btrfs_key key;
}
if (btrfs_header_level(eb) == 1 && eb != root->node) {
- if (rand() % 5)
+ if (rand_range(5))
return;
}
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;
{
u64 ret;
do {
- ret = rand();
+ ret = rand_u64();
} while (ret == orig);
return ret;
}
{
u32 ret;
do {
- ret = rand();
+ ret = rand_u32();
} while (ret == orig);
return ret;
}
{
u8 ret;
do {
- ret = rand();
+ ret = rand_u8();
} while (ret == orig);
return ret;
}
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;
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);
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;
root = btrfs_read_fs_root(root->fs_info, &root_key);
if (IS_ERR(root)) {
- fprintf(stderr, "Couldn't finde owner root %llu\n",
+ fprintf(stderr, "Couldn't find owner root %llu\n",
key.objectid);
return PTR_ERR(root);
}
}
/* corrupt item using NO cow.
- * Because chunk recover will recover based on whole partition scaning,
+ * Because chunk recover will recover based on whole partition scanning,
* 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 */
while (!btrfs_previous_item(root, path, 0, BTRFS_DEV_ITEM_KEY)) {
slot = path->slots[0];
leaf = path->nodes[0];
- del = rand() % 3;
+ del = rand_range(3);
/* Never delete the first item to keep the leaf structure */
if (path->slots[0] == 0)
del = 0;
while (!btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY)) {
slot = path->slots[0];
leaf = path->nodes[0];
- del = rand() % 3;
+ del = rand_range(3);
btrfs_item_key_to_cpu(leaf, &found_key, slot);
ret = corrupt_item_nocow(trans, root, path, del);
if (ret)
return ret;
}
-int main(int ac, char **av)
+int main(int argc, char **argv)
{
struct cache_tree root_cache;
struct btrfs_key key;
struct btrfs_root *root;
- struct extent_buffer *eb;
char *dev;
/* chunk offset can be 0,so change to (u64)-1 */
u64 logical = (u64)-1;
char field[FIELD_BUF_LEN];
field[0] = '\0';
- srand(128);
memset(&key, 0, sizeof(key));
while(1) {
{ 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;
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);
if (logical == (u64)-1)
print_usage(1);
- del = rand() % 3;
+ del = rand_range(3);
path = btrfs_alloc_path();
if (!path) {
fprintf(stderr, "path allocation failed\n");
if (corrupt_block_keys) {
corrupt_keys_in_block(root, logical);
} else {
- eb = debug_corrupt_block(root, logical,
- root->sectorsize, copy);
+ struct extent_buffer *eb;
+
+ eb = btrfs_find_create_tree_block(root->fs_info,
+ logical, root->sectorsize);
+ if (!eb) {
+ error(
+ "not enough memory to allocate extent buffer for bytenr %llu",
+ (unsigned long long)logical);
+ ret = 1;
+ goto out_close;
+ }
+
+ debug_corrupt_block(eb, root, logical, root->sectorsize,
+ copy);
free_extent_buffer(eb);
}
logical += root->sectorsize;