Skip to contentSkip to conversation

Btrfs-progs: deal with invalid key orderings and bad orphan items

PATCH In review Josef Bacik 2 participants 3 messages Dec 11, 2013 – Dec 12, 2013 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Btrfs-progs: deal with invalid key orderings and bad orphan items

A user had a fs where the objectid of an orphan item was not the actual orphan item objectid. This screwed up fsck because the block has keys in the wrong order, also the fs scanning stuff will freak out because we have an inode with nlink 0 and no orphan item. So this patch is pretty big but is all related.

1) Deal with bad key ordering. We can easily fix this up, so fix the checking stuff to tell us exactly what it found when it said there was a problem. Then if it's bad key ordering we can reorder the keys and restart the scan.

2) Deal with bad keys. If we find an orphan item with the wrong objectid it's likely to screw with stuff, so keep track of these sort of things with a bad_item list and just run through and delete any objects that don't make sense. So far we just do this for orphan items but we could extend this as new stuff pops up.

3) Deal with missing orphan items. This is easy, if we have a file with i_nlink set to 0 and no orphan item we can just add an orphan item.

4) Add the infrastructure to corrupt actual key values. Needed this to create a test image to verify I was fixing things properly.

This patch fixes the corrupt image I'm adding and passes the other make test tests. Thanks,

---
 btrfs-corrupt-block.c                   | 109 ++++++++++-
 cmds-check.c                            | 331 ++++++++++++++++++++++++++++----
 ctree.c                                 | 135 +++++++------
 ctree.h                                 |  32 ++-
 file-item.c                             |   2 +-
 tests/fsck-tests/003-bad-orphan-key.img | Bin 0 -> 4096 bytes
 6 files changed, 509 insertions(+), 100 deletions(-)
 create mode 100644 tests/fsck-tests/003-bad-orphan-key.img
btrfs-corrupt-block.c +108 -1
index f0c14a9..8c09855 100644
@@ -105,6 +105,8 @@ static void print_usage(void)
 		"specify -i for the inode and -f for the field to corrupt)\n");
 	fprintf(stderr, "\t-m The metadata block to corrupt (must also "
 		"specify -f for the field to corrupt)\n");
+	fprintf(stderr, "\t-K The key to corrupt in the format "
+		"<num>,<num>,<num> (must also specify -f for the field)\n");
 	fprintf(stderr, "\t-f The field in the item to corrupt\n");
 	exit(1);
 }
@@ -306,6 +308,13 @@ enum btrfs_metadata_block_field {
 	BTRFS_METADATA_BLOCK_BAD,
 };
 
+enum btrfs_key_field {
+	BTRFS_KEY_OBJECTID,
+	BTRFS_KEY_TYPE,
+	BTRFS_KEY_OFFSET,
+	BTRFS_KEY_BAD,
+};
+
 static enum btrfs_inode_field convert_inode_field(char *field)
 {
 	if (!strncmp(field, "isize", FIELD_BUF_LEN))
@@ -328,6 +337,17 @@ convert_metadata_block_field(char *field)
 	return BTRFS_METADATA_BLOCK_BAD;
 }
 
+static enum btrfs_key_field convert_key_field(char *field)
+{
+	if (!strncmp(field, "objectid", FIELD_BUF_LEN))
+		return BTRFS_KEY_OBJECTID;
+	if (!strncmp(field, "type", FIELD_BUF_LEN))
+		return BTRFS_KEY_TYPE;
+	if (!strncmp(field, "offset", FIELD_BUF_LEN))
+		return BTRFS_KEY_OFFSET;
+	return BTRFS_KEY_BAD;
+}
+
 static u64 generate_u64(u64 orig)
 {
 	u64 ret;
@@ -337,6 +357,73 @@ static u64 generate_u64(u64 orig)
 	return ret;
 }
 
