vfs: fix copy_file_range() averts filesystem freeze protection
authorAmir Goldstein <amir73il@gmail.com>
Thu, 17 Nov 2022 20:52:49 +0000 (22:52 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 19 Dec 2022 11:36:39 +0000 (12:36 +0100)
commit 10bc8e4af65946b727728d7479c028742321b60a upstream.

Commit 868f9f2f8e00 ("vfs: fix copy_file_range() regression in cross-fs
copies") removed fallback to generic_copy_file_range() for cross-fs
cases inside vfs_copy_file_range().

To preserve behavior of nfsd and ksmbd server-side-copy, the fallback to
generic_copy_file_range() was added in nfsd and ksmbd code, but that
call is missing sb_start_write(), fsnotify hooks and more.

Ideally, nfsd and ksmbd would pass a flag to vfs_copy_file_range() that
will take care of the fallback, but that code would be subtle and we got
vfs_copy_file_range() logic wrong too many times already.

Instead, add a flag to explicitly request vfs_copy_file_range() to
perform only generic_copy_file_range() and let nfsd and ksmbd use this
flag only in the fallback path.

This choise keeps the logic changes to minimum in the non-nfsd/ksmbd code
paths to reduce the risk of further regressions.

Fixes: 868f9f2f8e00 ("vfs: fix copy_file_range() regression in cross-fs copies")
Tested-by: Namjae Jeon <linkinjeon@kernel.org>
Tested-by: Luis Henriques <lhenriques@suse.de>
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
[backport comments for v5.15: - sb_write_started() is missing - assert was dropped ]
Signed-off-by: Amir Goldstein <amir73il@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/ksmbd/vfs.c
fs/nfsd/vfs.c
fs/read_write.c
include/linux/fs.h

index 513989b1c8cd74bf2f4aadb919760785e2ec6b8d..5d40a00fbce5042cb1161fdbda9b952e9a873836 100644 (file)
@@ -1788,9 +1788,9 @@ int ksmbd_vfs_copy_file_ranges(struct ksmbd_work *work,
                ret = vfs_copy_file_range(src_fp->filp, src_off,
                                          dst_fp->filp, dst_off, len, 0);
                if (ret == -EOPNOTSUPP || ret == -EXDEV)
-                       ret = generic_copy_file_range(src_fp->filp, src_off,
-                                                     dst_fp->filp, dst_off,
-                                                     len, 0);
+                       ret = vfs_copy_file_range(src_fp->filp, src_off,
+                                                 dst_fp->filp, dst_off, len,
+                                                 COPY_FILE_SPLICE);
                if (ret < 0)
                        return ret;
 
index abfbb6953e89a00724e62e6a0745e6eef3da843d..d4adc599737d85c43d21255227bf77864baaa478 100644 (file)
@@ -574,8 +574,8 @@ ssize_t nfsd_copy_file_range(struct file *src, u64 src_pos, struct file *dst,
        ret = vfs_copy_file_range(src, src_pos, dst, dst_pos, count, 0);
 
        if (ret == -EOPNOTSUPP || ret == -EXDEV)
-               ret = generic_copy_file_range(src, src_pos, dst, dst_pos,
-                                             count, 0);
+               ret = vfs_copy_file_range(src, src_pos, dst, dst_pos, count,
+                                         COPY_FILE_SPLICE);
        return ret;
 }
 
index 8d3ec975514d0fcffb54a27ff52d6556bdf9e98a..b4b15279b66b6cafedebc059c85f852440a9587d 100644 (file)
@@ -1418,7 +1418,9 @@ static int generic_copy_file_checks(struct file *file_in, loff_t pos_in,
         * and several different sets of file_operations, but they all end up
         * using the same ->copy_file_range() function pointer.
         */
-       if (file_out->f_op->copy_file_range) {
+       if (flags & COPY_FILE_SPLICE) {
+               /* cross sb splice is allowed */
+       } else if (file_out->f_op->copy_file_range) {
                if (file_in->f_op->copy_file_range !=
                    file_out->f_op->copy_file_range)
                        return -EXDEV;
@@ -1468,8 +1470,9 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
                            size_t len, unsigned int flags)
 {
        ssize_t ret;
+       bool splice = flags & COPY_FILE_SPLICE;
 
-       if (flags != 0)
+       if (flags & ~COPY_FILE_SPLICE)
                return -EINVAL;
 
        ret = generic_copy_file_checks(file_in, pos_in, file_out, pos_out, &len,
@@ -1495,14 +1498,14 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
         * same sb using clone, but for filesystems where both clone and copy
         * are supported (e.g. nfs,cifs), we only call the copy method.
         */
-       if (file_out->f_op->copy_file_range) {
+       if (!splice && file_out->f_op->copy_file_range) {
                ret = file_out->f_op->copy_file_range(file_in, pos_in,
                                                      file_out, pos_out,
                                                      len, flags);
                goto done;
        }
 
-       if (file_in->f_op->remap_file_range &&
+       if (!splice && file_in->f_op->remap_file_range &&
            file_inode(file_in)->i_sb == file_inode(file_out)->i_sb) {
                ret = file_in->f_op->remap_file_range(file_in, pos_in,
                                file_out, pos_out,
@@ -1522,6 +1525,8 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
         * consistent story about which filesystems support copy_file_range()
         * and which filesystems do not, that will allow userspace tools to
         * make consistent desicions w.r.t using copy_file_range().
+        *
+        * We also get here if caller (e.g. nfsd) requested COPY_FILE_SPLICE.
         */
        ret = generic_copy_file_range(file_in, pos_in, file_out, pos_out, len,
                                      flags);
@@ -1576,6 +1581,10 @@ SYSCALL_DEFINE6(copy_file_range, int, fd_in, loff_t __user *, off_in,
                pos_out = f_out.file->f_pos;
        }
 
+       ret = -EINVAL;
+       if (flags != 0)
+               goto out;
+
        ret = vfs_copy_file_range(f_in.file, pos_in, f_out.file, pos_out, len,
                                  flags);
        if (ret > 0) {
index 806ac72c722029e22f33732f67e084f9acadb73b..d55fdc02f82d8715b71c2ea50bab103d21dc5b19 100644 (file)
@@ -1990,6 +1990,14 @@ struct dir_context {
  */
 #define REMAP_FILE_ADVISORY            (REMAP_FILE_CAN_SHORTEN)
 
+/*
+ * These flags control the behavior of vfs_copy_file_range().
+ * They are not available to the user via syscall.
+ *
+ * COPY_FILE_SPLICE: call splice direct instead of fs clone/copy ops
+ */
+#define COPY_FILE_SPLICE               (1 << 0)
+
 struct iov_iter;
 
 struct file_operations {