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=1796385115; b=o9J9KTFq+UYFTRqh5tI3vrZrlP7cy9MPBsOrVfNfCv+paVwyIBsq7HOL0BssuKsfn/Tm iMmrDsgIRAd5FPu3NGvBXe4gG7gHonnC0NFIMlZXizNjKz+igRbwhnm03ABPVAufaJWcN p/GjavJRpl8PeUpUWo+znOhUX79DpqoQCObZja4Ar4kJ9g4aT5q9a2EcFBH0HWfFGYGKk h4muUgasqZx2a9hVDMpNwYFfHY2uIi40n5cYz6kV7Y6furr4kns0RdZgJ5KwMOQQywl6l Avj9StOQdnsAKEwOwkLC2h/ousOMAvpsdvW2n/JTktY1XmdpFiY+K8hbZZrbTa0OWKw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796385115; 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=vd0MN2X8eoyJIFP0eiRDrzPEpgL8C5BsRqmWy5qGbGo=; b=Lr9V8J9yXjVlRIFyMH38eQP8pO7jc9ypAa1+JcbN9ZcM2vOhz/cwPoLTXuMp7Z73EHok LkQjBeN+pr1ReyGa4/hldx/+IHUTQHXrT7ZrJwmDhQjMbVPvLBl43Ab/f9OJSpfLG6Pss xNvpYYp8F+RktbVIV8A4uZSJIya8WlPtXjnzjdRuyAHZhw/rQyK3UDaXfprE+F3YDJBSb NwQ/VYGg22s6/48h6CSaNG8rHEhx7MEtVxjvnQBqO01WXIZ0dGcX1M3zF885GB6T8E4sX WozBzjNfMdxivDatfQ0eoYPl0xW+SkceNbm/tGkF56y3mMItm5bd7cD24tkXjL9gB4A== 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=jmsuZq+m; 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 4F23A606A322; Fri, 4 Dec 2026 11:51:55 +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=jmsuZq+m Received: by smtp.kernel.org (Postfix) with ESMTPSA id BE12C1F00937; Fri, 4 Dec 2026 11:51:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1796385115; bh=vd0MN2X8eoyJIFP0eiRDrzPEpgL8C5BsRqmWy5qGbGo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=jmsuZq+moUjIOhy1JBDOIj5HNfBqCSV3z0DM0Xq+KzR61gX0PYQtD4rMElM/8ochF 8fPSMsWyJIP7YCBwjIeevzbRxxApWuDjgg0OzJzeBuXKsQ70HeapbCaO5kcaK5J546 jr9zP1VE/inTBumouHyaYBrU7VNgnqpUOonx9POM= 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.6 0425/1167] btrfs: update the inode item when removing a free space inode Date: Fri, 4 Dec 2026 18:57:32 +0900 Message-ID: <20261204100713.089916383@linuxfoundation.org> X-Mailer: git-send-email 2.56.1 In-Reply-To: <20261204100702.047403948@linuxfoundation.org> References: <20261204100702.047403948@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.6-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: Changed the new btrfs_update_inode() error branch to goto out instead of upstream's return ret, so the path is freed without the upstream scope-based auto-free cleanup. ] 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 4ea1fb222ba90..fc44c65f1cbbb 100644 --- a/fs/btrfs/block-group.c +++ b/fs/btrfs/block-group.c @@ -1328,12 +1328,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 @@ -1347,7 +1349,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 eb6d335493b16..6179a9c94d7f7 100644 --- a/fs/btrfs/free-space-cache.c +++ b/fs/btrfs/free-space-cache.c @@ -269,6 +269,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)); + if (ret) { + btrfs_add_delayed_iput(BTRFS_I(inode)); + return ret; + } /* 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