Blame 0001-dracut-install.c-try-clone-ioctl-for-more-speed.patch

Harald Hoyer c4a5a0
From 85854b245e1090970d566d6432dabc315e17461c Mon Sep 17 00:00:00 2001
Harald Hoyer c4a5a0
From: Harald Hoyer <harald@redhat.com>
Harald Hoyer c4a5a0
Date: Sat, 30 Jun 2012 09:06:13 +0200
Harald Hoyer c4a5a0
Subject: [PATCH] dracut-install.c: try clone ioctl for more speed
Harald Hoyer c4a5a0
Harald Hoyer c4a5a0
---
Harald Hoyer c4a5a0
 install/dracut-install.c |   64 ++++++++++++++++++++++++++++++++++++++++++----
Harald Hoyer c4a5a0
 1 file changed, 59 insertions(+), 5 deletions(-)
Harald Hoyer c4a5a0
Harald Hoyer c4a5a0
diff --git a/install/dracut-install.c b/install/dracut-install.c
Harald Hoyer c4a5a0
index ccd4ba4..86c32db 100644
Harald Hoyer c4a5a0
--- a/install/dracut-install.c
Harald Hoyer c4a5a0
+++ b/install/dracut-install.c
Harald Hoyer c4a5a0
@@ -39,6 +39,7 @@
Harald Hoyer c4a5a0
 #include <sys/types.h>
Harald Hoyer c4a5a0
 #include <sys/wait.h>
Harald Hoyer c4a5a0
 #include <unistd.h>
Harald Hoyer c4a5a0
+#include <sys/ioctl.h>
Harald Hoyer c4a5a0
 
Harald Hoyer c4a5a0
 #include "log.h"
Harald Hoyer c4a5a0
 #include "hashmap.h"
Harald Hoyer c4a5a0
@@ -163,25 +164,78 @@ static int ln_r(const char *src, const char *dst)
Harald Hoyer c4a5a0
         return 0;
Harald Hoyer c4a5a0
 }
Harald Hoyer c4a5a0
 
Harald Hoyer c4a5a0
+/* Perform the O(1) btrfs clone operation, if possible.
Harald Hoyer c4a5a0
+   Upon success, return 0.  Otherwise, return -1 and set errno.  */
Harald Hoyer c4a5a0
+static inline int clone_file(int dest_fd, int src_fd)
Harald Hoyer c4a5a0
+{
Harald Hoyer c4a5a0
+#undef BTRFS_IOCTL_MAGIC
Harald Hoyer c4a5a0
+#define BTRFS_IOCTL_MAGIC 0x94
Harald Hoyer c4a5a0
+#undef BTRFS_IOC_CLONE
Harald Hoyer c4a5a0
+#define BTRFS_IOC_CLONE _IOW (BTRFS_IOCTL_MAGIC, 9, int)
Harald Hoyer c4a5a0
+        return ioctl(dest_fd, BTRFS_IOC_CLONE, src_fd);
Harald Hoyer c4a5a0
+}
Harald Hoyer c4a5a0
+
Harald Hoyer c4a5a0
+static bool use_clone = true;
Harald Hoyer c4a5a0
+
Harald Hoyer c4a5a0
 static int cp(const char *src, const char *dst)
