@@ -20,6 +20,7 @@
#include <linux/compat.h>
#include <linux/mount.h>
#include <linux/fs.h>
+#include <linux/falloc.h>
#include "internal.h"
#include <linux/uaccess.h>
@@ -1541,6 +1542,63 @@ COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd,
}
#endif
+
+static ssize_t splice_with_holes(struct file *file_in, loff_t *pos_in,
+ struct file *file_out, loff_t *pos_out, size_t len)
+{
+ ssize_t ret = 0, total = 0;
+ loff_t size, end;
+
+ while (total < len) {
+ end = vfs_llseek(file_in, *pos_in, SEEK_HOLE);
+ if (end == *pos_in)
+ goto hole;
+ size = end - *pos_in;
+splice:
+ ret = do_splice_direct(file_in, pos_in, file_out, pos_out,
+ size, 0);
+ if (ret < 0)
+ goto out;
+ total += ret;
+ if (total == len)
+ break;
+hole:
+ end = vfs_llseek(file_in, *pos_in, SEEK_DATA);
+ if (end < 0) {
+ ret = end;
+ goto out;
+ }
+ size = end - *pos_in;
+ /* For files already containing data, punch holes */
+ if (i_size_read(file_out->f_inode) > *pos_out) {
+ ret = vfs_fallocate(file_out,
+ FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
+ *pos_out, size);
+ if (ret < 0) {
+ /*
+ * The filesystem does not support punching
+ * holes. Perform splice on the entire range.
+ */
+ if (ret == -EOPNOTSUPP) {
+ size = len - total;
+ goto splice;
+ }
+ goto out;
+ }
+ }
+ if (ret < 0) {
+ ret = end;
+ goto out;
+ }
+ *pos_out += size;
+ *pos_in = end;
+ total += size;
+ }
+
+out:
+ return total ? total : ret;
+}
+
/*
* copy_file_range() differs from regular file read and write in that it
* specifically allows return partial success. When it does so is up to
@@ -1604,8 +1662,8 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in,
}
do_splice:
- ret = do_splice_direct(file_in, &pos_in, file_out, &pos_out,
- len > MAX_RW_COUNT ? MAX_RW_COUNT : len, 0);
+ ret = splice_with_holes(file_in, &pos_in, file_out, &pos_out,
+ len > MAX_RW_COUNT ? MAX_RW_COUNT : len);
done:
if (ret > 0) {