From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796387949; b=RH3iESa00U8XvPzxsuj5jElf/rdIhN7uCDTmM2UV6XGbeDYS5q2Gis1yfpGlEL3vaYHj A9x+ex8mGcudR7e7dzuvcpbQxS0gqEtffaI1BN7t167qXAuL+j0SSszlC4/TpbUa9lSLY F0O7H2WW9tfOWs0NN/ox0xvD+g/SePZ9oXwsqRdbxldWquhvdnamK9ycS0cTtoaQ4Nb1i YgzLrTi4V192KIZA3p4u3ft6H4Oic9RNbETmiAL1hJoQjErs1i05XN/jowt8ZdTCwU9UJ MWiFEYSN0SvmgagmuIqfgMyBC9go/XixwImRApxvl25mqZGIFCurEfB6uNlRC7bSR4Q== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796387949; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=neU/4Ydv2t1L6AP8NiEukbtR0nHdf1eNa8xgs/Uyipg=; b=veMOxk6RX5yGpwd7l7Ltt6d730D7DDKp0IeIGUNis5/oJmF9jMtATXGaMilA9OOecL4v 14W9ST18p0k459oX6+QBZgWu6gaUZUFxwGWtL1B6bVdKD9epXMCUECt80lZQSzkqC6Gbt AS4iyip17pgjja8jkUrUa0FR6jgKmwsASQV/g7V6yZOgzolHkHrUXjdcNpxD0lEDoejOc qn+LvrMR31XFmPXLEbrAgeuQ2Wr7888w0Vp2WxKdLHthLHQYpFFRpl6qHgHZKpN5LOK72 xwWVZ9rIJNsUrkpE+jgloHYh1yDK0twJyASZLq+YQtQF/liYHUglEMvA/xDiHnFZsnA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=F7EGZD6D; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 38689606A32D; Fri, 4 Dec 2026 12:39:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=F7EGZD6D Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6D0F81F000C2; Fri, 4 Dec 2026 12:39:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796387948; bh=neU/4Ydv2t1L6AP8NiEukbtR0nHdf1eNa8xgs/Uyipg=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=F7EGZD6DFGBl0H6SGLbr7GOHHH0To/h2+D2i9ZgXi/UjjZzqQAsRDyvGhGez5djHa Nk+Go0GnqXxVws+vnWz0nnSsem8V33g0D+YVyTUpjxXa9F883MytqhM25aE5IDk00T Xje/Qoa1aikJkU6E/1eeVFRV3jP8pzFvw91BOrqA= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Tal Zussman , David Sterba , Sasha Levin Subject: [PATCH 6.1 0407/1059] btrfs: update the inode item when removing a free space inode Date: Fri, 4 Dec 2026 18:58:58 +0900 Message-ID: <20261204100654.881497505@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100643.980430235@linuxfoundation.org> References: <20261204100643.980430235@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.1-stable review patch. If anyone has any objections, please let me know. ------------------ From: Tal Zussman [ Upstream commit 4e89aff9ad6fcbec555c38169807c4023f93450f ] btrfs_remove_free_space_inode() adds an orphan item for the free space inode and clears its link count in memory, but never writes the inode item. The item is only removed once the delayed iput evicts the inode, which happens in a later transaction, from the cleaner thread or a throttled transaction end. If we crash in between, or if evict can't reserve space for the truncation and leaves the orphan item for the next mount, the on-disk inode still has a link count of 1. btrfs_orphan_cleanup() then treats it as an inode with links and only deletes the orphan item. The free space header item is already gone at that point, so nothing ever finds the inode again, and its data extents are leaked. Write the zero link count to the inode item in the same transaction as the orphan item, so the orphan cleanup evicts the inode instead. Account for the extra tree root item update when removing a block group. Fixes: 455757c322cc ("Btrfs: delay iput when deleting a block group") Assisted-by: LLM Signed-off-by: Tal Zussman Signed-off-by: David Sterba [ sashal: adapted to older APIs: kept the extent-map lookup in block-group.c, used the three-argument btrfs_update_inode(), passed a struct inode to btrfs_add_delayed_iput(), and used goto out to free the path. ] Signed-off-by: Sasha Levin --- fs/btrfs/block-group.c | 6 ++++-- fs/btrfs/free-space-cache.c | 10 ++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/fs/btrfs/block-group.c b/fs/btrfs/block-group.c index a8f096b46b7b2..df742b4d2fd7a 100644 --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -1179,12 +1179,14 @@ struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( ASSERT(em && em->start == chunk_offset); /* - * We need to reserve 3 + N units from the metadata space info in order + * We need to reserve 4 + N units from the metadata space info in order * to remove a block group (done at btrfs_remove_chunk() and at * btrfs_remove_block_group()), which are used for: * * 1 unit for adding the free space inode's orphan (located in the tree * of tree roots). + * 1 unit for updating the free space inode's item (located in the tree + * of tree roots). * 1 unit for deleting the block group item (located in the extent * tree). * 1 unit for deleting the free space item (located in tree of tree @@ -1198,7 +1200,7 @@ struct btrfs_trans_handle *btrfs_start_trans_remove_block_group( * btrfs_remove_chunk() through a call to check_system_chunk(). */ map = em->map_lookup; - num_items = 3 + map->num_stripes; + num_items = 4 + map->num_stripes; free_extent_map(em); return btrfs_start_transaction_fallback_global_rsv(root, num_items); diff --git a/fs/btrfs/free-space-cache.c b/fs/btrfs/free-space-cache.c index 92933cc5596e0..b9106a28c87cd 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -260,6 +260,16 @@ int btrfs_remove_free_space_inode(struct btrfs_trans_handle *trans, goto out; } clear_nlink(inode); + /* + * Persist the zero link count, so that if the delayed iput below + * doesn't get to evict the inode, the orphan cleanup on the next mount + * evicts it instead of only deleting the orphan item. + */ + ret = btrfs_update_inode(trans, BTRFS_I(inode)->root, BTRFS_I(inode)); + if (ret) { + btrfs_add_delayed_iput(inode); + goto out; + } /* One for the block groups ref */ spin_lock(&block_group->lock); if (test_and_clear_bit(BLOCK_GROUP_FLAG_IREF, &block_group->runtime_flags)) { -- 2.53.0