summaryrefslogtreecommitdiff
path: root/lib/dedupe-range.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/dedupe-range.c')
-rw-r--r--lib/dedupe-range.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/lib/dedupe-range.c b/lib/dedupe-range.c
new file mode 100644
index 0000000..dea70c7
--- /dev/null
+++ b/lib/dedupe-range.c
@@ -0,0 +1,86 @@
+#include <assert.h> /* for assert(3p) */
+#include <errno.h> /* for errno */
+#include <error.h> /* for error(3gnu) */
+#include <fcntl.h> /* for open(2) */
+#include <inttypes.h> /* for uint64_t, PRIu64 */
+#include <linux/fs.h> /* for FIDEDUPRANGE and related */
+#include <stdbool.h> /* for bool, true, false */
+#include <stdlib.h> /* for exit(3p), EXIT_SUCCESS, EXIT_FAILURE */
+#include <sys/ioctl.h> /* for ioctl(2) */
+#include <unistd.h> /* for sysconf(3p), _SC_PAGESIZE */
+
+#include "dedupe-range.h"
+
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+
+void dedupe_range(struct range src, struct range *dsts) {
+ size_t dst_count;
+ for (dst_count = 0; dsts[dst_count].filename; dst_count++);
+
+ const size_t max_dst_count = (sysconf(_SC_PAGESIZE) - sizeof(struct file_dedupe_range))
+ / sizeof(struct file_dedupe_range_info);
+
+ int src_fd = open(src.filename, src.flags);
+ if (src_fd < 0)
+ error(EXIT_FAILURE, errno, "open src: %s", src.filename);
+
+ struct file_dedupe_range_info *range_info =
+ calloc(dst_count, sizeof(struct file_dedupe_range_info));
+ for (size_t i = 0; i < dst_count; i++) {
+ int dst_fd = open(dsts[i].filename, dsts[i].flags);
+ if (dst_fd < 0)
+ error(EXIT_FAILURE, errno, "open dst: %s", dsts[i].filename);
+ range_info[i].dest_fd = dst_fd;
+ range_info[i].dest_offset = dsts[i].offset;
+ }
+
+ for (size_t files_deduped = 0; files_deduped < dst_count; ) {
+ uint16_t dest_count = MIN(dst_count - files_deduped, max_dst_count);
+ struct file_dedupe_range *range = malloc(sizeof(struct file_dedupe_range) + dest_count * sizeof(struct file_dedupe_range_info));
+ *range = (struct file_dedupe_range){
+ .src_offset = src.offset,
+ .src_length = src.length,
+ .dest_count = dest_count,
+ .reserved1 = 0,
+ .reserved2 = 0,
+ };
+ for (size_t i = 0; i < dest_count; i++)
+ range->info[i] = range_info[files_deduped+i];
+
+ bool erred = false;
+ while (range->src_length > 0) {
+ if (ioctl(src_fd, FIDEDUPERANGE, &range) < 0)
+ error(EXIT_FAILURE, errno, "ioctl (FIDEDUPERANGE)");
+ uint64_t bytes_deduped = range->info[0].bytes_deduped;
+ assert(bytes_deduped <= range->src_length);
+ for (size_t i = 0; i < range->dest_count; i ++) {
+ if (range->info[i].bytes_deduped != bytes_deduped) {
+ error(0, errno, "dedupe: %"PRIu64" != %"PRIu64": %s",
+ bytes_deduped,
+ /* on platforms where both "long" and "long long"
+ * are 64 bits, linux __u64 might disagree with
+ * glibc uint64_t and thus PRIu64 */
+ (uint64_t)range->info[i].bytes_deduped,
+ dsts[files_deduped+i].filename);
+ erred = true;
+ }
+ switch (range->info[i].status) {
+ case FILE_DEDUPE_RANGE_DIFFERS:
+ error(0, 0, "dedupe: range differs: %s", dsts[files_deduped+i].filename);
+ erred = true;
+ break;
+ case FILE_DEDUPE_RANGE_SAME:
+ range->info[i].dest_offset += range->info[i].bytes_deduped;
+ break;
+ default:
+ assert(false);
+ }
+ }
+ if (erred == true)
+ exit(EXIT_FAILURE);
+ range->src_offset += bytes_deduped;
+ range->src_length -= bytes_deduped;
+ }
+ files_deduped += range->dest_count;
+ }
+}