[PATCH] fuse: Move same-superblock check to fuse_copy_file_range

Chunsheng Luo posted 1 patch 1 month, 4 weeks ago
fs/fuse/file.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
[PATCH] fuse: Move same-superblock check to fuse_copy_file_range
Posted by Chunsheng Luo 1 month, 4 weeks ago
The copy_file_range COPY_FILE_SPLICE capability allows filesystems to
handle cross-superblock copy. However, in the current fuse implementation,
__fuse_copy_file_range accesses src_file->private_data under the assumption
that it points to a fuse_file structure. When the source file belongs to a
non-FUSE filesystem, it will leads to kernel panics.

To resolve this, move the same-superblock check from __fuse_copy_file_range
to fuse_copy_file_range to ensure both files belong to the same fuse
superblock before accessing private_data.

Signed-off-by: Chunsheng Luo <luochunsheng@ustc.edu>
---
 fs/fuse/file.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/fs/fuse/file.c b/fs/fuse/file.c
index 95275a1e2f54..a29f1b84f11b 100644
--- a/fs/fuse/file.c
+++ b/fs/fuse/file.c
@@ -2984,9 +2984,6 @@ static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
 	if (fc->no_copy_file_range)
 		return -EOPNOTSUPP;
 
-	if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
-		return -EXDEV;
-
 	inode_lock(inode_in);
 	err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
 	inode_unlock(inode_in);
@@ -3066,9 +3063,12 @@ static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
 {
 	ssize_t ret;
 
+	if (file_inode(src_file)->i_sb != file_inode(dst_file)->i_sb)
+		return splice_copy_file_range(src_file, src_off, dst_file,
+					     dst_off, len);
+
 	ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
 				     len, flags);
-
 	if (ret == -EOPNOTSUPP || ret == -EXDEV)
 		ret = splice_copy_file_range(src_file, src_off, dst_file,
 					     dst_off, len);
-- 
2.43.0
Re: [PATCH] fuse: Move same-superblock check to fuse_copy_file_range
Posted by Luis Henriques 1 month, 4 weeks ago
On Wed, Aug 06 2025, Chunsheng Luo wrote:

> The copy_file_range COPY_FILE_SPLICE capability allows filesystems to
> handle cross-superblock copy. However, in the current fuse implementation,
> __fuse_copy_file_range accesses src_file->private_data under the assumption
> that it points to a fuse_file structure. When the source file belongs to a
> non-FUSE filesystem, it will leads to kernel panics.

I wonder if you have actually seen this kernel panic happening.  It seems
like the code you're moving into fuse_copy_file_range() shouldn't be
needed as the same check is already done in generic_copy_file_checks()
(which is called from vfs_copy_file_range()).

Either way, I think your change to fuse_copy_file_range() could be
simplified with something like:

	ssize_t ret = -EXDEV;

	if (file_inode(src_file)->i_sb == file_inode(dst_file)->i_sb)
		ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
					     len, flags);

	if (ret == -EOPNOTSUPP || ret == -EXDEV)
		ret = splice_copy_file_range(src_file, src_off, dst_file,
					     dst_off, len);

But again, my understanding is that this should never happen in practice
and that the superblock check could even be removed from
__fuse_copy_file_range().

Cheers,
-- 
Luís

>
> To resolve this, move the same-superblock check from __fuse_copy_file_range
> to fuse_copy_file_range to ensure both files belong to the same fuse
> superblock before accessing private_data.
>
> Signed-off-by: Chunsheng Luo <luochunsheng@ustc.edu>
> ---
>  fs/fuse/file.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/fs/fuse/file.c b/fs/fuse/file.c
> index 95275a1e2f54..a29f1b84f11b 100644
> --- a/fs/fuse/file.c
> +++ b/fs/fuse/file.c
> @@ -2984,9 +2984,6 @@ static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
>  	if (fc->no_copy_file_range)
>  		return -EOPNOTSUPP;
>  
> -	if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
> -		return -EXDEV;
> -
>  	inode_lock(inode_in);
>  	err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
>  	inode_unlock(inode_in);
> @@ -3066,9 +3063,12 @@ static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
>  {
>  	ssize_t ret;
>  
> +	if (file_inode(src_file)->i_sb != file_inode(dst_file)->i_sb)
> +		return splice_copy_file_range(src_file, src_off, dst_file,
> +					     dst_off, len);
> +
>  	ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
>  				     len, flags);
> -
>  	if (ret == -EOPNOTSUPP || ret == -EXDEV)
>  		ret = splice_copy_file_range(src_file, src_off, dst_file,
>  					     dst_off, len);
> -- 
> 2.43.0
>
Re: [PATCH] fuse: Move same-superblock check to fuse_copy_file_range
Posted by Bernd Schubert 1 month, 4 weeks ago

On 8/6/25 15:52, Chunsheng Luo wrote:
> The copy_file_range COPY_FILE_SPLICE capability allows filesystems to
> handle cross-superblock copy. However, in the current fuse implementation,
> __fuse_copy_file_range accesses src_file->private_data under the assumption
> that it points to a fuse_file structure. When the source file belongs to a
> non-FUSE filesystem, it will leads to kernel panics.
> 
> To resolve this, move the same-superblock check from __fuse_copy_file_range
> to fuse_copy_file_range to ensure both files belong to the same fuse
> superblock before accessing private_data.
> 
> Signed-off-by: Chunsheng Luo <luochunsheng@ustc.edu>
> ---
>  fs/fuse/file.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/fuse/file.c b/fs/fuse/file.c
> index 95275a1e2f54..a29f1b84f11b 100644
> --- a/fs/fuse/file.c
> +++ b/fs/fuse/file.c
> @@ -2984,9 +2984,6 @@ static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
>  	if (fc->no_copy_file_range)
>  		return -EOPNOTSUPP;
>  
> -	if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
> -		return -EXDEV;
> -
>  	inode_lock(inode_in);
>  	err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
>  	inode_unlock(inode_in);
> @@ -3066,9 +3063,12 @@ static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
>  {
>  	ssize_t ret;
>  
> +	if (file_inode(src_file)->i_sb != file_inode(dst_file)->i_sb)
> +		return splice_copy_file_range(src_file, src_off, dst_file,
> +					     dst_off, len);
> +
>  	ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
>  				     len, flags);
> -
>  	if (ret == -EOPNOTSUPP || ret == -EXDEV)
>  		ret = splice_copy_file_range(src_file, src_off, dst_file,
>  					     dst_off, len);

I guess you can remove the check EXDEV here?