chiark / gitweb /
copy: use btrfs reflinking only whe we know we copy full files
authorLennart Poettering <lennart@poettering.net>
Fri, 12 Dec 2014 15:24:33 +0000 (16:24 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 12 Dec 2014 16:30:25 +0000 (17:30 +0100)
src/core/ima-setup.c
src/journal/coredump.c
src/machine/machine-dbus.c
src/shared/copy.c
src/shared/copy.h
src/systemctl/systemctl.c
src/sysusers/sysusers.c
src/test/test-copy.c

index 8e4fed17e7fc327d5356234468e810539fe1b30d..0e0d16a7c98c6d2a16e0d61281be7a437bb86257 100644 (file)
@@ -66,7 +66,7 @@ int ima_setup(void) {
                 return 0;
         }
 
-        r = copy_bytes(policyfd, imafd, -1);
+        r = copy_bytes(policyfd, imafd, (off_t) -1, false);
         if (r < 0)
                 log_error_errno(r, "Failed to load the IMA custom policy file "IMA_POLICY_PATH": %m");
         else
index 8678ec6a59e131db22ac43924728eafd0d7dee9f..a37e5eb8a782d3ff24c3ab986da463b94de18c3a 100644 (file)
@@ -316,7 +316,7 @@ static int save_external_coredump(
         if (fd < 0)
                 return log_error_errno(errno, "Failed to create coredump file %s: %m", tmp);
 
-        r = copy_bytes(STDIN_FILENO, fd, arg_process_size_max);
+        r = copy_bytes(STDIN_FILENO, fd, arg_process_size_max, false);
         if (r == -EFBIG) {
                 log_error("Coredump of %s (%s) is larger than configured processing limit, refusing.", info[INFO_PID], info[INFO_COMM]);
                 goto fail;
index 72ae6c65354df132b28e9dcb6aad4c851850ae31..f6fd9cf3611c5b9904bb14c9d5a4539f1c90f6a8 100644 (file)
@@ -344,7 +344,7 @@ int bus_machine_method_get_os_release(sd_bus *bus, sd_bus_message *message, void
                                 _exit(EXIT_FAILURE);
                 }
 
-                r = copy_bytes(fd, pair[1], (off_t) -1);
+                r = copy_bytes(fd, pair[1], (off_t) -1, false);
                 if (r < 0)
                         _exit(EXIT_FAILURE);
 
index 233dbbccc778ee53b2962519b378038fc81626ba..b4a85c7bff0a12f76835a7cce3f867be37951b30 100644 (file)
@@ -25,7 +25,7 @@
 #include "btrfs-util.h"
 #include "copy.h"
 
-int copy_bytes(int fdf, int fdt, off_t max_bytes) {
+int copy_bytes(int fdf, int fdt, off_t max_bytes, bool try_reflink) {
         bool try_sendfile = true;
         int r;
 
@@ -33,10 +33,10 @@ int copy_bytes(int fdf, int fdt, off_t max_bytes) {
         assert(fdt >= 0);
 
         /* Try btrfs reflinks first. */
-        if (max_bytes == (off_t) -1) {
+        if (try_reflink && max_bytes == (off_t) -1) {
                 r = btrfs_reflink(fdf, fdt);
                 if (r >= 0)
-                        return 0;
+                        return r;
         }
 
         for (;;) {
@@ -131,7 +131,7 @@ static int fd_copy_regular(int df, const char *from, const struct stat *st, int
         if (fdt < 0)
                 return -errno;
 
-        r = copy_bytes(fdf, fdt, (off_t) -1);
+        r = copy_bytes(fdf, fdt, (off_t) -1, true);
         if (r < 0) {
                 unlinkat(dt, to, 0);
                 return r;
@@ -318,7 +318,7 @@ int copy_tree_fd(int dirfd, const char *to, bool merge) {
         return fd_copy_directory(dirfd, NULL, &st, AT_FDCWD, to, st.st_dev, merge);
 }
 
-int copy_file_fd(const char *from, int fdt) {
+int copy_file_fd(const char *from, int fdt, bool try_reflink) {
         _cleanup_close_ int fdf = -1;
 
         assert(from);
@@ -328,7 +328,7 @@ int copy_file_fd(const char *from, int fdt) {
         if (fdf < 0)
                 return -errno;
 
-        return copy_bytes(fdf, fdt, (off_t) -1);
+        return copy_bytes(fdf, fdt, (off_t) -1, try_reflink);
 }
 
 int copy_file(const char *from, const char *to, int flags, mode_t mode) {
@@ -341,7 +341,7 @@ int copy_file(const char *from, const char *to, int flags, mode_t mode) {
         if (fdt < 0)
                 return -errno;
 
-        r = copy_file_fd(from, fdt);
+        r = copy_file_fd(from, fdt, true);
         if (r < 0) {
                 close(fdt);
                 unlink(to);
index 15faf548a1088680053f13a98b05eb882eeb4b94..201fe692ced9dd8c0f392b7421cff56c3b6c46f1 100644 (file)
@@ -24,8 +24,8 @@
 #include <stdbool.h>
 #include <sys/types.h>
 
-int copy_file_fd(const char *from, int to);
+int copy_file_fd(const char *from, int to, bool try_reflink);
 int copy_file(const char *from, const char *to, int flags, mode_t mode);
 int copy_tree(const char *from, const char *to, bool merge);
 int copy_tree_fd(int dirfd, const char *to, bool merge);
-int copy_bytes(int fdf, int fdt, off_t max_bytes);
+int copy_bytes(int fdf, int fdt, off_t max_bytes, bool try_reflink);
index 5ed430c82f21e8de0a0aae8187f40365f6aa52a2..a75e9dee170810d38e572e9d84b931d3b2fe0bc5 100644 (file)
@@ -4641,7 +4641,7 @@ static int cat(sd_bus *bus, char **args) {
                                ansi_highlight_off());
                         fflush(stdout);
 
-                        r = copy_file_fd(fragment_path, STDOUT_FILENO);
+                        r = copy_file_fd(fragment_path, STDOUT_FILENO, false);
                         if (r < 0) {
                                 log_warning_errno(r, "Failed to cat %s: %m", fragment_path);
                                 continue;
@@ -4656,7 +4656,7 @@ static int cat(sd_bus *bus, char **args) {
                                ansi_highlight_off());
                         fflush(stdout);
 
-                        r = copy_file_fd(*path, STDOUT_FILENO);
+                        r = copy_file_fd(*path, STDOUT_FILENO, false);
                         if (r < 0) {
                                 log_warning_errno(r, "Failed to cat %s: %m", *path);
                                 continue;
index 647eb577a602671d13fd22426bbbba9d0d23f7ec..0baa2c39d842791f0132ac88ff7884d29a779c7f 100644 (file)
@@ -215,7 +215,7 @@ static int make_backup(const char *target, const char *x) {
         if (r < 0)
                 return r;
 
-        r = copy_bytes(src, fileno(dst), (off_t) -1);
+        r = copy_bytes(src, fileno(dst), (off_t) -1, true);
         if (r < 0)
                 goto fail;
 
index d2cad08cb69c86b40b2a817d96535874b05eec32..d70a0be2a22b798052073756645a18c9d02b5195 100644 (file)
@@ -67,8 +67,8 @@ static void test_copy_file_fd(void) {
         assert_se(out_fd >= 0);
 
         assert_se(write_string_file(in_fn, text) == 0);
-        assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd) < 0);
-        assert_se(copy_file_fd(in_fn, out_fd) >= 0);
+        assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, true) < 0);
+        assert_se(copy_file_fd(in_fn, out_fd, true) >= 0);
         assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
 
         assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);