+static u8 generate_u8(u8 orig)
+{
+	u8 ret;
+	do {
+		ret = rand();
+	} while (ret == orig);
+	return ret;
+}
+
+static int corrupt_key(struct btrfs_root *root, struct btrfs_key *key,
+		       char *field)
+{
+	enum btrfs_key_field corrupt_field = convert_key_field(field);
+	struct btrfs_path *path;
+	struct btrfs_trans_handle *trans;
+	int ret;
+
+	root = root->fs_info->fs_root;
+	if (corrupt_field == BTRFS_KEY_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 < 0)
+		goto out;
+	if (ret > 0) {
+		fprintf(stderr, "Couldn't find the key to corrupt\n");
+		ret = -ENOENT;
+		goto out;
+	}
+
+	switch (corrupt_field) {
+	case BTRFS_KEY_OBJECTID:
+		key->objectid = generate_u64(key->objectid);
+		break;
+	case BTRFS_KEY_TYPE:
+		key->type = generate_u8(key->type);
+		break;
+	case BTRFS_KEY_OFFSET:
+		key->offset = generate_u64(key->objectid);
+		break;
+	default:
+		fprintf(stderr, "Invalid field %s, %d\n", field,
+			corrupt_field);
+		ret = -EINVAL;
+		goto out;
+	}
+
+	btrfs_set_item_key_unsafe(trans, root, path, key);
+out:
+	btrfs_free_path(path);
+	btrfs_commit_transaction(trans, root);
+	return ret;
+}
+
+
 static int corrupt_inode(struct btrfs_trans_handle *trans,
 			 struct btrfs_root *root, u64 inode, char *field)
 {
@@ -548,6 +635,7 @@ static struct option long_options[] = {
 	{ "file-extent", 1, NULL, 'x'},
 	{ "metadata-block", 1, NULL, 'm'},
 	{ "field", 1, NULL, 'f'},
+	{ "key", 1, NULL, 'K'},
 	{ 0, 0, 0, 0}
 };
 
@@ -696,6 +784,7 @@ out:
 int main(int ac, char **av)
 {
 	struct cache_tree root_cache;
+	struct btrfs_key key;
 	struct btrfs_root *root;
 	struct extent_buffer *eb;
 	char *dev;
@@ -717,10 +806,11 @@ int main(int ac, char **av)
 
 	field[0] = '\0';
 	srand(128);
+	memset(&key, 0, sizeof(key));
 
 	while(1) {
 		int c;
-		c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:", long_options,
+		c = getopt_long(ac, av, "l:c:b:eEkuUi:f:x:m:K:", long_options,
 				&option_index);
 		if (c < 0)
 			break;
@@ -787,6 +877,17 @@ int main(int ac, char **av)
 					print_usage();
 				}
 				break;
+			case 'K':
+				ret = sscanf(optarg, "%llu,%u,%llu",
+					     &key.objectid,
+					     (unsigned int *)&key.type,
+					     &key.offset);
+				if (ret != 3) {
+					fprintf(stderr, "error reading key "
+						"%d\n", errno);
+					print_usage();
+				}
+				break;
 			default:
 				print_usage();
 		}
@@ -882,6 +983,12 @@ int main(int ac, char **av)
 		ret = corrupt_metadata_block(root, metadata_block, field);
 		goto out_close;
 	}
+	if (key.objectid || key.offset || key.type) {
+		if (!strlen(field))
+			print_usage();
+		ret = corrupt_key(root, &key, field);
+		goto out_close;
+	}
 	/*
 	 * If we made it here and we have extent set then we didn't specify
 	 * inode and we're screwed.
cmds-check.c +296 -35

450 lines not on this page · raw message

ctree.c +80 -55

334 lines not on this page · raw message

ctree.h +24 -8
index 761987a..51fdca4 100644
@@ -548,6 +548,7 @@ struct btrfs_path {
 	unsigned int keep_locks:1;
 	unsigned int skip_locking:1;
 	unsigned int leave_spinning:1;
+	unsigned int skip_check_block:1;
 };
 
 /*
@@ -648,6 +649,16 @@ typedef enum {
 	BTRFS_ENCRYPTION_LAST = 1,
 } btrfs_encryption_type;
 
+enum btrfs_tree_block_status {
+	BTRFS_TREE_BLOCK_CLEAN,
+	BTRFS_TREE_BLOCK_INVALID_NRITEMS,
+	BTRFS_TREE_BLOCK_INVALID_PARENT_KEY,
+	BTRFS_TREE_BLOCK_BAD_KEY_ORDER,
+	BTRFS_TREE_BLOCK_INVALID_LEVEL,
+	BTRFS_TREE_BLOCK_INVALID_FREE_SPACE,
+	BTRFS_TREE_BLOCK_INVALID_OFFSETS,
+};
+
 struct btrfs_inode_item {
 	/* nfs style generation number */
 	__le64 generation;
@@ -2203,14 +2214,15 @@ int btrfs_record_file_extent(struct btrfs_trans_handle *trans,
 			      u64 file_pos, u64 disk_bytenr,
 			      u64 num_bytes);
 /* ctree.c */
+int btrfs_comp_cpu_keys(struct btrfs_key *k1, struct btrfs_key *k2);
 int btrfs_del_ptr(struct btrfs_trans_handle *trans, struct btrfs_root *root,
 		   struct btrfs_path *path, int level, int slot);
