mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Wei Gao <wegao@suse.com>
To: Carlos Maiolino <cem@kernel.org>
Cc: linux-xfs@vger.kernel.org, "Darrick J . Wong" <djwong@kernel.org>,
	Chandan Babu R <chandanbabu@kernel.org>,
	linux-kernel@vger.kernel.org, Wei Gao <wegao@suse.com>
Subject: [PATCH] xfs: clear XFS_EOFBLOCKS_RELEASED on truncate down
Date: Fri, 27 Nov 2026 10:09:29 +0000	[thread overview]
Message-ID: <20261127100929.32124-1-wegao@suse.com> (raw)

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()).
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.

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.

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).
 2. Re-open with O_TRUNC (sets XFS_ITRUNCATED = 1) and write 16 MiB
    sequentially (allocates 32 MiB / 65408 blocks due to speculative
    preallocation).
 3. Call fsync(fd) before close(): xfs_file_fsync() clears
    XFS_ITRUNCATED.
 4. Call close(fd): xfs_file_release() sees XFS_ITRUNCATED is 0,
    leaves XFS_EOFBLOCKS_RELEASED set, and skips xfs_free_eofblocks().
 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.

Fix this by clearing XFS_EOFBLOCKS_RELEASED directly in
xfs_vn_setattr_size() when the file is truncated down, ensuring that
subsequent close() treats the file as truncated regardless of whether
fsync() or writeback cleared XFS_ITRUNCATED in the meantime.

Fixes: f1204d96450f ("xfs: only free posteof blocks on first close")
Signed-off-by: Wei Gao <wegao@suse.com>
---
 fs/xfs/xfs_iops.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/fs/xfs/xfs_iops.c b/fs/xfs/xfs_iops.c
index e04b7d9d212d..b216b96d05a4 100644
--- a/fs/xfs/xfs_iops.c
+++ b/fs/xfs/xfs_iops.c
@@ -1124,6 +1124,7 @@ xfs_vn_setattr_size(
 		xfs_iflags_set(ip, XFS_ITRUNCATED);
 
 		/* A truncate down always removes post-EOF blocks. */
+		xfs_iflags_clear(ip, XFS_EOFBLOCKS_RELEASED);
 		xfs_inode_clear_eofblocks_tag(ip);
 	}
 
-- 
2.55.0


                 reply	other threads:[~2026-11-27 10:09 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127100929.32124-1-wegao@suse.com \
    --to=wegao@suse.com \
    --cc=cem@kernel.org \
    --cc=chandanbabu@kernel.org \
    --cc=djwong@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-xfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®