linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/4] btrfs: tree-checker: Clean up fs_info parameter from error message wrapper
@ 2019-12-09 10:54 Qu Wenruo
  2019-12-09 10:54 ` [PATCH 2/4] btrfs: tree-checker: Refactor inode key check into seperate function Qu Wenruo
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Qu Wenruo @ 2019-12-09 10:54 UTC (permalink / raw)
  To: linux-btrfs

The @fs_info parameter can be extracted from extent_buffer structure,
and there are already some wrappers getting rid of the @fs_info
parameter.

This patch will finish the cleanup.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/tree-checker.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
index 493d4d9e0f79..6cb49c75c5e1 100644
--- a/fs/btrfs/tree-checker.c
+++ b/fs/btrfs/tree-checker.c
@@ -787,7 +787,7 @@ static int check_dev_item(struct extent_buffer *leaf,
 }
 
 /* Inode item error output has the same format as dir_item_err() */
-#define inode_item_err(fs_info, eb, slot, fmt, ...)			\
+#define inode_item_err(eb, slot, fmt, ...)			\
 	dir_item_err(eb, slot, fmt, __VA_ARGS__)
 
 static int check_inode_item(struct extent_buffer *leaf,
@@ -812,7 +812,7 @@ static int check_inode_item(struct extent_buffer *leaf,
 		return -EUCLEAN;
 	}
 	if (key->offset != 0) {
-		inode_item_err(fs_info, leaf, slot,
+		inode_item_err(leaf, slot,
 			"invalid key offset: has %llu expect 0",
 			key->offset);
 		return -EUCLEAN;
@@ -821,7 +821,7 @@ static int check_inode_item(struct extent_buffer *leaf,
 
 	/* Here we use super block generation + 1 to handle log tree */
 	if (btrfs_inode_generation(leaf, iitem) > super_gen + 1) {
-		inode_item_err(fs_info, leaf, slot,
+		inode_item_err(leaf, slot,
 			"invalid inode generation: has %llu expect (0, %llu]",
 			       btrfs_inode_generation(leaf, iitem),
 			       super_gen + 1);
@@ -829,7 +829,7 @@ static int check_inode_item(struct extent_buffer *leaf,
 	}
 	/* Note for ROOT_TREE_DIR_ITEM, mkfs could set its transid 0 */
 	if (btrfs_inode_transid(leaf, iitem) > super_gen + 1) {
-		inode_item_err(fs_info, leaf, slot,
+		inode_item_err(leaf, slot,
 			"invalid inode generation: has %llu expect [0, %llu]",
 			       btrfs_inode_transid(leaf, iitem), super_gen + 1);
 		return -EUCLEAN;
@@ -842,7 +842,7 @@ static int check_inode_item(struct extent_buffer *leaf,
 	 */
 	mode = btrfs_inode_mode(leaf, iitem);
 	if (mode & ~valid_mask) {
-		inode_item_err(fs_info, leaf, slot,
+		inode_item_err(leaf, slot,
 			       "unknown mode bit detected: 0x%x",
 			       mode & ~valid_mask);
 		return -EUCLEAN;
@@ -855,20 +855,20 @@ static int check_inode_item(struct extent_buffer *leaf,
 	 */
 	if (!has_single_bit_set(mode & S_IFMT)) {
 		if (!S_ISLNK(mode) && !S_ISBLK(mode) && !S_ISSOCK(mode)) {
-			inode_item_err(fs_info, leaf, slot,
+			inode_item_err(leaf, slot,
 			"invalid mode: has 0%o expect valid S_IF* bit(s)",
 				       mode & S_IFMT);
 			return -EUCLEAN;
 		}
 	}
 	if (S_ISDIR(mode) && btrfs_inode_nlink(leaf, iitem) > 1) {
-		inode_item_err(fs_info, leaf, slot,
+		inode_item_err(leaf, slot,
 		       "invalid nlink: has %u expect no more than 1 for dir",
 			btrfs_inode_nlink(leaf, iitem));
 		return -EUCLEAN;
 	}
 	if (btrfs_inode_flags(leaf, iitem) & ~BTRFS_INODE_FLAG_MASK) {
-		inode_item_err(fs_info, leaf, slot,
+		inode_item_err(leaf, slot,
 			       "unknown flags detected: 0x%llx",
 			       btrfs_inode_flags(leaf, iitem) &
 			       ~BTRFS_INODE_FLAG_MASK);
@@ -1288,8 +1288,8 @@ static int check_extent_data_ref(struct extent_buffer *leaf,
 	return 0;
 }
 
-#define inode_ref_err(fs_info, eb, slot, fmt, args...)			\
-	inode_item_err(fs_info, eb, slot, fmt, ##args)
+#define inode_ref_err(eb, slot, fmt, args...)			\
+	inode_item_err(eb, slot, fmt, ##args)
 static int check_inode_ref(struct extent_buffer *leaf,
 			   struct btrfs_key *key, struct btrfs_key *prev_key,
 			   int slot)
@@ -1302,7 +1302,7 @@ static int check_inode_ref(struct extent_buffer *leaf,
 		return -EUCLEAN;
 	/* namelen can't be 0, so item_size == sizeof() is also invalid */
 	if (btrfs_item_size_nr(leaf, slot) <= sizeof(*iref)) {
-		inode_ref_err(fs_info, leaf, slot,
+		inode_ref_err(leaf, slot,
 			"invalid item size, have %u expect (%zu, %u)",
 			btrfs_item_size_nr(leaf, slot),
 			sizeof(*iref), BTRFS_LEAF_DATA_SIZE(leaf->fs_info));
@@ -1315,7 +1315,7 @@ static int check_inode_ref(struct extent_buffer *leaf,
 		u16 namelen;
 
 		if (ptr + sizeof(iref) > end) {
-			inode_ref_err(fs_info, leaf, slot,
+			inode_ref_err(leaf, slot,
 			"inode ref overflow, ptr %lu end %lu inode_ref_size %zu",
 				ptr, end, sizeof(iref));
 			return -EUCLEAN;
@@ -1324,7 +1324,7 @@ static int check_inode_ref(struct extent_buffer *leaf,
 		iref = (struct btrfs_inode_ref *)ptr;
 		namelen = btrfs_inode_ref_name_len(leaf, iref);
 		if (ptr + sizeof(*iref) + namelen > end) {
-			inode_ref_err(fs_info, leaf, slot,
+			inode_ref_err(leaf, slot,
 				"inode ref overflow, ptr %lu end %lu namelen %u",
 				ptr, end, namelen);
 			return -EUCLEAN;
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 2/4] btrfs: tree-checker: Refactor inode key check into seperate function
  2019-12-09 10:54 [PATCH 1/4] btrfs: tree-checker: Clean up fs_info parameter from error message wrapper Qu Wenruo
@ 2019-12-09 10:54 ` Qu Wenruo
  2019-12-09 12:11   ` Nikolay Borisov
  2019-12-10 14:42   ` Su Yue
  2019-12-09 10:54 ` [PATCH 3/4] btrfs: tree-checker: Refactor root key check into separate function Qu Wenruo
                   ` (3 subsequent siblings)
  4 siblings, 2 replies; 10+ messages in thread
