diff mbox series

[06/12] btrfs: extend btrfs_leaf_check to return btrfs_tree_block_status

Message ID a528c8c37d20b53a0608185ecb83f870026a9917.1682798736.git.josef@toxicpanda.com (mailing list archive)
State New, archived
Headers show
Series btrfs: various cleanups to make ctree.c sync easier | expand

Commit Message

Josef Bacik April 29, 2023, 8:07 p.m. UTC
Instead of blanket returning -EUCLEAN for all the failures in
btrfs_check_leaf, use btrfs_tree_block_status and return the appropriate
status for each failure.  Rename the helper to __btrfs_check_leaf and
then make a wrapper of btrfs_check_leaf that will return -EUCLEAN to
non-clean error codes.  This will allow us to have the
__btrfs_check_leaf variant in btrfs-progs while keeping the behavior in
the kernel consistent.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 fs/btrfs/tree-checker.c | 36 +++++++++++++++++++++++-------------
 fs/btrfs/tree-checker.h |  7 +++++++
 2 files changed, 30 insertions(+), 13 deletions(-)

Comments

Johannes Thumshirn May 2, 2023, 12:03 p.m. UTC | #1
On 29.04.23 22:08, Josef Bacik wrote:
> diff --git a/fs/btrfs/tree-checker.h b/fs/btrfs/tree-checker.h
> index 5e06d9ad2862..3b8de6d36141 100644
> --- a/fs/btrfs/tree-checker.h
> +++ b/fs/btrfs/tree-checker.h
> @@ -50,8 +50,15 @@ enum btrfs_tree_block_status {
>  	BTRFS_TREE_BLOCK_INVALID_OFFSETS,
>  	BTRFS_TREE_BLOCK_INVALID_BLOCKPTR,
>  	BTRFS_TREE_BLOCK_INVALID_ITEM,
> +	BTRFS_TREE_BLOCK_INVALID_OWNER,
>  };

Why didn't you add 'BTRFS_TREE_BLOCK_INVALID_OWNER' in patch 4?
David Sterba May 9, 2023, 9:01 p.m. UTC | #2
On Tue, May 02, 2023 at 12:03:54PM +0000, Johannes Thumshirn wrote:
> On 29.04.23 22:08, Josef Bacik wrote:
> > diff --git a/fs/btrfs/tree-checker.h b/fs/btrfs/tree-checker.h
> > index 5e06d9ad2862..3b8de6d36141 100644
> > --- a/fs/btrfs/tree-checker.h
> > +++ b/fs/btrfs/tree-checker.h
> > @@ -50,8 +50,15 @@ enum btrfs_tree_block_status {
> >  	BTRFS_TREE_BLOCK_INVALID_OFFSETS,
> >  	BTRFS_TREE_BLOCK_INVALID_BLOCKPTR,
> >  	BTRFS_TREE_BLOCK_INVALID_ITEM,
> > +	BTRFS_TREE_BLOCK_INVALID_OWNER,
> >  };
> 
> Why didn't you add 'BTRFS_TREE_BLOCK_INVALID_OWNER' in patch 4?

Moved to the patch adding all the other enums.
diff mbox series

Patch

diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
index bfc1f65726f6..2c330e9d123a 100644
--- a/fs/btrfs/tree-checker.c
+++ b/fs/btrfs/tree-checker.c
@@ -1678,7 +1678,7 @@  static enum btrfs_tree_block_status check_leaf_item(struct extent_buffer *leaf,
 	return BTRFS_TREE_BLOCK_CLEAN;
 }
 
-int btrfs_check_leaf(struct extent_buffer *leaf)
+enum btrfs_tree_block_status __btrfs_check_leaf(struct extent_buffer *leaf)
 {
 	struct btrfs_fs_info *fs_info = leaf->fs_info;
 	/* No valid key type is 0, so all key should be larger than this key */
@@ -1692,7 +1692,7 @@  int btrfs_check_leaf(struct extent_buffer *leaf)
 		generic_err(leaf, 0,
 			"invalid level for leaf, have %d expect 0",
 			btrfs_header_level(leaf));
-		return -EUCLEAN;
+		return BTRFS_TREE_BLOCK_INVALID_LEVEL;
 	}
 
 	/*
@@ -1715,32 +1715,32 @@  int btrfs_check_leaf(struct extent_buffer *leaf)
 			generic_err(leaf, 0,
 			"invalid root, root %llu must never be empty",
 				    owner);
-			return -EUCLEAN;
+			return BTRFS_TREE_BLOCK_INVALID_NRITEMS;
 		}
 
 		/* Unknown tree */
 		if (unlikely(owner == 0)) {
 			generic_err(leaf, 0,
 				"invalid owner, root 0 is not defined");
-			return -EUCLEAN;
+			return BTRFS_TREE_BLOCK_INVALID_OWNER;
 		}
 
 		/* EXTENT_TREE_V2 can have empty extent trees. */
 		if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
