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>
33 #ifdef HAVE_BTRFS_IOCTL_H
34 #include <btrfs/ioctl.h>
37 struct btrfs_ioctl_clone_range_args {
44 #define BTRFS_IOCTL_MAGIC 0x94
45 #define BTRFS_IOC_CLONE _IOW(BTRFS_IOCTL_MAGIC, 9, int)
46 #define BTRFS_IOC_CLONE_RANGE _IOW(BTRFS_IOCTL_MAGIC, 13, \
47 struct btrfs_ioctl_clone_range_args)
51 usage(char *name, const char *msg)
55 "%s [options] <src_file> <dest_file>\n"
56 "\tA full file clone (reflink) is performed by default, "
57 "unless any of the following are specified:\n"
58 "\t-s <offset>: source file offset (default = 0)\n"
59 "\t-d <offset>: destination file offset (default = 0)\n"
60 "\t-l <length>: length of clone (default = 0)\n",
66 clone_file(int src_fd, int dst_fd)
68 int ret = ioctl(dst_fd, BTRFS_IOC_CLONE, src_fd);
75 clone_file_range(int src_fd, int dst_fd, uint64_t src_off, uint64_t dst_off,
78 struct btrfs_ioctl_clone_range_args cr_args;
81 memset(&cr_args, 0, sizeof(cr_args));
82 cr_args.src_fd = src_fd;
83 cr_args.src_offset = src_off;
84 cr_args.src_length = len;
85 cr_args.dest_offset = dst_off;
86 ret = ioctl(dst_fd, BTRFS_IOC_CLONE_RANGE, &cr_args);
93 main(int argc, char **argv)
95 bool full_file = true;
106 while ((opt = getopt(argc, argv, "s:d:l:")) != -1) {
111 src_off = strtoull(optarg, &sval_end, 10);
112 if ((errno) || (*sval_end != '\0'))
113 usage(argv[0], "invalid source offset");
118 dst_off = strtoull(optarg, &sval_end, 10);
119 if ((errno) || (*sval_end != '\0'))
120 usage(argv[0], "invalid destination offset");
125 len = strtoull(optarg, &sval_end, 10);
126 if ((errno) || (*sval_end != '\0'))
127 usage(argv[0], "invalid length");
131 usage(argv[0], "invalid argument");
135 /* should be exactly two args left */
136 if (optind != argc - 2)
137 usage(argv[0], "src_file and dst_file arguments are madatory");
139 src_file = (char *)strdup(argv[optind++]);
140 if (src_file == NULL) {
142 printf("no memory\n");
145 dst_file = (char *)strdup(argv[optind++]);
146 if (dst_file == NULL) {
148 printf("no memory\n");
152 src_fd = open(src_file, O_RDONLY);
155 printf("failed to open %s: %s\n", src_file, strerror(errno));
158 dst_fd = open(dst_file, O_CREAT | O_WRONLY, 0644);
161 printf("failed to open %s: %s\n", dst_file, strerror(errno));
166 ret = clone_file(src_fd, dst_fd);
168 ret = clone_file_range(src_fd, dst_fd, src_off, dst_off, len);
171 printf("clone failed: %s\n", strerror(ret));
179 printf("failed to close dst file: %s\n", strerror(errno));
184 printf("failed to close src file: %s\n", strerror(errno));