From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D61E51D520; Wed, 30 Sep 2026 17:59:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790791157; cv=none; b=BAM/6SLoOQwEbYmIBmcyFLDh1E8aLOxsfU+3JoUtpEL2bT4IzQd6TEFfdHZzKLRf2D9QasXsvw5+GRPisEiunSv84a5qoE1F4F5FPCHLyyVs5qDVx43qlUexRyXD2UW1NCh0MFAziFFZHVIv3tQ2ritLDysUY2I031Gv6dKl2Tk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790791157; c=relaxed/simple; bh=lOQD4bqJy5BG9eJ9YYnSlzKRnof+2vxPbtEf69vXmz0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=GsYom/it4CbxLw9bMWhCHNA0fIlk5lvjcIrMWO3jk4Z1JpUOFMyyDS3BeoDjoCNUqSRKp4OLidIqXSl8JSch6mHQ5BFuQ0vG+3Axk+ejMuCf/Pb7fC2QmjZ2EbrliByG6oqlorD5BqI5Wk9jQE4tGYE5e8I5gjbDIqxCsv+i1is= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=arpWRCWV; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="arpWRCWV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id DA5321F000FF; Wed, 30 Sep 2026 17:59:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1790791156; bh=bJfaeuFJDJUHIwsjlhpUeueWZyRcZwfrnq+K9TGbQbY=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=arpWRCWVg+SyLv/PqvU4+6SEnTHEoZvYASehzIFbc3124Pcpj+E6HsaDhLkFuLcPn +vtzGdQcg5ZM3GKthYNgZdB7Ih6aBuu7VxHnxjHsSVbpXm2N4m3rcxkh7A8fu7zJA3 LCriUIuhya8rgK6AWHyqA1G4bnVZBketEDfvCbek= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Filipe Manana , Dave Chen , David Sterba , Sasha Levin Subject: [PATCH 5.15 203/752] btrfs: only account delalloc bytes for regular file inodes in btrfs_getattr() Date: Wed, 30 Sep 2026 17:21:13 +0200 Message-ID: <20260930152402.617880718@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260930152358.131179731@linuxfoundation.org> References: <20260930152358.131179731@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: Dave Chen [ Upstream commit 9411aafdf352b8d72668732af5a37dcb27383e25 ] btrfs_getattr() unconditionally reads BTRFS_I(inode)->new_delalloc_bytes and adds it (sector-aligned) to stat->blocks for every inode type. However, new_delalloc_bytes lives in a union with last_dir_index_offset: union { u64 new_delalloc_bytes; /* files only */ u64 last_dir_index_offset; /* directories only */ }; For a directory inode this memory holds last_dir_index_offset, which is set during directory logging (e.g. flush_dir_items_batch()) to the offset of the last logged BTRFS_DIR_INDEX_KEY. That offset grows with the number of entries ever created in the directory (dir indexes are monotonic and never reused), so it can be arbitrarily large. As a result, after a directory has been logged (e.g. via an fsync that triggers directory logging), btrfs_getattr() reports inflated st_blocks for that directory. The inflation is purely in-core and disappears after the inode is evicted and reloaded (btrfs_alloc_inode() zeroes the union), e.g. after a remount. Reproducer (on a btrfs filesystem): D=/mnt/btrfs/d mkdir -p $D for i in $(seq 1 20000); do touch $D/f$i; done sync # commit, push dir index high touch $D/trigger # dirty the dir in a new transaction xfs_io -c fsync $D # log the directory -> sets last_dir_index_offset stat -c '%b' $D # st_blocks is now inflated (e.g. 40) # umount + mount -> st_blocks drops back to the correct value The evict path already knows this union is type-dependent and guards the corresponding WARN_ON with !S_ISDIR() in btrfs_destroy_inode(); only btrfs_getattr() was missing the equivalent check. Only read new_delalloc_bytes for regular files, which are the only inodes that ever set it. Reviewed-by: Filipe Manana Signed-off-by: Dave Chen Signed-off-by: Filipe Manana Signed-off-by: David Sterba Signed-off-by: Sasha Levin --- fs/btrfs/inode.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 1f8e8cd49c12a..6ea8ba9f7f423 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -9550,7 +9550,8 @@ static int btrfs_getattr(struct user_namespace *mnt_userns, stat->dev = BTRFS_I(inode)->root->anon_dev; spin_lock(&BTRFS_I(inode)->lock); - delalloc_bytes = BTRFS_I(inode)->new_delalloc_bytes; + delalloc_bytes = S_ISREG(inode->i_mode) ? + BTRFS_I(inode)->new_delalloc_bytes : 0; inode_bytes = inode_get_bytes(inode); spin_unlock(&BTRFS_I(inode)->lock); stat->blocks = (ALIGN(inode_bytes, blocksize) + -- 2.53.0