Skip to contentSkip to conversation

fuse: zero the partial EOF page when extending a file

PATCH Superseded Jimmy Zuber 1 participant 3 messages Aug 20

Subsystems (2)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover fuse: zero the partial EOF page when extending a file

Extending a fuse file past a non-page-aligned EOF does not zero the tail of the old last page. When that page is cached and has been mmap-dirtied beyond the old EOF, the now in-bounds tail is served to later reads as stale data rather than zeros, which violates POSIX file-extension semantics.

Some file systems get this zeroing automatically at writeback time (block_write_full_folio() / iomap_writeback_handle_eof() zero the tail of the folio straddling i_size). A non-writeback caching fuse file system uses neither path, so it has to zero the tail itself from the size-extending paths, like XFS (xfs_file_write_zero_eof()) and ext4 (ext4_block_zero_eof()) do. pagecache_isize_extended() cannot be reused: it is a no-op when i_blocksize() >= PAGE_SIZE, and would increase the work in that function for use cases that don't need it, if changed to support this situation.

Patch 1 adds fuse_zero_partial_eof_folio() and calls it up front from the three paths that extend a file: buffered write, setattr, and fallocate. Patch 2 adds a self-contained raw /dev/fuse selftest covering each path.

Tested by booting the patched kernel under User-Mode Linux and running the new selftest, and reproduced against libfuse's passthrough_ll example daemon.

Changes since v1 [1]:
- Miklos and Jan Kara discussed whether there was a better pattern with less duplication between file systems. Jan confirmed that other file systems do this zeroing themselves from their write/setattr paths (e.g. XFS xfs_file_write_zero_eof(), ext4 ext4_block_zero_eof()), because the block / iomap writeback that would otherwise zero the straddling folio does not run on this path. File systems currently implement this separately. It is beyond me to pursue unifying it. Restructure to match the pattern in other file systems: zero [old EOF, write start) up front from fuse_perform_write() rather than after the fact from fuse_write_update_attr(), keyed on the write starting beyond EOF.
- This keeps the zeroed range disjoint from the written data, so a write that lands inside the old EOF folio is preserved by construction; the v1 "pos - written" arithmetic and the fallocate double-call are gone.
- fuse_write_update_attr() goes back to being a pure attr update; the setattr/truncate call in fuse_do_setattr() is unchanged.
- The selftest is unchanged from v1.

[1] https://lore.kernel.org/all/[email protected]/

Jimmy Zuber (2):
fuse: zero the partial EOF page when extending a file selftests/fuse: test post-EOF page zeroing when a file is extended

 fs/fuse/dir.c                                 |   3 +
 fs/fuse/file.c                                |  56 +++
 fs/fuse/fuse_i.h                              |   1 +
 .../selftests/filesystems/fuse/.gitignore     |   1 +
 .../selftests/filesystems/fuse/Makefile       |   3 +
 .../filesystems/fuse/write_extend_eof_test.c  | 368 ++++++++++++++++++
 6 files changed, 432 insertions(+)
 create mode 100644 tools/testing/selftests/filesystems/fuse/write_extend_eof_test.c

base-commit: 7d87a5a284bb34edb3f4e7e312ef403b3385a7b7

Signature · 1 line
2.50.1
#1 lore raw

PATCH v2 1/2 fuse: zero the partial EOF page when extending a file

Extending a fuse file past a non-page-aligned EOF does not zero the tail of the old last page. When that page is cached and has been mmap-dirtied beyond the old EOF, the now in-bounds tail is served to later reads as stale data rather than zeros, which violates POSIX file-extension semantics.

Some file systems get this zeroing automatically at writeback time (block_write_full_folio() / iomap_writeback_handle_eof() zero the tail of the folio straddling i_size). A non-writeback caching fuse file system uses neither path, so it has to zero the tail itself from the size-extending paths, like XFS (xfs_file_write_zero_eof()) and ext4 (ext4_block_zero_eof()) do. pagecache_isize_extended() cannot be reused: it is a no-op when i_blocksize() >= PAGE_SIZE, and would increase the work in that function for use cases that don't need it, if changed to support this situation.

Add fuse_zero_partial_eof_folio(), which zeroes the tail of the old EOF folio, and call it up front from the three paths that extend a file, mirroring xfs_file_write_zero_eof() and ext4_block_zero_eof():

- a buffered write whose position is past the old EOF (fuse_perform_write(),

    before the page cache is filled);

- a size-extending setattr/truncate (fuse_do_setattr());
- a size-extending fallocate (fuse_file_fallocate()).

