From patchwork Thu May 4 11:04:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13231044 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 801C1C7EE23 for ; Thu, 4 May 2023 11:04:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230119AbjEDLEq (ORCPT ); Thu, 4 May 2023 07:04:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39322 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230167AbjEDLEg (ORCPT ); Thu, 4 May 2023 07:04:36 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 854ED19B7 for ; Thu, 4 May 2023 04:04:33 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 2252B6176A for ; Thu, 4 May 2023 11:04:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 188C3C4339B for ; Thu, 4 May 2023 11:04:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1683198272; bh=l638eeBXZrWgfTqHYaOXhZ5Vsvyl6FKBGbB66dRgT2o=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ecDTMV1G42zux+1y50Wa+9eFL9BRWwy5JnWH/sSsApzCgrQIfREClPknb2TQ1Hap7 S8qaeltpXWc4KCzEL06C85z2MtNM3uWxAWTz/QPVughKsdhH4EVyOw75lf0AtxcSAh h9p4m9y+kkJ8fzWLYLZJ3lY7t6bbDdkfwMSs+kadcFOFanQfWVB3ycMKDCGQfu6fvF Qfduoc7HU8lsrK6KZypMVW7iRtvJ/jPnnsXkiruTVXRQZULBrrGS7zfPyvwBNLbrbk b4NiCr4xju593bWWdRwmaqhrJZWyUht92ebBs+buClbYwrokY/r6Ie6jTozkgoR5Xr smxW4x2iaW3dA== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 1/9] btrfs: fix space cache inconsistency after error loading it from disk Date: Thu, 4 May 2023 12:04:18 +0100 Message-Id: <4f6a98cfcca190f03b9a729d7cf9c191c29032da.1683196407.git.fdmanana@suse.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org From: Filipe Manana 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: a67509c30079 ("Btrfs: add a io_ctl struct and helpers for dealing with the space cache") Signed-off-by: Filipe Manana Reviewed-by: Anand Jain --- fs/btrfs/free-space-cache.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index d84cef89cdff..cf98a3c05480 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -870,15 +870,16 @@ static int __load_free_space_cache(struct btrfs_root *root, struct inode *inode, } spin_lock(&ctl->tree_lock); ret = link_free_space(ctl, e); - ctl->total_bitmaps++; - recalculate_thresholds(ctl); - spin_unlock(&ctl->tree_lock); if (ret) { + spin_unlock(&ctl->tree_lock); btrfs_err(fs_info, "Duplicate entries in free space cache, dumping"); kmem_cache_free(btrfs_free_space_cachep, e); goto free_cache; } + ctl->total_bitmaps++; + recalculate_thresholds(ctl); + spin_unlock(&ctl->tree_lock); list_add_tail(&e->list, &bitmaps); }