-			return 0;
+			return BTRFS_TREE_BLOCK_CLEAN;
 
 		if (unlikely(owner == BTRFS_EXTENT_TREE_OBJECTID)) {
 			generic_err(leaf, 0,
 			"invalid root, root %llu must never be empty",
 				    owner);
-			return -EUCLEAN;
+			return BTRFS_TREE_BLOCK_INVALID_NRITEMS;
 		}
 
-		return 0;
+		return BTRFS_TREE_BLOCK_CLEAN;
 	}
 
 	if (unlikely(nritems == 0))
-		return 0;
+		return BTRFS_TREE_BLOCK_CLEAN;
 
 	/*
 	 * Check the following things to make sure this is a good leaf, and
@@ -1766,7 +1766,7 @@  int btrfs_check_leaf(struct extent_buffer *leaf)
 				prev_key.objectid, prev_key.type,
 				prev_key.offset, key.objectid, key.type,
 				key.offset);
-			return -EUCLEAN;
+			return BTRFS_TREE_BLOCK_BAD_KEY_ORDER;
 		}
 
 		item_data_end = (u64)btrfs_item_offset(leaf, slot) +
@@ -1785,7 +1785,7 @@  int btrfs_check_leaf(struct extent_buffer *leaf)
 			generic_err(leaf, slot,
 				"unexpected item end, have %llu expect %u",
 				item_data_end, item_end_expected);
-			return -EUCLEAN;
+			return BTRFS_TREE_BLOCK_INVALID_OFFSETS;
 		}
 
 		/*
@@ -1797,7 +1797,7 @@  int btrfs_check_leaf(struct extent_buffer *leaf)
 			generic_err(leaf, slot,
 			"slot end outside of leaf, have %llu expect range [0, %u]",
 				item_data_end, BTRFS_LEAF_DATA_SIZE(fs_info));
-			return -EUCLEAN;
+			return BTRFS_TREE_BLOCK_INVALID_OFFSETS;
 		}
 
 		/* Also check if the item pointer overlaps with btrfs item. */
@@ -1808,7 +1808,7 @@  int btrfs_check_leaf(struct extent_buffer *leaf)
 				btrfs_item_nr_offset(leaf, slot) +
 				sizeof(struct btrfs_item),
 				btrfs_item_ptr_offset(leaf, slot));
-			return -EUCLEAN;
+			return BTRFS_TREE_BLOCK_INVALID_OFFSETS;
 		}
 
 		/*
@@ -1824,7 +1824,7 @@  int btrfs_check_leaf(struct extent_buffer *leaf)
 			 */
 			ret = check_leaf_item(leaf, &key, slot, &prev_key);
 			if (unlikely(ret != BTRFS_TREE_BLOCK_CLEAN))
-				return -EUCLEAN;
+				return ret;
 		}
 
 		prev_key.objectid = key.objectid;
@@ -1832,6 +1832,16 @@  int btrfs_check_leaf(struct extent_buffer *leaf)
 		prev_key.offset = key.offset;
 	}
 
+	return BTRFS_TREE_BLOCK_CLEAN;
+}
+
+int btrfs_check_leaf(struct extent_buffer *leaf)
+{
+	enum btrfs_tree_block_status ret;
+
+	ret = __btrfs_check_leaf(leaf);
+	if (unlikely(ret != BTRFS_TREE_BLOCK_CLEAN))
+		return -EUCLEAN;
 	return 0;
 }
 ALLOW_ERROR_INJECTION(btrfs_check_leaf, ERRNO);
diff --git a/fs/btrfs/tree-checker.h b/fs/btrfs/tree-checker.h
index 5e06d9ad2862..3b8de6d36141 100644
--- a/fs/btrfs/tree-checker.h
+++ b/fs/btrfs/tree-checker.h
@@ -50,8 +50,15 @@  enum btrfs_tree_block_status {
 	BTRFS_TREE_BLOCK_INVALID_OFFSETS,
 	BTRFS_TREE_BLOCK_INVALID_BLOCKPTR,
 	BTRFS_TREE_BLOCK_INVALID_ITEM,
+	BTRFS_TREE_BLOCK_INVALID_OWNER,
 };
 
+/*
+ * Exported simply for btrfs-progs which wants to have the
+ * btrfs_tree_block_status return codes.
+ */
+enum btrfs_tree_block_status __btrfs_check_leaf(struct extent_buffer *leaf);
+
 int btrfs_check_leaf(struct extent_buffer *leaf);
 int btrfs_check_node(struct extent_buffer *node);