2 * Tiny program to perform file (range) clones using raw Btrfs ioctls.
3 * It should only be needed until btrfs-progs has an xfs_io equivalent.
5 * Copyright (C) 2014 SUSE Linux Products GmbH. All Rights Reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 #include <sys/types.h>
24 #include <sys/ioctl.h>
34 struct btrfs_ioctl_clone_range_args {
41 #define BTRFS_IOCTL_MAGIC 0x94
42 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
43 #define BTRFS_IOC_CLONE_RANGE _IOW(BTRFS_IOCTL_MAGIC, 13, \
44 struct btrfs_ioctl_clone_range_args)
47 usage(char *name, const char *msg)
51 "%s [options] <src_file> <dest_file>\n"
52 "\tA full file clone (reflink) is performed by default, "
53 "unless any of the following are specified:\n"
54 "\t-s <offset>: source file offset (default = 0)\n"
55 "\t-d <offset>: destination file offset (default = 0)\n"
56 "\t-l <length>: length of clone (default = 0)\n",
62 clone_file(int src_fd, int dst_fd)
64 int ret = ioctl(dst_fd, BTRFS_IOC_CLONE, src_fd);
71 clone_file_range(int src_fd, int dst_fd, uint64_t src_off, uint64_t dst_off,
74 struct btrfs_ioctl_clone_range_args cr_args;
77 memset(&cr_args, 0, sizeof(cr_args));
78 cr_args.src_fd = src_fd;
79 cr_args.src_offset = src_off;
80 cr_args.src_length = len;
81 cr_args.dest_offset = dst_off;
82 ret = ioctl(dst_fd, BTRFS_IOC_CLONE_RANGE, &cr_args);
89 main(int argc, char **argv)
91 bool full_file = true;
102 while ((opt = getopt(argc, argv, "s:d:l:")) != -1) {
107 src_off = strtoull(optarg, &sval_end, 10);
108 if ((errno) || (*sval_end != '\0'))
109 usage(argv[0], "invalid source offset");
114 dst_off = strtoull(optarg, &sval_end, 10);
115 if ((errno) || (*sval_end != '\0'))
116 usage(argv[0], "invalid destination offset");
121 len = strtoull(optarg, &sval_end, 10);
122 if ((errno) || (*sval_end != '\0'))
123 usage(argv[0], "invalid length");
127 usage(argv[0], "invalid argument");
131 /* should be exactly two args left */
132 if (optind != argc - 2)
133 usage(argv[0], "src_file and dst_file arguments are madatory");
135 src_file = (char *)strdup(argv[optind++]);
136 if (src_file == NULL) {
138 printf("no memory\n");
141 dst_file = (char *)strdup(argv[optind++]);
142 if (dst_file == NULL) {
144 printf("no memory\n");
148 src_fd = open(src_file, O_RDONLY);
151 printf("failed to open %s: %s\n", src_file, strerror(errno));
154 dst_fd = open(dst_file, O_CREAT | O_WRONLY, 0644);
157 printf("failed to open %s: %s\n", dst_file, strerror(errno));
162 ret = clone_file(src_fd, dst_fd);
164 ret = clone_file_range(src_fd, dst_fd, src_off, dst_off, len);
167 printf("clone failed: %s\n", strerror(ret));
175 printf("failed to close dst file: %s\n", strerror(errno));
180 printf("failed to close src file: %s\n", strerror(errno));