Skip to content

Commit 0004ff1

Browse files
fdmananakdave
authored andcommitted
btrfs: fix space cache inconsistency after error loading it from disk
When loading a free space cache from disk, at __load_free_space_cache(), if we fail to insert a bitmap entry, we still increment the number of total bitmaps in the btrfs_free_space_ctl structure, which is incorrect since we failed to add the bitmap entry. On error we then empty the cache by calling __btrfs_remove_free_space_cache(), which will result in getting the total bitmaps counter set to 1. A failure to load a free space cache is not critical, so if a failure happens we just rebuild the cache by scanning the extent tree, which happens at block-group.c:caching_thread(). Yet the failure will result in having the total bitmaps of the btrfs_free_space_ctl always bigger by 1 then the number of bitmap entries we have. So fix this by having the total bitmaps counter be incremented only if we successfully added the bitmap entry. Fixes: a67509c ("Btrfs: add a io_ctl struct and helpers for dealing with the space cache") Reviewed-by: Anand Jain <[email protected]> CC: [email protected] # 4.4+ Signed-off-by: Filipe Manana <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent c87f318 commit 0004ff1

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

fs/btrfs/free-space-cache.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -870,15 +870,16 @@ static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
870870
}
871871
spin_lock(&ctl->tree_lock);
872872
ret = link_free_space(ctl, e);
873-
ctl->total_bitmaps++;
874-
recalculate_thresholds(ctl);
875-
spin_unlock(&ctl->tree_lock);
876873
if (ret) {
874+
spin_unlock(&ctl->tree_lock);
877875
btrfs_err(fs_info,
878876
"Duplicate entries in free space cache, dumping");
879877
kmem_cache_free(btrfs_free_space_cachep, e);
880878
goto free_cache;
881879
}
880+
ctl->total_bitmaps++;
881+
recalculate_thresholds(ctl);
882+
spin_unlock(&ctl->tree_lock);
882883
list_add_tail(&e->list, &bitmaps);
883884
}
884885

0 commit comments

Comments
 (0)