Zeroing [old EOF, write start) before the write, rather than after it, keeps the zeroed range disjoint from the written data, so a write that lands inside the old EOF folio is preserved without special-casing.

writeback_cache connections are unaffected, as their writes go through iomap_file_buffered_write(), which zeroes post-EOF folios. The bug is observable on a non-writeback_cache server that returns FOPEN_KEEP_CACHE on writable files (without FOPEN_DIRECT_IO), and is caught by the new write_extend_eof fuse selftest.

---
 fs/fuse/dir.c    |  3 +++
 fs/fuse/file.c   | 56 ++++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/fuse_i.h |  1 +
 3 files changed, 60 insertions(+)
fs/fuse/dir.c +3 -0
index 795e92037ce7..f6614ccef186 100644
@@ -2282,6 +2282,9 @@ int fuse_do_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
 	 */
 	if ((is_truncate || !is_wb) &&
 	    S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
+		if (outarg.attr.size > oldsize)
+			fuse_zero_partial_eof_folio(inode, oldsize,
+						    outarg.attr.size);
 		truncate_pagecache(inode, outarg.attr.size);
 		invalidate_inode_pages2(mapping);
 	}
fs/fuse/file.c +56 -0

83 lines not on this page · raw message

fs/fuse/fuse_i.h +1 -0
index 85f738c53122..ee3b91b56fef 100644
@@ -1183,6 +1183,7 @@ long fuse_ioctl_common(struct file *file, unsigned int cmd,
 __poll_t fuse_file_poll(struct file *file, poll_table *wait);
 
 bool fuse_write_update_attr(struct inode *inode, loff_t pos, ssize_t written);
+void fuse_zero_partial_eof_folio(struct inode *inode, loff_t from, loff_t to);
 
 int fuse_flush_times(struct inode *inode, struct fuse_file *ff);
 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc);
Signature · 1 line
2.50.1
#2 lore raw

PATCH v2 2/2 selftests/fuse: test post-EOF page zeroing when a file is extended

In reply to Jimmy Zuber

Add a regression test for the bug where extending a file left the tail of the old partial EOF page exposing stale mmap-dirtied data instead of zeros.

The test is a self-contained raw /dev/fuse server (no libfuse dependency) that runs without writeback_cache and returns FOPEN_KEEP_CACHE, the configuration in which the bug is visible. Its backing data is always zero in the hole, so any non-zero byte a read sees is stale page-cache data. All offsets are relative to the runtime page size. Four cases:

- write_extend: pollute the post-EOF tail, extend past it by writing into

    a later page, and verify the tail reads back as zero;

- ftruncate_extend: same, but extend via ftruncate();
- fallocate_extend: same, but extend via fallocate() at the old EOF;
- extend_into_eof_page_preserves_data: an extending write landing inside the old EOF page must not be clobbered by the zeroing.

Each case fails without the fix and passes with it.

---
 .../selftests/filesystems/fuse/.gitignore     |   1 +
 .../selftests/filesystems/fuse/Makefile       |   3 +
 .../filesystems/fuse/write_extend_eof_test.c  | 368 ++++++++++++++++++
 3 files changed, 372 insertions(+)
 create mode 100644 tools/testing/selftests/filesystems/fuse/write_extend_eof_test.c
tools/testing/selftests/filesystems/fuse/.gitignore +1 -0
index 3e72e742d08e..fb51603fe419 100644
@@ -1,3 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 fuse_mnt
 fusectl_test
+write_extend_eof_test
tools/testing/selftests/filesystems/fuse/Makefile +3 -0
index 612aad69a93a..0c2c613af0ac 100644
@@ -3,10 +3,13 @@
 CFLAGS += -Wall -O2 -g $(KHDR_INCLUDES)
 
 TEST_GEN_PROGS := fusectl_test
+TEST_GEN_PROGS += write_extend_eof_test
 TEST_GEN_FILES := fuse_mnt
 
 include ../../lib.mk
 
+$(OUTPUT)/write_extend_eof_test: LDLIBS += -lpthread
+
 VAR_CFLAGS := $(shell pkg-config fuse --cflags 2>/dev/null)
 ifeq ($(VAR_CFLAGS),)
 VAR_CFLAGS := -D_FILE_OFFSET_BITS=64 -I/usr/include/fuse
tools/testing/selftests/filesystems/fuse/write_extend_eof_test.c new file +368 -0

368 lines not on this page · raw message

Signature · 1 line
2.50.1