Skip to contentSkip to conversation

btrfs: fix unnecessary flush on close when truncating zero-sized files

PATCH In review Dave Chen 2 participants 5 messages Mar 23 – Mar 24 Btrfs file system

lore
Share & tools
lore

Dave Chen

author
#0 lore raw

PATCH 1/1 btrfs: fix unnecessary flush on close when truncating zero-sized files

In btrfs_setsize(), when a file is truncated to size 0, the BTRFS_INODE_FLUSH_ON_CLOSE flag is unconditionally set to ensure pending writes get flushed on close. This flag was designed to protect the "truncate-then-rewrite" pattern, where an application truncates a file with existing data down to zero and writes new content, ensuring the new data reaches disk on close.

However, when a file already has a size of 0 (e.g., a newly created file opened with O_CREAT | O_TRUNC), oldsize and newsize are both 0. In this case, setting BTRFS_INODE_FLUSH_ON_CLOSE is unnecessary because no "good data" was truncated away. The subsequent filemap_flush() in btrfs_release_file() then triggers avoidable writeback that disrupts the normal delayed writeback batching, adding I/O overhead.

Fixes a41ad394a03b ("Btrfs: convert to the new truncate sequence")
---
 fs/btrfs/inode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/btrfs/inode.c +1 -1
index a6da98435ef7c..73902056c1c50 100644
@@ -5434,7 +5434,7 @@ static int btrfs_setsize(struct inode *inode, struct iattr *attr)
 		 * zero. Make sure any new writes to the file get on disk
 		 * on close.
 		 */
-		if (newsize == 0)
+		if (newsize == 0 && oldsize != 0)
 			set_bit(BTRFS_INODE_FLUSH_ON_CLOSE,
 				&BTRFS_I(inode)->runtime_flags);
 
Signature · 4 lines
2.43.0


Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.
#1 lore raw
Quoted 7 lines · Dave Chen

In btrfs_setsize(), when a file is truncated to size 0, the BTRFS_INODE_FLUSH_ON_CLOSE flag is unconditionally set to ensure pending writes get flushed on close. This flag was designed to protect the "truncate-then-rewrite" pattern, where an application truncates a file with existing data down to zero and writes new content, ensuring the new data reaches disk on close.

Quoted 6 lines

However, when a file already has a size of 0 (e.g., a newly created file opened with O_CREAT | O_TRUNC), oldsize and newsize are both 0. In this case, setting BTRFS_INODE_FLUSH_ON_CLOSE is unnecessary because no "good data" was truncated away. The subsequent filemap_flush() in btrfs_release_file() then triggers avoidable writeback that disrupts the normal delayed writeback batching, adding I/O overhead.

Does this have a measurable impact? Truncating 0 -> 0 without any intermediate writes could happen but I kind of doubt it's worth optimizing. I'm not against adding the patch but would like to know if it's fixing some problem for you. Thanks.

author
#2 lore raw
Quoted 4 lines · David Sterba

Does this have a measurable impact? Truncating 0 -> 0 without any intermediate writes could happen but I kind of doubt it's worth optimizing. I'm not against adding the patch but would like to know if it's fixing some problem for you. Thanks.

Yes, this comes from a real workload. We have a backup service that creates temporary files via mkstemp(), closes them, and later reopens them with O_TRUNC for writing. The O_TRUNC is defensive -- the file is always empty at that point, but the temp file creation and usage are in separate components, so removing it from userspace isn't straightforward.

This pattern repeats for a large number of files per backup job, and each close() triggers an unnecessary filemap_flush().

When oldsize is already 0, the flag provides no protection -- there is no data being truncated away, so the filemap_flush() on close serves no purpose. The fix simply makes the condition match what the comment already describes.

Thanks.

Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.

author
#3 lore raw
In reply to David Sterba
Quoted 4 lines · David Sterba

Does this have a measurable impact? Truncating 0 -> 0 without any intermediate writes could happen but I kind of doubt it's worth optimizing. I'm not against adding the patch but would like to know if it's fixing some problem for you. Thanks.

Yes, this comes from a real workload. We have a backup service that creates temporary files via mkstemp(), closes them, and later reopens them with O_TRUNC for writing. The O_TRUNC is defensive -- the file is always empty at that point, but the temp file creation and usage are in separate components, so removing it from userspace isn't straightforward.

This pattern repeats for a large number of files per backup job, and each close() triggers an unnecessary filemap_flush().

When oldsize is already 0, the flag provides no protection -- there is no data being truncated away, so the filemap_flush() on close serves no purpose. The fix simply makes the condition match what the comment already describes.

Thanks.

Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.

#4 lore raw
Quoted 12 lines · Dave Chen
Quoted 4 lines · David Sterba

Does this have a measurable impact? Truncating 0 -> 0 without any intermediate writes could happen but I kind of doubt it's worth optimizing. I'm not against adding the patch but would like to know if it's fixing some problem for you. Thanks.

Yes, this comes from a real workload. We have a backup service that creates temporary files via mkstemp(), closes them, and later reopens them with O_TRUNC for writing. The O_TRUNC is defensive -- the file is always empty at that point, but the temp file creation and usage are in separate components, so removing it from userspace isn't straightforward.

Quoted 7 lines

This pattern repeats for a large number of files per backup job, and each close() triggers an unnecessary filemap_flush().

When oldsize is already 0, the flag provides no protection -- there is no data being truncated away, so the filemap_flush() on close serves no purpose. The fix simply makes the condition match what the comment already describes.

Thanks, I'll add the workload description to changelog.