1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright (c) 2023, Rob Norris <robn@despairlabs.com>
5 * Copyright (c) 2026, TrueNAS.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to
9 * deal in the Software without restriction, including without limitation the
10 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 * IN THE SOFTWARE.
24 */
25
26 /*
27 * This program is to test the availability and behaviour of various data
28 * cloning system calls. It should compile and run even if these features
29 * aren't exposed through the libc.
30 */
31
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36 #include <stdint.h>
37 #include <unistd.h>
38 #include <sys/syscall.h>
39 #include <stdlib.h>
40 #include <limits.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <errno.h>
44
45 #if defined(__linux__)
46 /*
47 * Not all Linux libc implementations define or expose copy_file_range(),
48 * for these we fall back to the syscall directly.
49 */
50
51 #ifndef __NR_copy_file_range
52 #if defined(__x86_64__)
53 #define __NR_copy_file_range (326)
54 #elif defined(__i386__)
55 #define __NR_copy_file_range (377)
56 #elif defined(__s390__)
57 #define __NR_copy_file_range (375)
58 #elif defined(__arm__)
59 #define __NR_copy_file_range (391)
60 #elif defined(__aarch64__)
61 #define __NR_copy_file_range (285)
62 #elif defined(__powerpc__)
63 #define __NR_copy_file_range (379)
64 #else
65 #error "no definition of __NR_copy_file_range for this platform"
66 #endif
67 #endif /* __NR_copy_file_range */
68
69 #if defined(_GNU_SOURCE)
70 _Static_assert(sizeof (loff_t) == sizeof (off_t),
71 "loff_t and off_t must be the same size");
72 #endif
73
74 ssize_t
75 copy_file_range(int, off_t *, int, off_t *, size_t, unsigned int)
76 __attribute__((weak));
77
78 static inline ssize_t
cf_copy_file_range(int sfd,off_t * soff,int dfd,off_t * doff,size_t len,unsigned int flags)79 cf_copy_file_range(int sfd, off_t *soff, int dfd, off_t *doff,
80 size_t len, unsigned int flags)
81 {
82 if (copy_file_range)
83 return (copy_file_range(sfd, soff, dfd, doff, len, flags));
84 return (
85 syscall(__NR_copy_file_range, sfd, soff, dfd, doff, len, flags));
86 }
87
88 #else
89 /* Other platforms, let the linker sort it out. */
90 static inline ssize_t
cf_copy_file_range(int sfd,off_t * soff,int dfd,off_t * doff,size_t len,unsigned int flags)91 cf_copy_file_range(int sfd, off_t *soff, int dfd, off_t *doff,
92 size_t len, unsigned int flags)
93 {
94 return (copy_file_range(sfd, soff, dfd, doff, len, flags));
95 }
96 #endif
97
98 /*
99 * Like above, not all libcs define the types we need for the ioctl-based
100 * clone calls, so define any that are missing directly.
101 */
102
103 /* Define missing FICLONE */
104 #ifdef FICLONE
105 #define CF_FICLONE FICLONE
106 #else
107 #define CF_FICLONE _IOW(0x94, 9, int)
108 #endif
109
110 /* Define missing FICLONERANGE and support structs */
111 #ifdef FICLONERANGE
112 #define CF_FICLONERANGE FICLONERANGE
113 typedef struct file_clone_range cf_file_clone_range_t;
114 #else
115 typedef struct {
116 int64_t src_fd;
117 uint64_t src_offset;
118 uint64_t src_length;
119 uint64_t dest_offset;
120 } cf_file_clone_range_t;
121 #define CF_FICLONERANGE _IOW(0x94, 13, cf_file_clone_range_t)
122 #endif
123
124 /* Define missing FIDEDUPERANGE and support structs */
125 #ifdef FIDEDUPERANGE
126 #define CF_FIDEDUPERANGE FIDEDUPERANGE
127 #define CF_FILE_DEDUPE_RANGE_SAME FILE_DEDUPE_RANGE_SAME
128 #define CF_FILE_DEDUPE_RANGE_DIFFERS FILE_DEDUPE_RANGE_DIFFERS
129 typedef struct file_dedupe_range_info cf_file_dedupe_range_info_t;
130 typedef struct file_dedupe_range cf_file_dedupe_range_t;
131 #else
132 typedef struct {
133 int64_t dest_fd;
134 uint64_t dest_offset;
135 uint64_t bytes_deduped;
136 int32_t status;
137 uint32_t reserved;
138 } cf_file_dedupe_range_info_t;
139 typedef struct {
140 uint64_t src_offset;
141 uint64_t src_length;
142 uint16_t dest_count;
143 uint16_t reserved1;
144 uint32_t reserved2;
145 cf_file_dedupe_range_info_t info[0];
146 } cf_file_dedupe_range_t;
147 #define CF_FIDEDUPERANGE _IOWR(0x94, 54, cf_file_dedupe_range_t)
148 #define CF_FILE_DEDUPE_RANGE_SAME (0)
149 #define CF_FILE_DEDUPE_RANGE_DIFFERS (1)
150
151 /* Define missing COPY_FILE_RANGE_CLONE flag. */
152 #ifdef COPY_FILE_RANGE_CLONE
153 #define CF_COPY_FILE_RANGE_CLONE COPY_FILE_RANGE_CLONE
154 #else
155 #define CF_COPY_FILE_RANGE_CLONE (0x00800000)
156 #endif
157
158 #endif
159
160 typedef enum {
161 CF_MODE_NONE,
162 CF_MODE_CLONE,
163 CF_MODE_CLONERANGE,
164 CF_MODE_COPYFILERANGE,
165 CF_MODE_COPYFILERANGE_CLONE,
166 CF_MODE_DEDUPERANGE,
167 } cf_mode_t;
168
169 static int
usage(void)170 usage(void)
171 {
172 printf(
173 "usage:\n"
174 " FICLONE:\n"
175 " clonefile -c <src> <dst>\n"
176 " FICLONERANGE:\n"
177 " clonefile -r <src> <dst> <soff> <doff> <len>\n"
178 " copy_file_range:\n"
179 " clonefile -f <src> <dst> [<soff> <doff> <len | \"all\">]\n"
180 " copy_file_range(CLONE):\n"
181 " clonefile -F <src> <dst> [<soff> <doff> <len | \"all\">]\n"
182 " FIDEDUPERANGE:\n"
183 " clonefile -d <src> <dst> <soff> <doff> <len>\n");
184 return (1);
185 }
186
187 int do_clone(int sfd, int dfd);
188 int do_clonerange(int sfd, int dfd, off_t soff, off_t doff, size_t len);
189 int do_copyfilerange(int sfd, int dfd, off_t soff, off_t doff, size_t len);
190 int do_copyfilerange_clone(int sfd, int dfd, off_t soff, off_t doff,
191 size_t len);
192 int do_deduperange(int sfd, int dfd, off_t soff, off_t doff, size_t len);
193
194 int quiet = 0;
195
196 int
main(int argc,char ** argv)197 main(int argc, char **argv)
198 {
199 cf_mode_t mode = CF_MODE_NONE;
200
201 int c;
202 while ((c = getopt(argc, argv, "crfFdq")) != -1) {
203 switch (c) {
204 case 'c':
205 mode = CF_MODE_CLONE;
206 break;
207 case 'r':
208 mode = CF_MODE_CLONERANGE;
209 break;
210 case 'f':
211 mode = CF_MODE_COPYFILERANGE;
212 break;
213 case 'F':
214 mode = CF_MODE_COPYFILERANGE_CLONE;
215 break;
216 case 'd':
217 mode = CF_MODE_DEDUPERANGE;
218 break;
219 case 'q':
220 quiet = 1;
221 break;
222 }
223 }
224
225 switch (mode) {
226 case CF_MODE_NONE:
227 return (usage());
228 case CF_MODE_CLONE:
229 if ((argc-optind) != 2)
230 return (usage());
231 break;
232 case CF_MODE_CLONERANGE:
233 case CF_MODE_DEDUPERANGE:
234 if ((argc-optind) != 5)
235 return (usage());
236 break;
237 case CF_MODE_COPYFILERANGE:
238 case CF_MODE_COPYFILERANGE_CLONE:
239 if ((argc-optind) != 2 && (argc-optind) != 5)
240 return (usage());
241 break;
242 default:
243 abort();
244 }
245
246 off_t soff = 0, doff = 0;
247 size_t len = SSIZE_MAX;
248 unsigned long long len2;
249 if ((argc-optind) == 5) {
250 soff = strtoull(argv[optind+2], NULL, 10);
251 if (soff == ULLONG_MAX) {
252 fprintf(stderr, "invalid source offset");
253 return (1);
254 }
255 doff = strtoull(argv[optind+3], NULL, 10);
256 if (doff == ULLONG_MAX) {
257 fprintf(stderr, "invalid dest offset");
258 return (1);
259 }
260 if (mode == CF_MODE_COPYFILERANGE &&
261 strcmp(argv[optind+4], "all") == 0) {
262 len = SSIZE_MAX;
263 } else {
264 len2 = strtoull(argv[optind+4], NULL, 10);
265 if (len2 == ULLONG_MAX) {
266 fprintf(stderr, "invalid length");
267 return (1);
268 }
269 if (len2 < SSIZE_MAX)
270 len = (size_t)len2;
271 }
272 }
273
274 int sfd = open(argv[optind], O_RDONLY);
275 if (sfd < 0) {
276 fprintf(stderr, "open: %s: %s\n",
277 argv[optind], strerror(errno));
278 return (1);
279 }
280
281 int dfd = open(argv[optind+1], O_WRONLY|O_CREAT,
282 S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
283 if (dfd < 0) {
284 fprintf(stderr, "open: %s: %s\n",
285 argv[optind+1], strerror(errno));
286 close(sfd);
287 return (1);
288 }
289
290 int err;
291 switch (mode) {
292 case CF_MODE_CLONE:
293 err = do_clone(sfd, dfd);
294 break;
295 case CF_MODE_CLONERANGE:
296 err = do_clonerange(sfd, dfd, soff, doff, len);
297 break;
298 case CF_MODE_COPYFILERANGE:
299 err = do_copyfilerange(sfd, dfd, soff, doff, len);
300 break;
301 case CF_MODE_COPYFILERANGE_CLONE:
302 err = do_copyfilerange_clone(sfd, dfd, soff, doff, len);
303 break;
304 case CF_MODE_DEDUPERANGE:
305 err = do_deduperange(sfd, dfd, soff, doff, len);
306 break;
307 default:
308 abort();
309 }
310
311 if (!quiet) {
312 off_t spos = lseek(sfd, 0, SEEK_CUR);
313 off_t slen = lseek(sfd, 0, SEEK_END);
314 off_t dpos = lseek(dfd, 0, SEEK_CUR);
315 off_t dlen = lseek(dfd, 0, SEEK_END);
316
317 fprintf(stderr, "file offsets: src=%jd/%jd; dst=%jd/%jd\n",
318 spos, slen, dpos, dlen);
319 }
320
321 close(dfd);
322 close(sfd);
323
324 return (err == 0 ? 0 : 1);
325 }
326
327 int
do_clone(int sfd,int dfd)328 do_clone(int sfd, int dfd)
329 {
330 if (!quiet)
331 fprintf(stderr, "using FICLONE\n");
332 int err = ioctl(dfd, CF_FICLONE, sfd);
333 if (err < 0) {
334 fprintf(stderr, "ioctl(FICLONE): %s\n", strerror(errno));
335 return (err);
336 }
337 return (0);
338 }
339
340 int
do_clonerange(int sfd,int dfd,off_t soff,off_t doff,size_t len)341 do_clonerange(int sfd, int dfd, off_t soff, off_t doff, size_t len)
342 {
343 if (!quiet)
344 fprintf(stderr, "using FICLONERANGE\n");
345 cf_file_clone_range_t fcr = {
346 .src_fd = sfd,
347 .src_offset = soff,
348 .src_length = len,
349 .dest_offset = doff,
350 };
351 int err = ioctl(dfd, CF_FICLONERANGE, &fcr);
352 if (err < 0) {
353 fprintf(stderr, "ioctl(FICLONERANGE): %s\n", strerror(errno));
354 return (err);
355 }
356 return (0);
357 }
358
359 static int
_do_copyfilerange_impl(int sfd,int dfd,off_t soff,off_t doff,size_t len,unsigned int flags,const char * name)360 _do_copyfilerange_impl(int sfd, int dfd, off_t soff, off_t doff, size_t len,
361 unsigned int flags, const char *name)
362 {
363 if (!quiet)
364 fprintf(stderr, "using %s\n", name);
365 ssize_t copied = cf_copy_file_range(sfd, &soff, dfd, &doff, len, flags);
366 if (copied < 0) {
367 fprintf(stderr, "%s: %s\n", name, strerror(errno));
368 return (1);
369 }
370 if (len == SSIZE_MAX) {
371 struct stat sb;
372
373 if (fstat(sfd, &sb) < 0) {
374 fprintf(stderr, "fstat(sfd): %s\n", strerror(errno));
375 return (1);
376 }
377 len = sb.st_size;
378 }
379 if (copied != len) {
380 fprintf(stderr, "%s: copied less than requested: "
381 "requested=%zu; copied=%zd\n", name, len, copied);
382 return (1);
383 }
384 return (0);
385 }
386
387 int
do_copyfilerange(int sfd,int dfd,off_t soff,off_t doff,size_t len)388 do_copyfilerange(int sfd, int dfd, off_t soff, off_t doff, size_t len)
389 {
390 return (_do_copyfilerange_impl(sfd, dfd, soff, doff, len, 0,
391 "copy_file_range"));
392 }
393
394 int
do_copyfilerange_clone(int sfd,int dfd,off_t soff,off_t doff,size_t len)395 do_copyfilerange_clone(int sfd, int dfd, off_t soff, off_t doff, size_t len)
396 {
397 return (_do_copyfilerange_impl(sfd, dfd, soff, doff, len,
398 CF_COPY_FILE_RANGE_CLONE, "copy_file_range(CLONE)"));
399 }
400
401 int
do_deduperange(int sfd,int dfd,off_t soff,off_t doff,size_t len)402 do_deduperange(int sfd, int dfd, off_t soff, off_t doff, size_t len)
403 {
404 if (!quiet)
405 fprintf(stderr, "using FIDEDUPERANGE\n");
406
407 char buf[sizeof (cf_file_dedupe_range_t)+
408 sizeof (cf_file_dedupe_range_info_t)] = {0};
409 cf_file_dedupe_range_t *fdr = (cf_file_dedupe_range_t *)&buf[0];
410 cf_file_dedupe_range_info_t *fdri =
411 (cf_file_dedupe_range_info_t *)
412 &buf[sizeof (cf_file_dedupe_range_t)];
413
414 fdr->src_offset = soff;
415 fdr->src_length = len;
416 fdr->dest_count = 1;
417
418 fdri->dest_fd = dfd;
419 fdri->dest_offset = doff;
420
421 int err = ioctl(sfd, CF_FIDEDUPERANGE, fdr);
422 if (err != 0)
423 fprintf(stderr, "ioctl(FIDEDUPERANGE): %s\n", strerror(errno));
424
425 if (fdri->status < 0) {
426 fprintf(stderr, "dedup failed: %s\n", strerror(-fdri->status));
427 err = -1;
428 } else if (fdri->status == CF_FILE_DEDUPE_RANGE_DIFFERS) {
429 fprintf(stderr, "dedup failed: range differs\n");
430 err = -1;
431 }
432
433 return (err);
434 }
435