001-copy-fix-reflink-auto-to-fallback-in-more-cases.patch 5.4 KB

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