From: Qu Wenruo @ 2019-12-09 10:54 UTC (permalink / raw)
  To: linux-btrfs

Inode key check is not as easy as several lines, and it will be called
in more than one location (INODE_ITEM check and
DIR_ITEM/DIR_INDEX/XATTR_ITEM location key check).

So here refactor such check into check_inode_key().

And add extra checks for XATTR_ITEM.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/tree-checker.c | 77 +++++++++++++++++++++++++++++++----------
 1 file changed, 59 insertions(+), 18 deletions(-)

diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
index 6cb49c75c5e1..68dad9ec38dd 100644
--- a/fs/btrfs/tree-checker.c
+++ b/fs/btrfs/tree-checker.c
@@ -359,6 +359,60 @@ static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key,
 	return 0;
 }
 
+/* Inode item error output has the same format as dir_item_err() */
+#define inode_item_err(eb, slot, fmt, ...)			\
+	dir_item_err(eb, slot, fmt, __VA_ARGS__)
+
+static int check_inode_key(struct extent_buffer *leaf, struct btrfs_key *key,
+			   int slot)
+{
+	struct btrfs_key item_key;
+	bool is_inode_item;
+
+	btrfs_item_key_to_cpu(leaf, &item_key, slot);
+	is_inode_item = (item_key.type == BTRFS_INODE_ITEM_KEY);
+
+	/* For XATTR_ITEM, location key should be all 0 */
+	if (item_key.type == BTRFS_XATTR_ITEM_KEY) {
+		if (key->type != 0 || key->objectid != 0 || key->offset != 0)
+			return -EUCLEAN;
+		return 0;
+	}
+
+	if ((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
+	     key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
+	    key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
+	    key->objectid != BTRFS_FREE_INO_OBJECTID) {
+		if (is_inode_item)
+			generic_err(leaf, slot,
+	"invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
+				key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
+				BTRFS_FIRST_FREE_OBJECTID,
+				BTRFS_LAST_FREE_OBJECTID,
+				BTRFS_FREE_INO_OBJECTID);
+		else
+			dir_item_err(leaf, slot,
+"invalid location key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
+				key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
+				BTRFS_FIRST_FREE_OBJECTID,
+				BTRFS_LAST_FREE_OBJECTID,
+				BTRFS_FREE_INO_OBJECTID);
+		return -EUCLEAN;
+	}
+	if (key->offset != 0) {
+		if (is_inode_item)
+			inode_item_err(leaf, slot,
+				       "invalid key offset: has %llu expect 0",
+				       key->offset);
+		else
+			dir_item_err(leaf, slot,
+				"invalid location key offset:has %llu expect 0",
+				key->offset);
+		return -EUCLEAN;
+	}
+	return 0;
+}
+
 static int check_dir_item(struct extent_buffer *leaf,
 			  struct btrfs_key *key, struct btrfs_key *prev_key,
 			  int slot)
@@ -798,25 +852,12 @@ static int check_inode_item(struct extent_buffer *leaf,
 	u64 super_gen = btrfs_super_generation(fs_info->super_copy);
 	u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
 	u32 mode;
+	int ret;
+
+	ret = check_inode_key(leaf, key, slot);
+	if (ret < 0)
+		return ret;
 
-	if ((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
-	     key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
-	    key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
-	    key->objectid != BTRFS_FREE_INO_OBJECTID) {
-		generic_err(leaf, slot,
-	"invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
-			    key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
-			    BTRFS_FIRST_FREE_OBJECTID,
-			    BTRFS_LAST_FREE_OBJECTID,
-			    BTRFS_FREE_INO_OBJECTID);
-		return -EUCLEAN;
-	}
-	if (key->offset != 0) {
-		inode_item_err(leaf, slot,
-			"invalid key offset: has %llu expect 0",
-			key->offset);
-		return -EUCLEAN;
-	}
 	iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
 
 	/* Here we use super block generation + 1 to handle log tree */
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 3/4] btrfs: tree-checker: Refactor root key check into separate function
  2019-12-09 10:54 [PATCH 1/4] btrfs: tree-checker: Clean up fs_info parameter from error message wrapper Qu Wenruo
  2019-12-09 10:54 ` [PATCH 2/4] btrfs: tree-checker: Refactor inode key check into seperate function Qu Wenruo
@ 2019-12-09 10:54 ` Qu Wenruo
  2019-12-09 12:12   ` Nikolay Borisov
  2019-12-09 10:54 ` [PATCH 4/4] btrfs: tree-checker: Verify location key for DIR_ITEM/DIR_INDEX Qu Wenruo
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 10+ messages in thread
From: Qu Wenruo @ 2019-12-09 10:54 UTC (permalink / raw)
  To: linux-btrfs

ROOT_ITEM key check itself is not as simple as single line check, and
will be reused for both ROOT_ITEM and DIR_ITEM/DIR_INDEX location key
check, so refactor such check into check_root_key().

Also since we are here, fix a comment error about ROOT_ITEM offset,
which is transid of snapshot creation, not some "older kernel behavior".

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/tree-checker.c | 61 +++++++++++++++++++++++++++++++----------
 1 file changed, 46 insertions(+), 15 deletions(-)

diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
index 68dad9ec38dd..9a6743ee874a 100644
--- a/fs/btrfs/tree-checker.c
+++ b/fs/btrfs/tree-checker.c
@@ -413,6 +413,48 @@ static int check_inode_key(struct extent_buffer *leaf, struct btrfs_key *key,
 	return 0;
 }
 
+static int check_root_key(struct extent_buffer *leaf, struct btrfs_key *key,
+			  int slot)
+{
+	struct btrfs_key item_key;
+	bool is_root_item;
+
+	btrfs_item_key_to_cpu(leaf, &item_key, slot);
+	is_root_item = (item_key.type == BTRFS_ROOT_ITEM_KEY);
+
+	/* No such tree id */
+	if (key->objectid == 0) {
+		if (is_root_item)
+			generic_err(leaf, slot, "invalid root id 0");
+		else
+			dir_item_err(leaf, slot,
+				     "invalid location key root id 0");
+		return -EUCLEAN;
+	}
+
+	/* DIR_ITEM/INDEX/INODE_REF is not allowed to point to non-fs trees */
+	if (!is_fstree(key->objectid) && !is_root_item) {
+		dir_item_err(leaf, slot,
+		"invalid location key objectid, have %llu expect [%llu, %llu]",
+				key->objectid, BTRFS_FIRST_FREE_OBJECTID,
+				BTRFS_LAST_FREE_OBJECTID);
+		return -EUCLEAN;
+	}
+	/*
+	 * ROOT_ITEM with non-zero offset means, this is a snapshot, created at
+	 * @offset transid.
+	 * Furthermore, for location key in DIR_ITEM, its offset is always -1.
+	 *
+	 * So here we only check offset for reloc tree whose key->offset must
+	 * be a valid tree.
+	 */
+	if (key->objectid == BTRFS_TREE_RELOC_OBJECTID && key->offset == 0) {
+		generic_err(leaf, slot, "invalid root id 0 for reloc tree");
+		return -EUCLEAN;
+	}
+	return 0;
+}
+
 static int check_dir_item(struct extent_buffer *leaf,
 			  struct btrfs_key *key, struct btrfs_key *prev_key,
 			  int slot)
@@ -925,22 +967,11 @@ static int check_root_item(struct extent_buffer *leaf, struct btrfs_key *key,
 	struct btrfs_root_item ri;
 	const u64 valid_root_flags = BTRFS_ROOT_SUBVOL_RDONLY |
 				     BTRFS_ROOT_SUBVOL_DEAD;
+	int ret;
 
-	/* No such tree id */
-	if (key->objectid == 0) {
-		generic_err(leaf, slot, "invalid root id 0");
-		return -EUCLEAN;
-	}
-
-	/*
-	 * Some older kernel may create ROOT_ITEM with non-zero offset, so here
-	 * we only check offset for reloc tree whose key->offset must be a
-	 * valid tree.
-	 */
-	if (key->objectid == BTRFS_TREE_RELOC_OBJECTID && key->offset == 0) {
-		generic_err(leaf, slot, "invalid root id 0 for reloc tree");
-		return -EUCLEAN;
-	}
+	ret = check_root_key(leaf, key, slot);
+	if (ret < 0)
+		return ret;
 
 	if (btrfs_item_size_nr(leaf, slot) != sizeof(ri)) {
 		generic_err(leaf, slot,
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH 4/4] btrfs: tree-checker: Verify location key for DIR_ITEM/DIR_INDEX
  2019-12-09 10:54 [PATCH 1/4] btrfs: tree-checker: Clean up fs_info parameter from error message wrapper Qu Wenruo
  2019-12-09 10:54 ` [PATCH 2/4] btrfs: tree-checker: Refactor inode key check into seperate function Qu Wenruo
  2019-12-09 10:54 ` [PATCH 3/4] btrfs: tree-checker: Refactor root key check into separate function Qu Wenruo
@ 2019-12-09 10:54 ` Qu Wenruo
  2019-12-09 12:07 ` [PATCH 1/4] btrfs: tree-checker: Clean up fs_info parameter from error message wrapper Nikolay Borisov
  2020-01-02 15:04 ` David Sterba
  4 siblings, 0 replies; 10+ messages in thread
From: Qu Wenruo @ 2019-12-09 10:54 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Mike Gilbert

[PROBLEM]
There is a user report in the mail list, showing the following corrupted
tree blocks:

       item 62 key (486836 DIR_ITEM 2543451757) itemoff 6273 itemsize 74
               location key (4065004 INODE_ITEM 1073741824) type FILE
               transid 21397 data_len 0 name_len 44
               name: 0390cb341d248c589c419007da68b2-7351.manifest

Note that location key, its offset should be 0 for all INODE_ITEMS.

This caused btrfs kernel failed to lookup the inode.

[CAUSE]
That offending value, 1073741824, is 0x40000000. So this looks like a
memory bit flip.

[FIX]
This patch will enhance tree-checker to check location key of
DIR_INDEX/DIR_ITEM/XATTR_ITEM.

There are several different combinations needs to check:
- item_key.type == DIR_INDEX/DIR_ITEM
  * location_key.type == BTRFS_INODE_ITEM_KEY
    This location_key should follow the check in inode_item check.
  * location_key.type == BTRFS_ROOT_ITEM_KEY
    Despite the existing check, DIR_INDEX/DIR_ITEM can only points to
    subvolume trees.
  * All other keys are not allowed.

- item_key.type == XATTR_ITEM
  location_key should be all 0.

Reported-by: Mike Gilbert <floppymaster@gmail.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/tree-checker.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
index 9a6743ee874a..7bd1a2f986c4 100644
--- a/fs/btrfs/tree-checker.c
+++ b/fs/btrfs/tree-checker.c
@@ -468,12 +468,14 @@ static int check_dir_item(struct extent_buffer *leaf,
 		return -EUCLEAN;
 	di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
 	while (cur < item_size) {
+		struct btrfs_key location_key;
 		u32 name_len;
 		u32 data_len;
 		u32 max_name_len;
 		u32 total_size;
 		u32 name_hash;
 		u8 dir_type;
+		int ret;
 
 		/* header itself should not cross item boundary */
 		if (cur + sizeof(*di) > item_size) {
@@ -483,6 +485,24 @@ static int check_dir_item(struct extent_buffer *leaf,
 			return -EUCLEAN;
 		}
 
+		/* Location key check */
+		btrfs_dir_item_key_to_cpu(leaf, di, &location_key);
+		if (location_key.type == BTRFS_ROOT_ITEM_KEY) {
+			ret = check_root_key(leaf, &location_key, slot);
+			if (ret < 0)
+				return ret;
+		} else if (location_key.type == BTRFS_INODE_ITEM_KEY ||
+			   location_key.type == 0) {
+			ret = check_inode_key(leaf, &location_key, slot);
+			if (ret < 0)
+				return ret;
+		} else {
+			dir_item_err(leaf, slot,
+			"invalid location key type, have %u, expect %u or %u",
+				     location_key.type, BTRFS_ROOT_ITEM_KEY,
+				     BTRFS_INODE_ITEM_KEY);
+			return -EUCLEAN;
+		}
 		/* dir type check */
 		dir_type = btrfs_dir_type(leaf, di);
 		if (dir_type >= BTRFS_FT_MAX) {
-- 
2.24.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH 1/4] btrfs: tree-checker: Clean up fs_info parameter from error message wrapper
  2019-12-09 10:54 [PATCH 1/4] btrfs: tree-checker: Clean up fs_info parameter from error message wrapper Qu Wenruo
                   ` (2 preceding siblings ...)
  2019-12-09 10:54 ` [PATCH 4/4] btrfs: tree-checker: Verify location key for DIR_ITEM/DIR_INDEX Qu Wenruo
@ 2019-12-09 12:07 ` Nikolay Borisov
  2020-01-02 15:04 ` David Sterba
  4 siblings, 0 replies; 10+ messages in thread
From: Nikolay Borisov @ 2019-12-09 12:07 UTC (permalink / raw)
  To: Qu Wenruo, linux-btrfs



On 9.12.19 г. 12:54 ч., Qu Wenruo wrote:
> The @fs_info parameter can be extracted from extent_buffer structure,
> and there are already some wrappers getting rid of the @fs_info
> parameter.
> 
> This patch will finish the cleanup.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>

In this particular case I don't think it makes much of a difference but
the changes itself is ok.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 2/4] btrfs: tree-checker: Refactor inode key check into seperate function
  2019-12-09 10:54 ` [PATCH 2/4] btrfs: tree-checker: Refactor inode key check into seperate function Qu Wenruo
@ 2019-12-09 12:11   ` Nikolay Borisov
  2019-12-10 14:42   ` Su Yue
  1 sibling, 0 replies; 10+ messages in thread
From: Nikolay Borisov @ 2019-12-09 12:11 UTC (permalink / raw)
  To: Qu Wenruo, linux-btrfs



On 9.12.19 г. 12:54 ч., Qu Wenruo wrote:
> Inode key check is not as easy as several lines, and it will be called
> in more than one location (INODE_ITEM check and
> DIR_ITEM/DIR_INDEX/XATTR_ITEM location key check).
> 
> So here refactor such check into check_inode_key().
> 
> And add extra checks for XATTR_ITEM.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>

Reviewed-by: Nikolay Borisov <nborisov@suse.com>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 3/4] btrfs: tree-checker: Refactor root key check into separate function
  2019-12-09 10:54 ` [PATCH 3/4] btrfs: tree-checker: Refactor root key check into separate function Qu Wenruo
@ 2019-12-09 12:12   ` Nikolay Borisov
  0 siblings, 0 replies; 10+ messages in thread
From: Nikolay Borisov @ 2019-12-09 12:12 UTC (permalink / raw)
  To: Qu Wenruo, linux-btrfs



On 9.12.19 г. 12:54 ч., Qu Wenruo wrote:
> ROOT_ITEM key check itself is not as simple as single line check, and
> will be reused for both ROOT_ITEM and DIR_ITEM/DIR_INDEX location key
> check, so refactor such check into check_root_key().
> 
> Also since we are here, fix a comment error about ROOT_ITEM offset,
> which is transid of snapshot creation, not some "older kernel behavior".
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>

Reviewed-by : Nikolay Borisov <nborisov@suse.com>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 2/4] btrfs: tree-checker: Refactor inode key check into seperate function
  2019-12-09 10:54 ` [PATCH 2/4] btrfs: tree-checker: Refactor inode key check into seperate function Qu Wenruo
  2019-12-09 12:11   ` Nikolay Borisov
@ 2019-12-10 14:42   ` Su Yue
  2019-12-11  0:24     ` Qu WenRuo
  1 sibling, 1 reply; 10+ messages in thread
From: Su Yue @ 2019-12-10 14:42 UTC (permalink / raw)
  To: Qu Wenruo, linux-btrfs



On 2019/12/9 6:54 PM, Qu Wenruo wrote:
> Inode key check is not as easy as several lines, and it will be called
> in more than one location (INODE_ITEM check and
> DIR_ITEM/DIR_INDEX/XATTR_ITEM location key check).
>
> So here refactor such check into check_inode_key().
>
> And add extra checks for XATTR_ITEM.
>
> Signed-off-by: Qu Wenruo <wqu@suse.com>
> ---
>   fs/btrfs/tree-checker.c | 77 +++++++++++++++++++++++++++++++----------
>   1 file changed, 59 insertions(+), 18 deletions(-)
>
> diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
> index 6cb49c75c5e1..68dad9ec38dd 100644
> --- a/fs/btrfs/tree-checker.c
> +++ b/fs/btrfs/tree-checker.c
> @@ -359,6 +359,60 @@ static int check_csum_item(struct extent_buffer *leaf, struct btrfs_key *key,
>   	return 0;
>   }
>
> +/* Inode item error output has the same format as dir_item_err() */
> +#define inode_item_err(eb, slot, fmt, ...)			\
> +	dir_item_err(eb, slot, fmt, __VA_ARGS__)
> +
> +static int check_inode_key(struct extent_buffer *leaf, struct btrfs_key *key,
> +			   int slot)

The function name is confusing to me. It checks xattr which is not
inode related obviously.

I saw the 4th patch. How about introduction of new function
check_location_key(), then calls check_root_key() , check_inode_key()
and checks xatrr case inside?

Others in the patchset seem fine to me.


Thanks
> +{
> +	struct btrfs_key item_key;
> +	bool is_inode_item;
> +
> +	btrfs_item_key_to_cpu(leaf, &item_key, slot);
> +	is_inode_item = (item_key.type == BTRFS_INODE_ITEM_KEY);
> +
> +	/* For XATTR_ITEM, location key should be all 0 */
> +	if (item_key.type == BTRFS_XATTR_ITEM_KEY) {
> +		if (key->type != 0 || key->objectid != 0 || key->offset != 0)
> +			return -EUCLEAN;
> +		return 0;
> +	}
> +
> +	if ((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
> +	     key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
> +	    key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
> +	    key->objectid != BTRFS_FREE_INO_OBJECTID) {
> +		if (is_inode_item)
> +			generic_err(leaf, slot,
> +	"invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
> +				key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
> +				BTRFS_FIRST_FREE_OBJECTID,
> +				BTRFS_LAST_FREE_OBJECTID,
> +				BTRFS_FREE_INO_OBJECTID);
> +		else
> +			dir_item_err(leaf, slot,
> +"invalid location key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
> +				key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
> +				BTRFS_FIRST_FREE_OBJECTID,
> +				BTRFS_LAST_FREE_OBJECTID,
> +				BTRFS_FREE_INO_OBJECTID);
> +		return -EUCLEAN;
> +	}
> +	if (key->offset != 0) {
> +		if (is_inode_item)
> +			inode_item_err(leaf, slot,
> +				       "invalid key offset: has %llu expect 0",
> +				       key->offset);
> +		else
> +			dir_item_err(leaf, slot,
> +				"invalid location key offset:has %llu expect 0",
> +				key->offset);
> +		return -EUCLEAN;
> +	}
> +	return 0;
> +}
> +
>   static int check_dir_item(struct extent_buffer *leaf,
>   			  struct btrfs_key *key, struct btrfs_key *prev_key,
>   			  int slot)
> @@ -798,25 +852,12 @@ static int check_inode_item(struct extent_buffer *leaf,
>   	u64 super_gen = btrfs_super_generation(fs_info->super_copy);
>   	u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
>   	u32 mode;
> +	int ret;
> +
> +	ret = check_inode_key(leaf, key, slot);
> +	if (ret < 0)
> +		return ret;
>
> -	if ((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
> -	     key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
> -	    key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
> -	    key->objectid != BTRFS_FREE_INO_OBJECTID) {
> -		generic_err(leaf, slot,
> -	"invalid key objectid: has %llu expect %llu or [%llu, %llu] or %llu",
> -			    key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
> -			    BTRFS_FIRST_FREE_OBJECTID,
> -			    BTRFS_LAST_FREE_OBJECTID,
> -			    BTRFS_FREE_INO_OBJECTID);
> -		return -EUCLEAN;
> -	}
> -	if (key->offset != 0) {
> -		inode_item_err(leaf, slot,
> -			"invalid key offset: has %llu expect 0",
> -			key->offset);
> -		return -EUCLEAN;
> -	}
>   	iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
>
>   	/* Here we use super block generation + 1 to handle log tree */
>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 2/4] btrfs: tree-checker: Refactor inode key check into seperate function
  2019-12-10 14:42   ` Su Yue
@ 2019-12-11  0:24     ` Qu WenRuo
  0 siblings, 0 replies; 10+ messages in thread
From: Qu WenRuo @ 2019-12-11  0:24 UTC (permalink / raw)
  To: Su Yue, linux-btrfs



On 2019/12/10 下午10:42, Su Yue wrote:
> 
> 
> On 2019/12/9 6:54 PM, Qu Wenruo wrote:
>> Inode key check is not as easy as several lines, and it will be called
>> in more than one location (INODE_ITEM check and
>> DIR_ITEM/DIR_INDEX/XATTR_ITEM location key check).
>>
>> So here refactor such check into check_inode_key().
>>
>> And add extra checks for XATTR_ITEM.
>>
>> Signed-off-by: Qu Wenruo <wqu@suse.com>
>> ---
>>   fs/btrfs/tree-checker.c | 77 +++++++++++++++++++++++++++++++----------
>>   1 file changed, 59 insertions(+), 18 deletions(-)
>>
>> diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
>> index 6cb49c75c5e1..68dad9ec38dd 100644
>> --- a/fs/btrfs/tree-checker.c
>> +++ b/fs/btrfs/tree-checker.c
>> @@ -359,6 +359,60 @@ static int check_csum_item(struct extent_buffer
>> *leaf, struct btrfs_key *key,
>>       return 0;
>>   }
>>
>> +/* Inode item error output has the same format as dir_item_err() */
>> +#define inode_item_err(eb, slot, fmt, ...)            \
>> +    dir_item_err(eb, slot, fmt, __VA_ARGS__)
>> +
>> +static int check_inode_key(struct extent_buffer *leaf, struct
>> btrfs_key *key,
>> +               int slot)
> 
> The function name is confusing to me. It checks xattr which is not
> inode related obviously.

If renamed to check_location_key() then the call site in
check_inode_item() will be very strange...

Thanks,
Qu

> 
> I saw the 4th patch. How about introduction of new function
> check_location_key(), then calls check_root_key() , check_inode_key()
> and checks xatrr case inside?
> 
> Others in the patchset seem fine to me.
> 
> 
> Thanks
>> +{
>> +    struct btrfs_key item_key;
>> +    bool is_inode_item;
>> +
>> +    btrfs_item_key_to_cpu(leaf, &item_key, slot);
>> +    is_inode_item = (item_key.type == BTRFS_INODE_ITEM_KEY);
>> +
>> +    /* For XATTR_ITEM, location key should be all 0 */
>> +    if (item_key.type == BTRFS_XATTR_ITEM_KEY) {
>> +        if (key->type != 0 || key->objectid != 0 || key->offset != 0)
>> +            return -EUCLEAN;
>> +        return 0;
>> +    }
>> +
>> +    if ((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
>> +         key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
>> +        key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
>> +        key->objectid != BTRFS_FREE_INO_OBJECTID) {
>> +        if (is_inode_item)
>> +            generic_err(leaf, slot,
>> +    "invalid key objectid: has %llu expect %llu or [%llu, %llu] or
>> %llu",
>> +                key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
>> +                BTRFS_FIRST_FREE_OBJECTID,
>> +                BTRFS_LAST_FREE_OBJECTID,
>> +                BTRFS_FREE_INO_OBJECTID);
>> +        else
>> +            dir_item_err(leaf, slot,
>> +"invalid location key objectid: has %llu expect %llu or [%llu, %llu]
>> or %llu",
>> +                key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
>> +                BTRFS_FIRST_FREE_OBJECTID,
>> +                BTRFS_LAST_FREE_OBJECTID,
>> +                BTRFS_FREE_INO_OBJECTID);
>> +        return -EUCLEAN;
>> +    }
>> +    if (key->offset != 0) {
>> +        if (is_inode_item)
>> +            inode_item_err(leaf, slot,
>> +                       "invalid key offset: has %llu expect 0",
>> +                       key->offset);
>> +        else
>> +            dir_item_err(leaf, slot,
>> +                "invalid location key offset:has %llu expect 0",
>> +                key->offset);
>> +        return -EUCLEAN;
>> +    }
>> +    return 0;
>> +}
>> +
>>   static int check_dir_item(struct extent_buffer *leaf,
>>                 struct btrfs_key *key, struct btrfs_key *prev_key,
>>                 int slot)
>> @@ -798,25 +852,12 @@ static int check_inode_item(struct extent_buffer
>> *leaf,
>>       u64 super_gen = btrfs_super_generation(fs_info->super_copy);
>>       u32 valid_mask = (S_IFMT | S_ISUID | S_ISGID | S_ISVTX | 0777);
>>       u32 mode;
>> +    int ret;
>> +
>> +    ret = check_inode_key(leaf, key, slot);
>> +    if (ret < 0)
>> +        return ret;
>>
>> -    if ((key->objectid < BTRFS_FIRST_FREE_OBJECTID ||
>> -         key->objectid > BTRFS_LAST_FREE_OBJECTID) &&
>> -        key->objectid != BTRFS_ROOT_TREE_DIR_OBJECTID &&
>> -        key->objectid != BTRFS_FREE_INO_OBJECTID) {
>> -        generic_err(leaf, slot,
>> -    "invalid key objectid: has %llu expect %llu or [%llu, %llu] or
>> %llu",
>> -                key->objectid, BTRFS_ROOT_TREE_DIR_OBJECTID,
>> -                BTRFS_FIRST_FREE_OBJECTID,
>> -                BTRFS_LAST_FREE_OBJECTID,
>> -                BTRFS_FREE_INO_OBJECTID);
>> -        return -EUCLEAN;
>> -    }
>> -    if (key->offset != 0) {
>> -        inode_item_err(leaf, slot,
>> -            "invalid key offset: has %llu expect 0",
>> -            key->offset);
>> -        return -EUCLEAN;
>> -    }
>>       iitem = btrfs_item_ptr(leaf, slot, struct btrfs_inode_item);
>>
>>       /* Here we use super block generation + 1 to handle log tree */
>>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH 1/4] btrfs: tree-checker: Clean up fs_info parameter from error message wrapper
  2019-12-09 10:54 [PATCH 1/4] btrfs: tree-checker: Clean up fs_info parameter from error message wrapper Qu Wenruo
                   ` (3 preceding siblings ...)
  2019-12-09 12:07 ` [PATCH 1/4] btrfs: tree-checker: Clean up fs_info parameter from error message wrapper Nikolay Borisov
@ 2020-01-02 15:04 ` David Sterba
  4 siblings, 0 replies; 10+ messages in thread
From: David Sterba @ 2020-01-02 15:04 UTC (permalink / raw)
  To: Qu Wenruo; +Cc: linux-btrfs

On Mon, Dec 09, 2019 at 06:54:32PM +0800, Qu Wenruo wrote:
> The @fs_info parameter can be extracted from extent_buffer structure,
> and there are already some wrappers getting rid of the @fs_info
> parameter.
> 
> This patch will finish the cleanup.
> 
> Signed-off-by: Qu Wenruo <wqu@suse.com>

1-4 added to misc-next, thanks.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2020-01-02 15:04 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-12-09 10:54 [PATCH 1/4] btrfs: tree-checker: Clean up fs_info parameter from error message wrapper Qu Wenruo
2019-12-09 10:54 ` [PATCH 2/4] btrfs: tree-checker: Refactor inode key check into seperate function Qu Wenruo
2019-12-09 12:11   ` Nikolay Borisov
2019-12-10 14:42   ` Su Yue
2019-12-11  0:24     ` Qu WenRuo
2019-12-09 10:54 ` [PATCH 3/4] btrfs: tree-checker: Refactor root key check into separate function Qu Wenruo
2019-12-09 12:12   ` Nikolay Borisov
2019-12-09 10:54 ` [PATCH 4/4] btrfs: tree-checker: Verify location key for DIR_ITEM/DIR_INDEX Qu Wenruo
2019-12-09 12:07 ` [PATCH 1/4] btrfs: tree-checker: Clean up fs_info parameter from error message wrapper Nikolay Borisov
2020-01-02 15:04 ` David Sterba

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).