From: Gao Xiang <hsiangkao@linux.alibaba.com>
To: linux-erofs@lists.ozlabs.org, Chao Yu <chao@kernel.org>,
Liu Bo <bo.liu@linux.alibaba.com>
Cc: Joseph Qi <joseph.qi@linux.alibaba.com>,
Eryu Guan <eguan@linux.alibaba.com>,
Liu Jiang <gerry@linux.alibaba.com>,
Peng Tao <tao.peng@linux.alibaba.com>
Subject: [PATCH 3/4] erofs-utils: introduce copy_file_range
Date: Wed, 18 Aug 2021 15:03:15 +0800 [thread overview]
Message-ID: <20210818070316.1970-3-hsiangkao@linux.alibaba.com> (raw)
In-Reply-To: <20210818070316.1970-1-hsiangkao@linux.alibaba.com>
Add copy_file_range support. Emulate it instead if libc
doesn't support it or have no emulation.
Signed-off-by: Gao Xiang <hsiangkao@linux.alibaba.com>
---
configure.ac | 1 +
include/erofs/io.h | 5 +++
lib/io.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 101 insertions(+)
diff --git a/configure.ac b/configure.ac
index 7217cf531265..af1d06c15996 100644
--- a/configure.ac
+++ b/configure.ac
@@ -158,6 +158,7 @@ AC_CHECK_DECL(lseek64,[AC_DEFINE(HAVE_LSEEK64_PROTOTYPE, 1,
# Checks for library functions.
AC_CHECK_FUNCS(m4_flatten([
backtrace
+ copy_file_range
fallocate
gettimeofday
lgetxattr
diff --git a/include/erofs/io.h b/include/erofs/io.h
index 557424578ece..5b685d217a0f 100644
--- a/include/erofs/io.h
+++ b/include/erofs/io.h
@@ -9,6 +9,7 @@
#ifndef __EROFS_IO_H
#define __EROFS_IO_H
+#define _GNU_SOURCE
#include <unistd.h>
#include "internal.h"
@@ -26,6 +27,10 @@ int dev_fsync(void);
int dev_resize(erofs_blk_t nblocks);
u64 dev_length(void);
+int erofs_copy_file_range(int fd_in, erofs_off_t *off_in,
+ int fd_out, erofs_off_t *off_out,
+ size_t length);
+
static inline int blk_write(const void *buf, erofs_blk_t blkaddr,
u32 nblocks)
{
diff --git a/lib/io.c b/lib/io.c
index 6067041fd829..e4083bb53c27 100644
--- a/lib/io.c
+++ b/lib/io.c
@@ -261,3 +261,98 @@ int dev_read(void *buf, u64 offset, size_t len)
}
return 0;
}
+
+static int __erofs_copy_file_range(int fd_in, erofs_off_t *off_in,
+ int fd_out, erofs_off_t *off_out,
+ size_t length)
+{
+ size_t copied = 0;
+ char buf[8192];
+
+ /*
+ * Main copying loop. The buffer size is arbitrary and is a
+ * trade-off between stack size consumption, cache usage, and
+ * amortization of system call overhead.
+ */
+ while (length > 0) {
+ size_t to_read;
+ ssize_t read_count;
+ char *end, *p;
+
+ to_read = min_t(size_t, length, sizeof(buf));
+#ifdef HAVE_PREAD64
+ read_count = pread64(fd_in, buf, to_read, *off_in);
+#else
+ read_count = pread(fd_in, buf, to_read, *off_in);
+#endif
+ if (read_count == 0)
+ /* End of file reached prematurely. */
+ return copied;
+ if (read_count < 0) {
+ /* Report the number of bytes copied so far. */
+ if (copied > 0)
+ return copied;
+ return -1;
+ }
+ *off_in += read_count;
+
+ /* Write the buffer part which was read to the destination. */
+ end = buf + read_count;
+ for (p = buf; p < end; ) {
+ ssize_t write_count;
+
+#ifdef HAVE_PWRITE64
+ write_count = pwrite64(fd_out, p, end - p, *off_out);
+#else
+ write_count = pwrite(fd_out, p, end - p, *off_out);
+#endif
+ if (write_count < 0) {
+ /*
+ * Adjust the input read position to match what
+ * we have written, so that the caller can pick
+ * up after the error.
+ */
+ size_t written = p - buf;
+ /*
+ * NB: This needs to be signed so that we can
+ * form the negative value below.
+ */
+ ssize_t overread = read_count - written;
+
+ *off_in -= overread;
+ /* Report the number of bytes copied so far. */
+ if (copied + written > 0)
+ return copied + written;
+ return -1;
+ }
+ p += write_count;
+ *off_out += write_count;
+ } /* Write loop. */
+ copied += read_count;
+ length -= read_count;
+ }
+ return copied;
+}
+
+int erofs_copy_file_range(int fd_in, erofs_off_t *off_in,
+ int fd_out, erofs_off_t *off_out,
+ size_t length)
+{
+#ifdef HAVE_COPY_FILE_RANGE
+ off64_t off64_in = *off_in, off64_out = *off_out;
+ ssize_t ret;
+
+ ret = copy_file_range(fd_in, &off64_in, fd_out, &off64_out,
+ length, 0);
+ if (ret >= 0)
+ goto out;
+ if (errno != ENOSYS) {
+ ret = -errno;
+out:
+ *off_in = off64_in;
+ *off_out = off64_out;
+ return ret;
+ }
+#endif
+ return __erofs_copy_file_range(fd_in, off_in, fd_out, off_out, length);
+}
--
2.24.4
next prev parent reply other threads:[~2021-08-18 7:03 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-18 7:03 [PATCH 1/4] erofs-utils: introduce hashmap from git source Gao Xiang
2021-08-18 7:03 ` [PATCH 2/4] erofs-utils: introduce sha256 Gao Xiang
2021-08-18 7:03 ` Gao Xiang [this message]
2021-08-18 7:03 ` [PATCH 4/4] erofs-utils: support chunk-based uncompressed files Gao Xiang
2021-08-19 6:57 ` [PATCH v2 " Gao Xiang
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=20210818070316.1970-3-hsiangkao@linux.alibaba.com \
--to=hsiangkao@linux.alibaba.com \
--cc=bo.liu@linux.alibaba.com \
--cc=chao@kernel.org \
--cc=eguan@linux.alibaba.com \
--cc=gerry@linux.alibaba.com \
--cc=joseph.qi@linux.alibaba.com \
--cc=linux-erofs@lists.ozlabs.org \
--cc=tao.peng@linux.alibaba.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).