1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
#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, malloc(3p), calloc(3p), free(3p) */
#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));
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;
}
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));
if (!range)
error(EXIT_FAILURE, errno, "malloc");
*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,
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;
free(range);
}
free(range_info);
}
|