All of lore.kernel.org
 help / color / mirror / Atom feed
From: Goldwyn Rodrigues <rgoldwyn@suse.de>
To: linux-fsdevel@vger.kernel.org
Cc: hch@lst.de, smfrench@gmail.com, linux-unionfs@vger.kernel.org,
	Anna.Schumaker@Netapp.com, darrick.wong@oracle.com,
	Goldwyn Rodrigues <rgoldwyn@suse.com>
Subject: [PATCH 2/3] copy_file_range: splice with holes
Date: Thu,  3 May 2018 10:26:34 -0500	[thread overview]
Message-ID: <20180503152635.14974-3-rgoldwyn@suse.de> (raw)
In-Reply-To: <20180503152635.14974-1-rgoldwyn@suse.de>

From: Goldwyn Rodrigues <rgoldwyn@suse.com>

copy_file_range calls do_splice_direct() if fs->clone_file_range
or fs->copy_file_range() is not available. However, do_splice_direct()
converts holes to zeros. Detect holes in the file_in range, and
create them in the corresponding file_out range.

If there is already data present at the offset in file_out, attempt
to punch a hole there. If the operation is not supported, fall
back to performing splice on the whole range.

Signed-off-by: Goldwyn Rodrigues <rgoldwyn@suse.com>
---
 fs/read_write.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 60 insertions(+), 2 deletions(-)

diff --git a/fs/read_write.c b/fs/read_write.c
index e71270033402..6866e2a27594 100644
--- a/fs/read_write.c
+++ b/fs/read_write.c
@@ -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) {
-- 
2.16.3


  parent reply	other threads:[~2018-05-03 15:26 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-03 15:26 [PATCH 0/3] Holey splice! copy_file_range() with holes Goldwyn Rodrigues
2018-05-03 15:26 ` [PATCH 1/3] Perform splice in copy_file_range if in/out SB are not same Goldwyn Rodrigues
2018-05-03 15:26 ` Goldwyn Rodrigues [this message]
2018-05-08  5:11   ` [lkp-robot] [copy_file_range] 9cb0698bda: stress-ng.copy-file.ops_per_sec -97.9% regression kernel test robot
2018-05-03 15:26 ` [PATCH 3/3] ovl: Use splice_with_holes in copy_up Goldwyn Rodrigues
2018-05-03 19:57   ` Amir Goldstein
2018-05-03 22:11     ` Dave Chinner
2018-05-04  1:29       ` Goldwyn Rodrigues
2018-05-05 23:16         ` Dave Chinner
2018-05-07 12:16           ` Christoph Hellwig
2018-05-07 23:16             ` Dave Chinner
2018-05-08  4:02               ` Christoph Hellwig
2018-05-08 10:06                 ` Dave Chinner
2018-05-08 16:11                   ` Goldwyn Rodrigues
2018-05-08 16:24                     ` Christoph Hellwig
2018-05-07 18:50           ` Andreas Dilger
2018-05-04  1:29     ` Goldwyn Rodrigues
2018-05-04  1:31     ` Goldwyn Rodrigues
2018-05-04  6:18       ` Amir Goldstein

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180503152635.14974-3-rgoldwyn@suse.de \
    --to=rgoldwyn@suse.de \
    --cc=Anna.Schumaker@Netapp.com \
    --cc=darrick.wong@oracle.com \
    --cc=hch@lst.de \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-unionfs@vger.kernel.org \
    --cc=rgoldwyn@suse.com \
    --cc=smfrench@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.