fuse: fall back to copy_splice_read() when i_size is zero
RFC PATCH 1/1 fuse: fall back to copy_splice_read() when i_size is zero
After upgrading the kernel from 6.1.y to 6.18.y on production servers, some containers failed to read lxcfs data. The root cause is that our lxcfs is still at the old v3.0.4, which does not include commit 25982f5 ("Set the file size to 4k") [0].
Between 6.1.y and 6.18.y, the FUSE splice_read path was converted from generic_file_splice_read() to filemap_splice_read() by commit 2cb1e08985e3 ("splice: Use filemap_splice_read() instead of generic_file_splice_read()"). Unlike generic_file_splice_read(), filemap_splice_read() strictly checks i_size and refuses to read from zero-size files, which breaks virtual FUSE filesystems like the old lxcfs.
While the proper long-term fix is to upgrade lxcfs, the kernel should be resilient to this common FUSE daemon behavior. Therefore, when i_size is zero, fall back to copy_splice_read() instead of filemap_splice_read(). copy_splice_read() calls the file's read_iter operation, which sends a FUSE_READ request to the daemon and lets it generate the content dynamically. This is safe because for genuinely empty files the daemon simply returns 0 bytes.
--- fs/fuse/file.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
fs/fuse/file.c +7 -1
@@ -1893,8 +1893,14 @@ static ssize_t fuse_splice_read(struct file *in, loff_t *ppos,/* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */ if (fuse_file_passthrough (ff) && !(ff->open_flags &FOPEN_DIRECT_IO ))return fuse_passthrough_splice_read (in, ppos, pipe, len, flags); -else +else if (i_size_read (in->f_mapping->host)) +/* filemap_splice_read() requires non-zero i_size; for virtual +* FUSE daemons that report st_size = 0, fall back to +* copy_splice_read() which reaches the daemon via read_iter. +*/ return filemap_splice_read (in, ppos, pipe, len, flags); +else +return copy_splice_read (in, ppos, pipe, len, flags); } static ssize_t fuse_splice_write(struct pipe_inode_info *pipe, struct file *out,
Signature · 1 line
2.52.0
Miklos Szeredi
maintainerQuoted 7 lines · Yafang Shao
While the proper long-term fix is to upgrade lxcfs, the kernel should be resilient to this common FUSE daemon behavior. Therefore, when
i_sizeis zero, fall back tocopy_splice_read()instead offilemap_splice_read().copy_splice_read()calls the file'sread_iteroperation, which sends aFUSE_READrequest to the daemon and lets it generate the content dynamically. This is safe because for genuinely empty files the daemon simply returns 0 bytes.
Thanks for the report and patch. Naturally the kernel needs to be fixed in this case, the no-regression rules makes that mandatory.
Can you please try the attached patch? This is actually fixing
behavior for FOPEN_DIRECT_IO, so if it fixes the regression that would be wonderful.
Thanks,
Miklos
From 2010ed732435f7d65b7007595c263ada9c3bc8e4 Mon Sep 17 00:00:00 2001
From: Miklos Szeredi <[email protected]>
Date: Wed, 26 Aug 2026 16:11:59 +0200
Subject: [PATCH] fuse: fall back to copy_splice_read() on FOPEN_DIRECT_IO
Yafang Shao reports:
"After upgrading the kernel from 6.1.y to 6.18.y on production servers, some containers failed to read lxcfs data. The root cause is that our lxcfs is still at the old v3.0.4, which does not include commit 25982f5 ("Set the file size to 4k") [0].
Between 6.1.y and 6.18.y, the FUSE splice_read path was converted from generic_file_splice_read() to filemap_splice_read() by commit 2cb1e08985e3 ("splice: Use filemap_splice_read() instead of generic_file_splice_read()"). Unlike generic_file_splice_read(), filemap_splice_read() strictly checks i_size and refuses to read from zero-size files, which breaks virtual FUSE filesystems like the old lxcfs."
Fix by using copy_splice_read() for the FOPEN_DIRECT_IO case, just like the VFS does for DAX and O_DIRECT files.
--- fs/fuse/file.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
fs/fuse/file.c +4 -1
@@ -1874,7 +1874,10 @@ static ssize_t fuse_splice_read(struct file *in, loff_t *ppos,struct fuse_file *ff = in->private_data;/* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */ -if (fuse_file_passthrough (ff) && !(ff->open_flags &FOPEN_DIRECT_IO )) + +if (ff->open_flags &FOPEN_DIRECT_IO ) +return copy_splice_read (in, ppos, pipe, len, flags); +else if (fuse_file_passthrough (ff))return fuse_passthrough_splice_read (in, ppos, pipe, len, flags);else return filemap_splice_read (in, ppos, pipe, len, flags);
Signature · 1 line
2.54.0
Quoted 11 lines · Miklos Szeredi
Quoted 7 lines · Yafang Shao
While the proper long-term fix is to upgrade lxcfs, the kernel should be resilient to this common FUSE daemon behavior. Therefore, when
i_sizeis zero, fall back tocopy_splice_read()instead offilemap_splice_read().copy_splice_read()calls the file'sread_iteroperation, which sends aFUSE_READrequest to the daemon and lets it generate the content dynamically. This is safe because for genuinely empty files the daemon simply returns 0 bytes.
Quoted 6 lines
Thanks for the report and patch. Naturally the kernel needs to be fixed in this case, the no-regression rules makes that mandatory.
Can you please try the attached patch? This is actually fixing
behavior forFOPEN_DIRECT_IO, so if it fixes the regression that would be wonderful.
Thanks for the fix. I've verified that it resolves the regression.
For your patch, feel free to add:
BTW, below is a simpler reproducer for this regression.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
// lxcfs without commit 25982f5 ("Set the file size to 4k") #define LXCFS_PROC_PATH "/lxcfs/proc/meminfo"
#define MAX_FILE_SIZE 16777216 /* 16MB */
int main(void)
{
struct stat file_stat;
ssize_t sent_bytes;
off_t offset = 0;
int in_fd;
in_fd = openat(AT_FDCWD, LXCFS_PROC_PATH, O_RDONLY);
if (in_fd == -1) {
fprintf(stderr, "Error: Cannot open '%s': %s\n",
LXCFS_PROC_PATH, strerror(errno));
return EXIT_FAILURE;
}
if (fstat(in_fd, &file_stat) == -1)
fprintf(stderr, "Warning: Cannot stat file: %s\n", strerror(errno));
sent_bytes = sendfile(STDOUT_FILENO, in_fd, &offset, MAX_FILE_SIZE);
if (sent_bytes == -1) {
fprintf(stderr, "Error: sendfile failed: %s\n", strerror(errno));
close(in_fd);
return EXIT_FAILURE;
}
if (close(in_fd) == -1)
fprintf(stderr, "Warning: Failed to close file: %s\n", strerror(errno));
return EXIT_SUCCESS;
}Signature · 2 lines
Regards Yafang