mirror of
https://github.com/kdave/btrfs-progs
synced 2025-01-30 09:21:45 +00:00
Btrfs-progs: setup framework to corrupt specific fields of an inode
A user reported a problem with his fs where he had a bogus isize on his directory. In order to make sure my patch for fsck fixes this properly I needed to be able to corrupt an inode like this, which is what this patch is for. Eventually I want to extend this to corrupt everything so we can integrate tests into btrfs-progs to run btrfsck against to make sure we don't regress on fixing things with btrfsck. Thanks, Signed-off-by: Josef Bacik <jbacik@fusionio.com> Signed-off-by: David Sterba <dsterba@suse.cz> Signed-off-by: Chris Mason <chris.mason@fusionio.com>
This commit is contained in:
parent
114a79f823
commit
eb626f4918
@ -33,6 +33,8 @@
|
||||
#include "version.h"
|
||||
#include "utils.h"
|
||||
|
||||
#define FIELD_BUF_LEN 80
|
||||
|
||||
struct extent_buffer *debug_corrupt_block(struct btrfs_root *root, u64 bytenr,
|
||||
u32 blocksize, int copy)
|
||||
{
|
||||
@ -97,6 +99,9 @@ static void print_usage(void)
|
||||
fprintf(stderr, "\t-E The whole extent tree to be corrupted\n");
|
||||
fprintf(stderr, "\t-u Given chunk item to be corrupted\n");
|
||||
fprintf(stderr, "\t-U The whole chunk tree to be corrupted\n");
|
||||
fprintf(stderr, "\t-i The inode item to corrupt (must also specify "
|
||||
"the field to corrupt)\n");
|
||||
fprintf(stderr, "\t-f The field in the item to corrupt\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@ -280,6 +285,83 @@ static void btrfs_corrupt_extent_tree(struct btrfs_trans_handle *trans,
|
||||
}
|
||||
}
|
||||
|
||||
enum btrfs_inode_field {
|
||||
BTRFS_INODE_FIELD_ISIZE,
|
||||
BTRFS_INODE_FIELD_BAD,
|
||||
};
|
||||
|
||||
static enum btrfs_inode_field convert_field(char *field)
|
||||
{
|
||||
if (!strncmp(field, "isize", FIELD_BUF_LEN))
|
||||
return BTRFS_INODE_FIELD_ISIZE;
|
||||
return BTRFS_INODE_FIELD_BAD;
|
||||
}
|
||||
|
||||
static int corrupt_inode(struct btrfs_trans_handle *trans,
|
||||
struct btrfs_root *root, u64 inode, char *field)
|
||||
{
|
||||
struct btrfs_inode_item *ei;
|
||||
struct btrfs_path *path;
|
||||
struct btrfs_key key;
|
||||
enum btrfs_inode_field corrupt_field = convert_field(field);
|
||||
u64 bogus;
|
||||
u64 orig;
|
||||
int ret;
|
||||
|
||||
if (corrupt_field == BTRFS_INODE_FIELD_BAD) {
|
||||
fprintf(stderr, "Invalid field %s\n", field);
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
key.objectid = inode;
|
||||
key.type = BTRFS_INODE_ITEM_KEY;
|
||||
key.offset = (u64)-1;
|
||||
|
||||
path = btrfs_alloc_path();
|
||||
if (!path)
|
||||
return -ENOMEM;
|
||||
|
||||
ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
|
||||
if (ret < 0)
|
||||
goto out;
|
||||
if (ret) {
|
||||
if (!path->slots[0]) {
|
||||
fprintf(stderr, "Couldn't find inode %Lu\n", inode);
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
path->slots[0]--;
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
|
||||
if (key.objectid != inode) {
|
||||
fprintf(stderr, "Couldn't find inode %Lu\n", inode);
|
||||
ret = -ENOENT;
|
||||
goto out;
|
||||
}
|
||||
|
||||
ei = btrfs_item_ptr(path->nodes[0], path->slots[0],
|
||||
struct btrfs_inode_item);
|
||||
switch (corrupt_field) {
|
||||
case BTRFS_INODE_FIELD_ISIZE:
|
||||
orig = btrfs_inode_size(path->nodes[0], ei);
|
||||
do {
|
||||
bogus = rand();
|
||||
} while (bogus == orig);
|
||||
|
||||
btrfs_set_inode_size(path->nodes[0], ei, bogus);
|
||||
break;
|
||||
default:
|
||||
ret = -EINVAL;
|
||||
break;
|
||||
}
|
||||
btrfs_mark_buffer_dirty(path->nodes[0]);
|
||||
out:
|
||||
btrfs_free_path(path);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static struct option long_options[] = {
|
||||
/* { "byte-count", 1, NULL, 'b' }, */
|
||||
{ "logical", 1, NULL, 'l' },
|
||||
@ -290,6 +372,8 @@ static struct option long_options[] = {
|
||||
{ "keys", 0, NULL, 'k' },
|
||||
{ "chunk-record", 0, NULL, 'u' },
|
||||
{ "chunk-tree", 0, NULL, 'U' },
|
||||
{ "inode", 1, NULL, 'i'},
|
||||
{ "field", 1, NULL, 'f'},
|
||||
{ 0, 0, 0, 0}
|
||||
};
|
||||
|
||||
@ -450,12 +534,15 @@ int main(int ac, char **av)
|
||||
int corrupt_block_keys = 0;
|
||||
int chunk_rec = 0;
|
||||
int chunk_tree = 0;
|
||||
u64 inode = 0;
|
||||
char field[FIELD_BUF_LEN];
|
||||
|
||||
field[0] = '\0';
|
||||
srand(128);
|
||||
|
||||
while(1) {
|
||||
int c;
|
||||
c = getopt_long(ac, av, "l:c:b:eEkuU", long_options,
|
||||
c = getopt_long(ac, av, "l:c:b:eEkuUi:f:", long_options,
|
||||
&option_index);
|
||||
if (c < 0)
|
||||
break;
|
||||
@ -465,7 +552,7 @@ int main(int ac, char **av)
|
||||
break;
|
||||
case 'c':
|
||||
copy = atoi(optarg);
|
||||
if (copy == 0) {
|
||||
if (copy <= 0) {
|
||||
fprintf(stderr,
|
||||
"invalid copy number\n");
|
||||
print_usage();
|
||||
@ -493,6 +580,16 @@ int main(int ac, char **av)
|
||||
break;
|
||||
case 'U':
|
||||
chunk_tree = 1;
|
||||
case 'i':
|
||||
inode = atoll(optarg);
|
||||
if (inode == 0) {
|
||||
fprintf(stderr,
|
||||
"invalid inode number\n");
|
||||
print_usage();
|
||||
}
|
||||
break;
|
||||
case 'f':
|
||||
strncpy(field, optarg, FIELD_BUF_LEN);
|
||||
break;
|
||||
default:
|
||||
print_usage();
|
||||
@ -501,11 +598,6 @@ int main(int ac, char **av)
|
||||
ac = ac - optind;
|
||||
if (ac == 0)
|
||||
print_usage();
|
||||
if (logical == (u64)-1 && !(extent_tree || chunk_tree))
|
||||
print_usage();
|
||||
if (copy < 0)
|
||||
print_usage();
|
||||
|
||||
dev = av[optind];
|
||||
|
||||
radix_tree_init();
|
||||
@ -518,6 +610,9 @@ int main(int ac, char **av)
|
||||
}
|
||||
if (extent_rec) {
|
||||
struct btrfs_trans_handle *trans;
|
||||
|
||||
if (logical == (u64)-1)
|
||||
print_usage();
|
||||
trans = btrfs_start_transaction(root, 1);
|
||||
ret = corrupt_extent (trans, root, logical, 0);
|
||||
btrfs_commit_transaction(trans, root);
|
||||
@ -536,6 +631,8 @@ int main(int ac, char **av)
|
||||
struct btrfs_path *path;
|
||||
int del;
|
||||
|
||||
if (logical == (u64)-1)
|
||||
print_usage();
|
||||
del = rand() % 3;
|
||||
path = btrfs_alloc_path();
|
||||
|
||||
@ -561,6 +658,20 @@ int main(int ac, char **av)
|
||||
btrfs_commit_transaction(trans, root);
|
||||
goto out_close;
|
||||
}
|
||||
if (inode) {
|
||||
struct btrfs_trans_handle *trans;
|
||||
|
||||
if (!strlen(field))
|
||||
print_usage();
|
||||
printf("corrupting inode\n");
|
||||
trans = btrfs_start_transaction(root, 1);
|
||||
ret = corrupt_inode(trans, root, inode, field);
|
||||
btrfs_commit_transaction(trans, root);
|
||||
goto out_close;
|
||||
}
|
||||
|
||||
if (logical == (u64)-1)
|
||||
print_usage();
|
||||
|
||||
if (bytes == 0)
|
||||
bytes = root->sectorsize;
|
||||
|
Loading…
Reference in New Issue
Block a user