]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
safe_io: add functions for handling splice
authorJosh Durgin <josh.durgin@inktank.com>
Sun, 20 Oct 2013 17:46:49 +0000 (10:46 -0700)
committerJosh Durgin <josh.durgin@inktank.com>
Sat, 23 Nov 2013 00:14:03 +0000 (16:14 -0800)
Like the other functions, these don't handle non-blocking I/O.

Signed-off-by: Josh Durgin <josh.durgin@inktank.com>
configure.ac
src/common/safe_io.c
src/common/safe_io.h

index 58c5e1b94a634d2f3c3277e464800e7576f334fa..91c623abdaf162e06f306982d25ec70ea3daf524 100644 (file)
@@ -536,6 +536,12 @@ AC_CHECK_FUNC([fallocate],
        [])
 
 
+# splice/tee
+AC_CHECK_FUNC([splice],
+       [AC_DEFINE([CEPH_HAVE_SPLICE], [], [splice(2) is supported])],
+       [])
+
+AC_CHECK_HEADERS([arpa/nameser_compat.h])
 AC_CHECK_HEADERS([sys/prctl.h])
 AC_CHECK_FUNCS([prctl])
 
index afee82edf0778b7a5ab8c9ddc10b3bc1e0b38173..16cc7293d9cd3d64a4f1f38017a4a28f7d173775 100644 (file)
@@ -117,6 +117,40 @@ ssize_t safe_pwrite(int fd, const void *buf, size_t count, off_t offset)
        return 0;
 }
 
+#ifdef CEPH_HAVE_SPLICE
+ssize_t safe_splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out,
+                   size_t len, unsigned int flags)
+{
+  size_t cnt = 0;
+
+  while (cnt < len) {
+    ssize_t r = splice(fd_in, off_in, fd_out, off_out, len - cnt, flags);
+    if (r <= 0) {
+      if (r == 0) {
+       // EOF
+       return cnt;
+      }
+      if (errno == EINTR)
+       continue;
+      return -errno;
+    }
+    cnt += r;
+  }
+  return cnt;
+}
+
+ssize_t safe_splice_exact(int fd_in, loff_t *off_in, int fd_out,
+                         loff_t *off_out, size_t len, unsigned int flags)
+{
+  ssize_t ret = safe_splice(fd_in, off_in, fd_out, off_out, len, flags);
+  if (ret < 0)
+    return ret;
+  if ((size_t)ret != len)
+    return -EDOM;
+  return 0;
+}
+#endif
+
 int safe_write_file(const char *base, const char *file,
                    const char *val, size_t vallen)
 {
index a4c9bc7a72f916c8dfa4ef1e95de0d2dfbc611df..c45589eee7e05670e047ac5524e3c91ad64f5621 100644 (file)
@@ -15,6 +15,7 @@
 #ifndef CEPH_SAFE_IO
 #define CEPH_SAFE_IO
 
+#include "acconfig.h"
 #include "common/compiler_extensions.h"
 #include <sys/types.h>
 
@@ -35,6 +36,18 @@ extern "C" {
       WARN_UNUSED_RESULT;
   ssize_t safe_pwrite(int fd, const void *buf, size_t count, off_t offset)
       WARN_UNUSED_RESULT;
+#ifdef CEPH_HAVE_SPLICE
+  /*
+   * Similar to the above (non-exact version) and below (exact version).
+   * See splice(2) for parameter descriptions.
+   */
+  ssize_t safe_splice(int fd_in, loff_t *off_in, int fd_out, loff_t *off_out,
+                     size_t len, unsigned int flags)
+    WARN_UNUSED_RESULT;
+  ssize_t safe_splice_exact(int fd_in, loff_t *off_in, int fd_out,
+                           loff_t *off_out, size_t len, unsigned int flags)
+    WARN_UNUSED_RESULT;
+#endif
 
   /*
    * Same as the above functions, but return -EDOM unless exactly the requested