summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@beefcake.parabola.nu>2018-05-15 17:36:35 -0400
committerLuke Shumaker <lukeshu@beefcake.parabola.nu>2018-05-15 17:36:35 -0400
commitc787006ee3742ee8384210c4355f73aef7c8d0b8 (patch)
tree958ae42438961420796a60d7f4a5b0d7cdcc98d9
initial commit
-rw-r--r--.gitignore2
-rw-r--r--Makefile13
-rw-r--r--README.md4
-rw-r--r--lib/dedupe-range.c86
-rw-r--r--lib/dedupe-range.h10
-rw-r--r--src/cow-dedupe-range.c97
6 files changed, 212 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..59a4ba5
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+/cow-*
+*.o
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..10ebe01
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,13 @@
+CFLAGS += -std=c11
+CPPFLAGS += -I$(CURDIR)/lib
+CFLAGS += -Wall -Werror -Wextra
+CPPFLAGS += -O2 -D_FORTIFY_SOURCE=2
+
+all: cow-dedupe-range
+.PHONY: all
+
+cow-dedupe-range: src/cow-dedupe-range.o lib/dedupe-range.o
+ $(CC) $(LDFLAGS) -o $@ $^
+
+.SECONDARY:
+.DELETE_ON_ERROR:
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..bd7afd9
--- /dev/null
+++ b/README.md
@@ -0,0 +1,4 @@
+- ioctl
+ * FICLONE
+ * FICLONERANGE
+ * FIDEDUPERANGE
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;
+ }
+}
diff --git a/lib/dedupe-range.h b/lib/dedupe-range.h
new file mode 100644
index 0000000..028fe08
--- /dev/null
+++ b/lib/dedupe-range.h
@@ -0,0 +1,10 @@
+#include <stdint.h>
+
+struct range {
+ char *filename;
+ int flags; /* to pass to open(2) */
+ uint64_t offset;
+ uint64_t length;
+};
+
+void dedupe_range(struct range src, struct range *dsts);
diff --git a/src/cow-dedupe-range.c b/src/cow-dedupe-range.c
new file mode 100644
index 0000000..7508f04
--- /dev/null
+++ b/src/cow-dedupe-range.c
@@ -0,0 +1,97 @@
+#define _GNU_SOURCE /* for program_invocation_name */
+#include <assert.h> /* for assert(3p) */
+#include <errno.h> /* for errno, program_invocation_name */
+#include <error.h> /* for error(3gnu) */
+#include <fcntl.h> /* for O_RDONLY, O_RDWR */
+#include <getopt.h> /* for getopt_long(3gnu), struct option, optind */
+#include <stdbool.h> /* for bool, true, false */
+#include <stdint.h> /* for uint64_t */
+#include <stdio.h> /* for printf(3gnu), fprintf(3p), stderr */
+#include <stdlib.h> /* for strtoll(3p), calloc(3p), free(3p) */
+#include <string.h> /* for strlen(3p) */
+#include <unistd.h> /* for dup2(3p) */
+
+#include "dedupe-range.h" /* for dedupe_range, struct range */
+
+bool atou64(const char *str, uint64_t *ret) {
+ assert(sizeof(uint64_t) <= sizeof(unsigned long long int));
+ if (!('0' <= str[0] && str[0] <= '9'))
+ return false;
+ errno = 0;
+ char *end;
+ unsigned long long int n = strtoll(str, &end, 10);
+ if (end[0] != '\0' || errno != 0)
+ return false;
+ *ret = (uint64_t)n;
+ return true;
+}
+
+void usage() {
+ printf("Usage: %2$*1$s [OPTIONS] SRC_FILENAME SRC_OFFSET SRC_LENGTH \\\n"
+ " %3$*1$s DST_FILENAME DST_OFFSET \\\n"
+ " %3$*1$s [DST_FILENAME DST_OFFSET]...\n"
+ "Submit a file deduplication request to the kernel.\n"
+ "If the file ranges are not duplicates, the kernel will ignore this request.\n"
+ "\n"
+ "OPTIONS:\n"
+ " -r, --readonly Open destination files read-only, rather than read/write.\n"
+ " Generically, the destination must be open for writing.\n"
+ " However, on Btrfs, this allows deduplication to be done\n"
+ " on read-only subvolumes when invoked as root.\n"
+ " -h, --help Display this help and exit.\n",
+ (int)strlen(program_invocation_name), program_invocation_name, "");
+}
+
+int main(int argc, char *argv[]) {
+ int ro = 0;
+ struct option long_options[] = {
+ {"readonly", no_argument, &ro, 1},
+ {"help", no_argument, NULL, 'h'},
+ {0}
+ };
+ int flag;
+ while ((flag = getopt_long(argc, argv, "rh", long_options, NULL)) >= 0) {
+ switch (flag) {
+ case 0:
+ break;
+ case 'r':
+ ro = 1;
+ break;
+ case 'h':
+ usage();
+ return 0;
+ case '?':
+ fprintf(stderr, "Try '%s --help' for more information.\n", program_invocation_name);
+ return 2;
+ default:
+ assert(false);
+ }
+ }
+
+ if (argc - optind < 5) {
+ error(0, 0, "too few arguments");
+ fprintf(stderr, "Try '%s --help' for more information.\n", program_invocation_name);
+ return 2;
+ }
+
+ struct range src;
+ src.filename = argv[optind];
+ src.flags = O_RDONLY;
+ if (!atou64(argv[optind+1], &src.offset))
+ error(2, errno, "invalid offset '%s'", argv[optind+1]);
+ if (!atou64(argv[optind+2], &src.length))
+ error(2, errno, "invalid length '%s'", argv[optind+2]);
+
+ const size_t dst_count = (argc - optind - 3) / 2;
+ struct range *dsts = calloc(dst_count + 1, sizeof(struct range));
+ for (size_t i = 0; i < dst_count; i++) {
+ dsts[i].filename = argv[optind+3+(i*2)];
+ dsts[i].flags = ro ? O_RDONLY : O_RDWR;
+ if (!atou64(argv[optind+3+(i*2)+1], &dsts[i].offset))
+ error(2, errno, "invalid offset '%s'", argv[optind+3+(i*2)+1]);
+ }
+
+ dedupe_range(src, dsts);
+ free(dsts);
+ return 0;
+}