Harald Hoyer c4a5a0
 {
Harald Hoyer c4a5a0
         int pid;
Harald Hoyer c4a5a0
-        int status;
Harald Hoyer c4a5a0
+        int ret;
Harald Hoyer c4a5a0
+
Harald Hoyer c4a5a0
+        if(use_clone) {
Harald Hoyer c4a5a0
+                struct stat sb;
Harald Hoyer c4a5a0
+                int dest_desc, source_desc;
Harald Hoyer c4a5a0
+
Harald Hoyer c4a5a0
+                if (lstat(src, &sb) != 0)
Harald Hoyer c4a5a0
+                        goto normal_copy;
Harald Hoyer c4a5a0
+
Harald Hoyer c4a5a0
+                if (S_ISLNK(sb.st_mode))
Harald Hoyer c4a5a0
+                        goto normal_copy;
Harald Hoyer c4a5a0
+
Harald Hoyer c4a5a0
+                source_desc = open(src, O_RDONLY | O_CLOEXEC);
Harald Hoyer c4a5a0
+                if (source_desc < 0)
Harald Hoyer c4a5a0
+                        goto normal_copy;
Harald Hoyer c4a5a0
 
Harald Hoyer c4a5a0
+                dest_desc =
Harald Hoyer c4a5a0
+                        open(dst, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC,
Harald Hoyer c4a5a0
+                             (sb.st_mode) & (S_ISUID | S_ISGID | S_ISVTX | S_IRWXU | S_IRWXG | S_IRWXO));
Harald Hoyer c4a5a0
+
Harald Hoyer c4a5a0
+                if (dest_desc < 0) {
Harald Hoyer c4a5a0
+                        close(source_desc);
Harald Hoyer c4a5a0
+                        goto normal_copy;
Harald Hoyer c4a5a0
+                }
Harald Hoyer c4a5a0
+
Harald Hoyer c4a5a0
+                ret = clone_file(dest_desc, source_desc);
Harald Hoyer c4a5a0
+                close(source_desc);
Harald Hoyer c4a5a0
+                if (ret == 0) {
Harald Hoyer c4a5a0
+                        if (fchown(dest_desc, sb.st_uid, sb.st_gid) != 0)
Harald Hoyer c4a5a0
+                                fchown(dest_desc, -1, sb.st_gid);
Harald Hoyer c4a5a0
+                        close(dest_desc);
Harald Hoyer c4a5a0
+                        return ret;
Harald Hoyer c4a5a0
+                }
Harald Hoyer c4a5a0
+                close(dest_desc);
Harald Hoyer c4a5a0
+
Harald Hoyer c4a5a0
+                /* clone did not work, remove the file */
Harald Hoyer c4a5a0
+                unlink(dst);
Harald Hoyer c4a5a0
+                /* do not try clone again */
Harald Hoyer c4a5a0
+                use_clone = false;
Harald Hoyer c4a5a0
+        }
Harald Hoyer c4a5a0
+
Harald Hoyer c4a5a0
+ normal_copy:
Harald Hoyer c4a5a0
         pid = fork();
Harald Hoyer c4a5a0
         if (pid == 0) {
Harald Hoyer c4a5a0
                 execlp("cp", "cp", "--reflink=auto", "--sparse=auto", "--preserve=mode", "-fL", src, dst, NULL);
Harald Hoyer c4a5a0
                 _exit(EXIT_FAILURE);
Harald Hoyer c4a5a0
         }
Harald Hoyer c4a5a0
 
Harald Hoyer c4a5a0
-        while (waitpid(pid, &status, 0) < 0) {
Harald Hoyer c4a5a0
+        while (waitpid(pid, &ret, 0) < 0) {
Harald Hoyer c4a5a0
                 if (errno != EINTR) {
Harald Hoyer c4a5a0
-                        status = -1;
Harald Hoyer c4a5a0
+                        ret = -1;
Harald Hoyer c4a5a0
                         break;
Harald Hoyer c4a5a0
                 }
Harald Hoyer c4a5a0
         }
Harald Hoyer c4a5a0
 
Harald Hoyer c4a5a0
-        return status;
Harald Hoyer c4a5a0
+        return ret;
Harald Hoyer c4a5a0
 }
Harald Hoyer c4a5a0
 
Harald Hoyer c4a5a0
 static int resolve_deps(const char *src)
Harald Hoyer c4a5a0
@@ -643,7 +697,7 @@ static int install_all(int argc, char **argv)
Harald Hoyer c4a5a0
                         free(dest);
Harald Hoyer c4a5a0
                 }
Harald Hoyer c4a5a0
 
Harald Hoyer c4a5a0
-                if ((ret != 0) &&  (!arg_optional)) {
Harald Hoyer c4a5a0
+                if ((ret != 0) && (!arg_optional)) {
Harald Hoyer c4a5a0
                         log_error("ERROR: installing '%s'", argv[i]);
Harald Hoyer c4a5a0
                         r = EXIT_FAILURE;
Harald Hoyer c4a5a0
                 }