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=1795903967; b=JMf0XB3D/jYSyjJl167euPLn8dvEU4A6hqn0tSBC7rECFc6IuJVoM3JnlhVKL94rmGLW qT7uhiG8YA8wR8NLFHkz4u2uV+9AXqQFki4X4Z94lUpA9uk3Os/9Mz5JWvyBPJLmZCK1p us7e25GvnJbqysjkuIedXeM0HRcIkQSVaDgpnVWCd1ar9sq4r3k1a5bwHmJ4+ZKIJDXrF DR7813C2mOgljwW0RqMDYb0B0Z8TC43OFLnESZGMlBmjOO/eDw6MywlfP/UHXruqWDVrT 0vYjSlspY5uCAskj+YIImqYvP5D5j31p1HPDnoRH6PhzrveW4mddC5uk3scXTD5UCpg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795903967; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-ID: References:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=KD0JsXD4UBYx7X13S6Od5kTou3jBKiQTLy8n0NHWBX8=; b=M6MLL+ji8998ehS/KHWgQltC1GZJIwlRCtl7BYazkO7Cl0rKBkQAkHJlf4jvz8PnDdgL Cw0q/NvuxC26pJBazthNCNNW4dhjGX/qBeMnbuqE5MVzBTbhuH2ksCyr5TD2D3jL4nyU7 KSDQi5YT6n9bvhqif1k5hJYsSTVUojx1Xkq1WITVbTYgBBrWPXJkvB1HHYRkVnQGqEWNk C80p42nK0A9RskpYMdLDpYVCAw9bPlBKaf2IwZpFu7Xj6KIe+IWNNed4BsmbKX4/k+bI5 vSu1DFSYzcux5wSwUmRI9sCX9byE7OJyBWQ6qfxt8ouAuxekxxsPdVLyBW3sEAz0C4w== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=PZHusTx2; 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 2DB1F606A31C; Sat, 28 Nov 2026 22:12:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=PZHusTx2 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 899A81F00893; Sat, 28 Nov 2026 22:12:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1795903967; bh=KD0JsXD4UBYx7X13S6Od5kTou3jBKiQTLy8n0NHWBX8=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=PZHusTx2KXHdTXAFXYaBspJkAKs7mDbqcEwZk590oyVXUGEIPK61XPLAKWFLR/+DY nPbx4KLYkIeXrmcBGqBSn9gcgJhDS/CeSue6dMSXUQdhx5YTyzXR15uZBcvsIuPXLJ 8MH+XjDPQ0Gheaskk0DPDq1gOLcttsaRGzIiy3Egy9B58OmphXx+59s8XigxKp/XS5 G0kCTv8UUtP9nipasDIO/6gJ4nqE2YeoHV+9JxvG3MFsnRop0Sy1NEeqTjOsijJcp8 ppZDQjaevmrX9Cf5OT74dKDBEYc02qWdsP0I/z8qB+O+EaRwzLuM4worIIuP1+Rz5z wBMoV4/uJkbZA== Date: Sun, 29 Nov 2026 09:12:38 +1100 From: Dave Chinner To: Wei Gao Cc: Carlos Maiolino , linux-xfs@vger.kernel.org, "Darrick J . Wong" , Chandan Babu R , linux-kernel@vger.kernel.org Subject: Re: [PATCH] xfs: clear XFS_EOFBLOCKS_RELEASED on truncate down Message-ID: References: <20261127100929.32124-1-wegao@suse.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20261127100929.32124-1-wegao@suse.com> On Fri, Nov 27, 2026 at 10:09:29AM +0000, Wei Gao wrote: > Commit f1204d96450f ("xfs: only free posteof blocks on first close") > introduced XFS_EOFBLOCKS_RELEASED to avoid freeing speculative > post-EOF preallocations across repeated open/write/close cycles. > To reset this flag when a file is truncated down, xfs_file_release() > relies on the XFS_ITRUNCATED flag: > > if (xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED)) { > xfs_iflags_clear(ip, XFS_EOFBLOCKS_RELEASED); > ... > } > > However, XFS_ITRUNCATED is an ephemeral flush-on-close flag that is > cleared as soon as dirty pages are written back to disk by background > writeback (xfs_vm_writepages()) or explicit fsync (xfs_file_fsync()). Yes, ITRUNCATED is a flush-on-close mitigation for an unsafe application overwrite anti-pattern, and fsync() before close negates the need for mitigation (i.e. application is attempting to write safely) and so removes ITRUNCATED as flush-on-close is no longer needed to mitigate broken application file overwrite behaviour. If we trigger flush on close, the implication is that the application is not going to come back to this file and write it again. Hence in that case, it is better to also trigger post-eof block removal whilst this inode is hot in the CPU cache, rather than having to do it at inode reclaim time or via background GC on a cold CPU cache. > If writeback occurs while the file is being written, or if fsync() > is called before close(), XFS_ITRUNCATED is cleared. Consequently, > when close() is subsequently called, xfs_iflags_test_and_clear() > evaluates to false, XFS_EOFBLOCKS_RELEASED is never cleared, and > xfs_free_eofblocks() is skipped. Which is perfectly fine, and behaviour as intended. There has been an open/write/close behaviour observed, fsync has negated the need for flush-on-close but it implies nothing about the already observed open-write-close IO pattern that has triggered the XFS_EOFBLOCKS_RELEASED file fragmentation mitigation. > If an open reference (such as a loop device) pins the inode in > memory, the post-EOF blocks remain allocated permanently, causing > tools and tests (such as LTP swapon04) to observe leaked blocks > beyond EOF. This behaviour does not 'leak blocks beyond EOF'. XFS will reclaim then periodically if the inode goes idle (background block GC runs every 5 minutes), low free space triggers block GC, or when the inode is evicted from the cache. There is no specification that requires 'st_blocks must exactly match the amount of data the application wrote'. XFS has had this "post eof block allocation observable by stat" behaviour for a long time (i.e. 25+ years). Further, think about what reflink copies and deduplication imply for st_blocks. Do you account shared blocks as allocated to that file or not? That's not defined in any specification, either... Hence any test that assumes all filesystems will have the exact same block count reported for a specific amount of data written by an application is simply a broken test. > The issue can be easily and deterministically reproduced on XFS using > fsync(): > 1. Create a file, write data, and close it (sets in-core > XFS_EOFBLOCKS_RELEASED = 1). Yup, it removed post-eof blocks on close(). > 2. Re-open with O_TRUNC (sets XFS_ITRUNCATED = 1) and write 16 MiB > sequentially (allocates 32 MiB / 65408 blocks due to speculative > preallocation). Yup, we now have an repeated open/write/close pattern, and we have a need for flush-on-close because this is an unsafe overwrite pattern (i.e. ITRUNCATED is set) > 3. Call fsync(fd) before close(): xfs_file_fsync() clears > XFS_ITRUNCATED. Yup, no need for flush-on-close any more because we don't have to mitigate an unsafe application write pattern. > 4. Call close(fd): xfs_file_release() sees XFS_ITRUNCATED is 0, > leaves XFS_EOFBLOCKS_RELEASED set, and skips xfs_free_eofblocks(). Yup, as intended - there has been an repeated open/write/close pattern observed. > 5. stat() reports st_blocks = 65408 (32 MiB) instead of 32768 (16 MiB). > Any subsequent swapon discard punches only the 16 MiB swap area, > leaving 32648 blocks and failing discard assertions. Yup, st_blocks is is correct, we've got 32MB of blocks allocated at this point for the file, and when the inode is reclaimed or blockgc runs, it will be cleaned up back to match EOF. Everything seems to be working as intended... Cheers, Dave. -- Dave Chinner dgc@kernel.org