Skip to contentSkip to conversation

xfs: clear XFS_EOFBLOCKS_RELEASED on truncate down

PATCH In review Wei Gao 2 participants 3 messages Nov 27 – Nov 30 XFS filesystem

lore
Share & tools
lore

Wei Gao

author
#0 lore raw

PATCH 1/1 xfs: clear XFS_EOFBLOCKS_RELEASED on truncate down

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")
---
 fs/xfs/xfs_iops.c | 1 +
 1 file changed, 1 insertion(+)
fs/xfs/xfs_iops.c +1 -0
index e04b7d9d212d..b216b96d05a4 100644
@@ -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);
 	}
 
Signature · 1 line
2.55.0
#1 lore raw
Quoted 6 lines · Wei Gao

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:

Quoted 8 lines
    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.

Quoted 5 lines

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.

Quoted 4 lines

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.

Quoted 4 lines

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

Quoted 3 lines

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)

Quoted 2 lines

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.

Quoted 2 lines

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.

Quoted 3 lines

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.

Signature · 2 lines
Dave Chinner
[email protected]
author
#2 lore raw
Quoted 95 lines · Dave Chinner
Quoted 6 lines · Wei Gao

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:

Quoted 8 lines
    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.

Quoted 5 lines

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.

Quoted 4 lines

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.

Quoted 4 lines

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

Quoted 3 lines

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)

Quoted 2 lines

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.

Quoted 2 lines

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.

Quoted 3 lines

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.

Quoted 5 lines

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

Hi Dave,

Thanks a lot for the thorough explanation!

I have submitted a fix to LTP swapon04 instead to verify the delta of discarded blocks rather than asserting an exact remaining block count:
https://patchwork.kernel.org/project/ltp/patch/[email protected]/

Thanks again for your time.

Best regards,
Wei

Quoted 7 lines

Cheers,

Dave.

Signature · 2 lines
Dave Chinner
[email protected]