]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
btrfs: handle unexpected exact match in btrfs_set_inode_index_count()
authorAdarsh Das <adarshdas950@gmail.com>
Tue, 3 Feb 2026 17:23:56 +0000 (22:53 +0530)
committerDavid Sterba <dsterba@suse.com>
Wed, 18 Feb 2026 14:25:53 +0000 (15:25 +0100)
We search with offset (u64)-1 which should never match exactly.
Previously the code silently returned success without setting the index
count. Now logs an error and return -EUCLEAN instead.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Adarsh Das <adarshdas950@gmail.com>
Reviewed-by: David Sterba <dsterba@suse.com>,
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/inode.c

index 1aebd2ee2704e3f5f3b6e59d716e47d5a36d89e1..b6c763a17406bd3e02a437ff1fa2d2635c49e8c9 100644 (file)
@@ -6149,9 +6149,18 @@ static int btrfs_set_inode_index_count(struct btrfs_inode *inode)
        ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
        if (ret < 0)
                return ret;
-       /* FIXME: we should be able to handle this */
-       if (ret == 0)
-               return ret;
+
+       if (unlikely(ret == 0)) {
+               /*
+                * Key with offset -1 found, there would have to exist a dir
+                * index item with such offset, but this is out of the valid
+                * range.
+                */
+               btrfs_err(root->fs_info,
+                         "unexpected exact match for DIR_INDEX key, inode %llu",
+                         btrfs_ino(inode));
+               return -EUCLEAN;
+       }
 
        if (path->slots[0] == 0) {
                inode->index_cnt = BTRFS_DIR_START_INDEX;