#include /* for assert(3p) */ #include /* for errno */ #include /* for error(3gnu) */ #include /* for open(2) */ #include /* for uint64_t, PRIu64 */ #include /* for FIDEDUPRANGE and related */ #include /* for bool, true, false */ #include /* for exit(3p), EXIT_SUCCESS, EXIT_FAILURE, malloc(3p), calloc(3p), free(3p) */ #include /* for ioctl(2) */ #include /* for sysconf(3p), _SC_PAGESIZE */ #include "dedupe-range.h" #define MIN(a, b) ((a) < (b) ? (a) : (b)) void dedupe_range(uint64_t src_length, struct filepos src, struct filepos *dsts) { // Count how many destination ranges we have size_t dst_count; for (dst_count = 0; dsts[dst_count].filename; dst_count++); // Figure the maximum number of destination ranges we can fit // in 1 ioctl const size_t max_dst_count = (sysconf(_SC_PAGESIZE) - sizeof(struct file_dedupe_range)) / sizeof(struct file_dedupe_range_info); assert(max_dst_count > 0); // Open the source file int src_fd = open(src.filename, src.flags); if (src_fd < 0) error(EXIT_FAILURE, errno, "open src: %s", src.filename); // Open the destination files struct file_dedupe_range_info *range_info = calloc(dst_count, sizeof(struct file_dedupe_range_info)); if (!range_info) error(EXIT_FAILURE, errno, "malloc"); 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; } // Do the work struct file_dedupe_range *range = malloc(sysconf(_SC_PAGESIZE)); if (!range) error(EXIT_FAILURE, errno, "malloc"); for (size_t files_deduped = 0; files_deduped < dst_count; ) { // initialize the range structure *range = (struct file_dedupe_range){0}; range->dest_count = MIN(dst_count - files_deduped, max_dst_count); assert(range->dest_count > 0); for (size_t i = 0;i < range->dest_count; i++) range->info[i] = range_info[files_deduped+i]; // call FIDEDUPERANGE repeatedly to dedupe all of src_length uint64_t bytes_todo = src_length; uint64_t bytes_done = 0; while (bytes_todo > 0) { // allegedly, these need to be aligned to the // fundamental block size (statfs->f_frsize, // fall back to ->f_bsize) range->src_offset = src.offset + bytes_done; range->src_length = bytes_todo; if (ioctl(src_fd, FIDEDUPERANGE, range) < 0) error(EXIT_FAILURE, errno, "FIDEDUPERANGE"); // how much actually got done? uint64_t bytes_deduped = range->info[0].bytes_deduped; assert(bytes_deduped <= range->src_length); bytes_done += bytes_deduped; bytes_todo -= bytes_deduped; // verify that dedupe for each dst went ok bool erred = false; 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, 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(range->info[i].status < 0); error(0, -range->info[i].status, "dedupe: %s", dsts[files_deduped+i].filename); erred = true; break; } } if (erred == true) exit(EXIT_FAILURE); } files_deduped += range->dest_count; } free(range); for (size_t i = 0; i < dst_count; i++) close(range_info[i].dest_fd); free(range_info); close(src_fd); }