Btrfs doesn't reuse devid, thus if we add and delete device in a loop,
we can increase devid to higher value, triggering tree checker to give a
false alert.
But we still don't want to give up the devid check, so here we
compromise by setting a larger devid upper limit, 1<<32.
So crazy scripts can't bump devid to that high value easily, while we can
still detect obviously wrong devid.
Reported-by: Anand Jain <anand.jain@xxxxxxxxxx>
Fixes: ab4ba2e13346 ("btrfs: tree-checker: Verify dev item")
Signed-off-by: Qu Wenruo <wqu@xxxxxxxx>
---
fs/btrfs/tree-checker.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
index 43e488f5d063..f9d24f01801e 100644
--- a/fs/btrfs/tree-checker.c
+++ b/fs/btrfs/tree-checker.c
@@ -686,9 +686,14 @@ static void dev_item_err(const struct extent_buffer *eb, int slot,
static int check_dev_item(struct extent_buffer *leaf,
struct btrfs_key *key, int slot)
{
- struct btrfs_fs_info *fs_info = leaf->fs_info;
struct btrfs_dev_item *ditem;
- u64 max_devid = max(BTRFS_MAX_DEVS(fs_info), BTRFS_MAX_DEVS_SYS_CHUNK);
+ /*
+ * Btrfs doesn't really reuse devid, thus devid can increase to any
+ * value, but we don't believe a devid higher than (1<<32) is really
+ * valid. This could at least detect bitflip at the higher
+ * 32 bits while still consider high devid valid.
+ */
+ u64 max_devid = (1ULL << 32);
if (key->objectid != BTRFS_DEV_ITEMS_OBJECTID) {
dev_item_err(leaf, slot,
--
2.23.0