From: Josef Bacik <jbacik@fb.com>
To: <linux-btrfs@vger.kernel.org>
Subject: [PATCH 11/12] Btrfs-progs: add ability to corrupt dir items
Date: Fri, 10 Oct 2014 16:57:16 -0400 [thread overview]
Message-ID: <1412974637-31334-12-git-send-email-jbacik@fb.com> (raw)
In-Reply-To: <1412974637-31334-1-git-send-email-jbacik@fb.com>
In order to test the dir index corruption fixing patches in fsck we need to add
functionality to btrfs-corrupt-block to corrupt dir item fields. Thanks,
Signed-off-by: Josef Bacik <jbacik@fb.com>
---
btrfs-corrupt-block.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 102 insertions(+), 1 deletion(-)
diff --git a/btrfs-corrupt-block.c b/btrfs-corrupt-block.c
index 3e36f90..66d9f02 100644
--- a/btrfs-corrupt-block.c
+++ b/btrfs-corrupt-block.c
@@ -111,6 +111,7 @@ static void print_usage(void)
fprintf(stderr, "\t-d Delete this item (must specify -K)\n");
fprintf(stderr, "\t-I An item to corrupt (must also specify the field "
"to corrupt and a root+key for the item)\n");
+ fprintf(stderr, "\t-D Corrupt a dir item, must specify key and field\n");
exit(1);
}
@@ -304,6 +305,12 @@ enum btrfs_file_extent_field {
BTRFS_FILE_EXTENT_BAD,
};
+enum btrfs_dir_item_field {
+ BTRFS_DIR_ITEM_NAME,
+ BTRFS_DIR_ITEM_LOCATION_OBJECTID,
+ BTRFS_DIR_ITEM_BAD,
+};
+
enum btrfs_metadata_block_field {
BTRFS_METADATA_BLOCK_GENERATION,
BTRFS_METADATA_BLOCK_SHIFT_ITEMS,
@@ -364,6 +371,15 @@ static enum btrfs_item_field convert_item_field(char *field)
return BTRFS_ITEM_BAD;
}
+static enum btrfs_dir_item_field convert_dir_item_field(char *field)
+{
+ if (!strncmp(field, "name", FIELD_BUF_LEN))
+ return BTRFS_DIR_ITEM_NAME;
+ if (!strncmp(field, "location_objectid", FIELD_BUF_LEN))
+ return BTRFS_DIR_ITEM_LOCATION_OBJECTID;
+ return BTRFS_DIR_ITEM_BAD;
+}
+
static u64 generate_u64(u64 orig)
{
u64 ret;
@@ -448,6 +464,80 @@ out:
return ret;
}
+static int corrupt_dir_item(struct btrfs_root *root, struct btrfs_key *key,
+ char *field)
+{
+ struct btrfs_trans_handle *trans;
+ struct btrfs_dir_item *di;
+ struct btrfs_path *path;
+ char *name;
+ struct btrfs_key location;
+ struct btrfs_disk_key disk_key;
+ unsigned long name_ptr;
+ enum btrfs_dir_item_field corrupt_field =
+ convert_dir_item_field(field);
+ u64 bogus;
+ u16 name_len;
+ int ret;
+
+ if (corrupt_field == BTRFS_DIR_ITEM_BAD) {
+ fprintf(stderr, "Invalid field %s\n", field);
+ return -EINVAL;
+ }
+
+ path = btrfs_alloc_path();
+ if (!path)
+ return -ENOMEM;
+
+ trans = btrfs_start_transaction(root, 1);
+ if (IS_ERR(trans)) {
+ btrfs_free_path(path);
+ return PTR_ERR(trans);
+ }
+
+ ret = btrfs_search_slot(trans, root, key, path, 0, 1);
+ if (ret) {
+ if (ret > 0)
+ ret = -ENOENT;
+ fprintf(stderr, "Error searching for dir item %d\n", ret);
+ goto out;
+ }
+
+ di = btrfs_item_ptr(path->nodes[0], path->slots[0],
+ struct btrfs_dir_item);
+
+ 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);
+ bogus = generate_u64(location.objectid);
+ location.objectid = bogus;
+ btrfs_cpu_key_to_disk(&disk_key, &location);
+ btrfs_set_dir_item_key(path->nodes[0], di, &disk_key);
+ btrfs_mark_buffer_dirty(path->nodes[0]);
+ goto out;
+ default:
+ ret = -EINVAL;
+ goto out;
+ }
+out:
+ btrfs_commit_transaction(trans, root);
+ btrfs_free_path(path);
+ return ret;
+}
static int corrupt_inode(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 inode, char *field)
@@ -772,6 +862,7 @@ static struct option long_options[] = {
{ "key", 1, NULL, 'K'},
{ "delete", 0, NULL, 'd'},
{ "item", 0, NULL, 'I'},
+ { "dir-item", 0, NULL, 'D'},
{ 0, 0, 0, 0}
};
@@ -937,6 +1028,7 @@ int main(int ac, char **av)
int chunk_tree = 0;
int delete = 0;
int corrupt_item = 0;
+ int corrupt_di = 0;
u64 metadata_block = 0;
u64 inode = 0;
u64 file_extent = (u64)-1;
@@ -948,7 +1040,7 @@ int main(int ac, char **av)
while(1) {
int c;
- c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:dI",
+ c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:dID",
long_options, &option_index);
if (c < 0)
break;
@@ -1005,6 +1097,9 @@ int main(int ac, char **av)
case 'I':
corrupt_item = 1;
break;
+ case 'D':
+ corrupt_di = 1;
+ break;
default:
print_usage();
}
@@ -1113,6 +1208,12 @@ int main(int ac, char **av)
ret = corrupt_btrfs_item(root, &key, field);
goto out_close;
}
+ if (corrupt_di) {
+ if (!key.objectid || !strlen(field))
+ print_usage();
+ ret = corrupt_dir_item(root, &key, field);
+ goto out_close;
+ }
if (key.objectid || key.offset || key.type) {
if (!strlen(field))
print_usage();
--
1.8.3.1
prev parent reply other threads:[~2014-10-10 20:57 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-10-10 20:57 Btrfs-progs: fix horrible things Josef Bacik
2014-10-10 20:57 ` [PATCH 01/12] Btrfs-progs: repair missing dir index Josef Bacik
2014-10-10 20:57 ` [PATCH 02/12] Btrfs-progs: pull back backref.c and fix it up Josef Bacik
2014-10-10 20:57 ` [PATCH 03/12] Btrfs-progs: break out rbtree util functions Josef Bacik
2014-10-10 20:57 ` [PATCH 04/12] Btrfs-progs: update rbtree libs Josef Bacik
2014-10-10 20:57 ` [PATCH 05/12] Btrfs-progs: lookup all roots that point to a corrupt block Josef Bacik
2014-10-10 20:57 ` [PATCH 06/12] Btrfs-progs: reset chunk state if we restart check Josef Bacik
2014-10-10 20:57 ` [PATCH 07/12] Btrfs-progs: re-search tree root if it changes Josef Bacik
2014-10-10 20:57 ` [PATCH 08/12] Btrfs-progs: delete bogus dir indexes Josef Bacik
2014-10-10 20:57 ` [PATCH 09/12] Btrfs-progs: add a dummy backref if our location is wrong Josef Bacik
2014-10-10 20:57 ` [PATCH 10/12] Btrfs-progs: deal with mismatch index between dir index and inode ref Josef Bacik
2014-10-10 20:57 ` Josef Bacik [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1412974637-31334-12-git-send-email-jbacik@fb.com \
--to=jbacik@fb.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).