-int btrfs_check_node(struct btrfs_root *root,
-		      struct btrfs_disk_key *parent_key,
-		      struct extent_buffer *buf);
-int btrfs_check_leaf(struct btrfs_root *root,
-		      struct btrfs_disk_key *parent_key,
-		      struct extent_buffer *buf);
+enum btrfs_tree_block_status
+btrfs_check_node(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
+		 struct extent_buffer *buf);
+enum btrfs_tree_block_status
+btrfs_check_leaf(struct btrfs_root *root, struct btrfs_disk_key *parent_key,
+		 struct extent_buffer *buf);
 void reada_for_search(struct btrfs_root *root, struct btrfs_path *path,
 			     int level, int slot, u64 objectid);
 struct extent_buffer *read_node_slot(struct btrfs_root *root,
@@ -2280,9 +2292,13 @@ static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
 int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
 int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
 int btrfs_leaf_free_space(struct btrfs_root *root, struct extent_buffer *leaf);
-int btrfs_set_item_key_safe(struct btrfs_trans_handle *trans,
-			    struct btrfs_root *root, struct btrfs_path *path,
+void btrfs_fixup_low_keys(struct btrfs_root *root, struct btrfs_path *path,
+			  struct btrfs_disk_key *key, int level);
+int btrfs_set_item_key_safe(struct btrfs_root *root, struct btrfs_path *path,
 			    struct btrfs_key *new_key);
+void btrfs_set_item_key_unsafe(struct btrfs_root *root,
+			       struct btrfs_path *path,
+			       struct btrfs_key *new_key);
 
 /* root-item.c */
 int btrfs_add_root_ref(struct btrfs_trans_handle *trans,
file-item.c +1 -1
index f53a5ca..6f3708b 100644
@@ -367,7 +367,7 @@ static noinline int truncate_one_csum(struct btrfs_trans_handle *trans,
 		BUG_ON(ret);
 
 		key->offset = end_byte;
-		ret = btrfs_set_item_key_safe(trans, root, path, key);
+		ret = btrfs_set_item_key_safe(root, path, key);
 		BUG_ON(ret);
 	} else {
 		BUG();
tests/fsck-tests/003-bad-orphan-key.img new file binary +0 -0
new file mode 100644
index 0000000000000000000000000000000000000000..2a9b60d95debb9fe1dd8620858e34671c84b86ed
GIT binary patch
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 11 lines · Josef Bacik
cmds-check.c +3 -3
@@ -5527,15 +5734,16 @@ again:
 	}
 	btrfs_release_path(&path);
 	while(1) {
-		ret = run_next_block(root, bits, bits_nr, &last, &pending,
-				     &seen, &reada, &nodes, &extent_cache,
-				     &chunk_cache, &dev_cache,
+		ret = run_next_block(trans, root, bits, bits_nr, &last,
+				     &pending, &seen, &reada, &nodes,
+				     &extent_cache, &chunk_cache, &dev_cache,
 				     &block_group_cache, &dev_extent_cache);

cmds-check.c again: · line 5749

Quoted 10 lines
cmds-check.c +2 -1
 		if (ret != 0)
 			break;
 	}
 
-	ret = check_extent_refs(trans, root, &extent_cache);
+	if (ret >= 0)
+		ret = check_extent_refs(trans, root, &extent_cache);
 	if (ret == -EAGAIN) {
 		ret = btrfs_commit_transaction(trans, root);
 		if (ret)

This hunk conflicts with Gui Hecheng's patch "Btrfs-progs: fix btrfsck improper prompt on dropping snapshots"

I applied the change where necessary, but haven't reviewed if this does not break the semantics. You may want to take a look.

thanks,
david

#2 lore raw
Quoted 25 lines · David Sterba
Quoted 11 lines · Josef Bacik
cmds-check.c +3 -3
@@ -5527,15 +5734,16 @@ again:
  	}
  	btrfs_release_path(&path);
  	while(1) {
-		ret = run_next_block(root, bits, bits_nr, &last, &pending,
-				     &seen, &reada, &nodes, &extent_cache,
-				     &chunk_cache, &dev_cache,
+		ret = run_next_block(trans, root, bits, bits_nr, &last,
+				     &pending, &seen, &reada, &nodes,
+				     &extent_cache, &chunk_cache, &dev_cache,
  				     &block_group_cache, &dev_extent_cache);

cmds-check.c again: · line 5749

Quoted 10 lines
cmds-check.c +2 -1
  		if (ret != 0)
  			break;
  	}
 
-	ret = check_extent_refs(trans, root, &extent_cache);
+	if (ret >= 0)
+		ret = check_extent_refs(trans, root, &extent_cache);
  	if (ret == -EAGAIN) {
  		ret = btrfs_commit_transaction(trans, root);
  		if (ret)

This hunk conflicts with Gui Hecheng's patch "Btrfs-progs: fix btrfsck improper prompt on dropping snapshots"

Quoted 3 lines

I applied the change where necessary, but haven't reviewed if this does not break the semantics. You may want to take a look.

As long as it passes make test it should be fine, I'll double check just in case. Thanks,

Josef