archrelease: copy trunk to extra-x86_64
[arch-packages.git] / coreutils / repos / core-x86_64 / copy-fix-reflink-auto-fallback.patch
blob22ede9b49657eaf675af1f529d57c83ab463f8e2
1 From 093a8b4bfaba60005f14493ce7ef11ed665a0176 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?P=C3=A1draig=20Brady?= <P@draigBrady.com>
3 Date: Thu, 23 Mar 2023 13:19:04 +0000
4 Subject: copy: fix --reflink=auto to fallback in more cases
6 On restricted systems like android or some containers,
7 FICLONE could return EPERM, EACCES, or ENOTTY,
8 which would have induced the command to fail to copy
9 rather than falling back to a more standard copy.
11 * src/copy.c (is_terminal_failure): A new function refactored
12 from handle_clone_fail().
13 (is_CLONENOTSUP): Merge in the handling of EACCES, ENOTTY, EPERM
14 as they also pertain to determination of whether cloning is supported
15 if we ever use this function in that context.
16 (handle_clone_fail): Use is_terminal_failure() in all cases,
17 so that we assume a terminal failure in less errno cases.
18 * NEWS: Mention the bug fix.
19 Addresses https://bugs.gnu.org/62404
20 ---
21 NEWS | 8 ++++++++
22 src/copy.c | 62 +++++++++++++++++++++++++++++++++++---------------------------
23 2 files changed, 43 insertions(+), 27 deletions(-)
25 diff --git a/src/copy.c b/src/copy.c
26 index 3919787..f8ba058 100644
27 --- a/src/copy.c
28 +++ b/src/copy.c
29 @@ -278,15 +278,27 @@ create_hole (int fd, char const *name, bool punch_holes, off_t size)
33 -/* Whether the errno from FICLONE, or copy_file_range
34 - indicates operation is not supported for this file or file system. */
35 +/* Whether the errno indicates the operation is a transient failure.
36 + I.e., a failure that would indicate the operation _is_ supported,
37 + but has failed in a terminal way. */
39 +static bool
40 +is_terminal_error (int err)
42 + return err == EIO || err == ENOMEM || err == ENOSPC || err == EDQUOT;
46 +/* Whether the errno from FICLONE, or copy_file_range indicates
47 + the operation is not supported/allowed for this file or process. */
49 static bool
50 is_CLONENOTSUP (int err)
52 - return err == ENOSYS || is_ENOTSUP (err)
53 + return err == ENOSYS || err == ENOTTY || is_ENOTSUP (err)
54 || err == EINVAL || err == EBADF
55 - || err == EXDEV || err == ETXTBSY;
56 + || err == EXDEV || err == ETXTBSY
57 + || err == EPERM || err == EACCES;
61 @@ -339,20 +351,18 @@ sparse_copy (int src_fd, int dest_fd, char **abuf, size_t buf_size,
63 copy_debug.offload = COPY_DEBUG_UNSUPPORTED;
65 - if (is_CLONENOTSUP (errno))
66 - break;
68 - /* copy_file_range might not be enabled in seccomp filters,
69 - so retry with a standard copy. EPERM can also occur
70 - for immutable files, but that would only be in the edge case
71 - where the file is made immutable after creating/truncating,
72 + /* Consider operation unsupported only if no data copied.
73 + For example, EPERM could occur if copy_file_range not enabled
74 + in seccomp filters, so retry with a standard copy. EPERM can
75 + also occur for immutable files, but that would only be in the
76 + edge case where the file is made immutable after creating,
77 in which case the (more accurate) error is still shown. */
78 - if (errno == EPERM && *total_n_read == 0)
79 + if (*total_n_read == 0 && is_CLONENOTSUP (errno))
80 break;
82 /* ENOENT was seen sometimes across CIFS shares, resulting in
83 no data being copied, but subsequent standard copies succeed. */
84 - if (errno == ENOENT && *total_n_read == 0)
85 + if (*total_n_read == 0 && errno == ENOENT)
86 break;
88 if (errno == EINTR)
89 @@ -1172,17 +1182,15 @@ handle_clone_fail (int dst_dirfd, char const* dst_relname,
90 char const* src_name, char const* dst_name,
91 int dest_desc, bool new_dst, enum Reflink_type reflink_mode)
93 - /* If the clone operation is creating the destination,
94 - then don't try and cater for all non transient file system errors,
95 - and instead only cater for specific transient errors. */
96 - bool transient_failure;
97 - if (dest_desc < 0) /* currently for fclonefileat(). */
98 - transient_failure = errno == EIO || errno == ENOMEM
99 - || errno == ENOSPC || errno == EDQUOT;
100 - else /* currently for FICLONE. */
101 - transient_failure = ! is_CLONENOTSUP (errno);
103 - if (reflink_mode == REFLINK_ALWAYS || transient_failure)
104 + /* When the clone operation fails, report failure only with errno values
105 + known to mean trouble when the clone is supported and called properly.
106 + Do not report failure merely because !is_CLONENOTSUP (errno),
107 + as systems may yield oddball errno values here with FICLONE.
108 + Also is_CLONENOTSUP() is not appropriate for the range of errnos
109 + possible from fclonefileat(), so it's more consistent to avoid. */
110 + bool report_failure = is_terminal_error (errno);
112 + if (reflink_mode == REFLINK_ALWAYS || report_failure)
113 error (0, errno, _("failed to clone %s from %s"),
114 quoteaf_n (0, dst_name), quoteaf_n (1, src_name));
116 @@ -1190,14 +1198,14 @@ handle_clone_fail (int dst_dirfd, char const* dst_relname,
117 but cloned no data. */
118 if (new_dst /* currently not for fclonefileat(). */
119 && reflink_mode == REFLINK_ALWAYS
120 - && ((! transient_failure) || lseek (dest_desc, 0, SEEK_END) == 0)
121 + && ((! report_failure) || lseek (dest_desc, 0, SEEK_END) == 0)
122 && unlinkat (dst_dirfd, dst_relname, 0) != 0 && errno != ENOENT)
123 error (0, errno, _("cannot remove %s"), quoteaf (dst_name));
125 - if (! transient_failure)
126 + if (! report_failure)
127 copy_debug.reflink = COPY_DEBUG_UNSUPPORTED;
129 - if (reflink_mode == REFLINK_ALWAYS || transient_failure)
130 + if (reflink_mode == REFLINK_ALWAYS || report_failure)
131 return false;
133 return true;
135 cgit v1.1