File: clonefile.c

package info (click to toggle)
zfs-linux 2.3.4~git20250812.3b64a96-1
  • links: PTS, VCS
  • area: contrib
  • in suites: experimental
  • size: 70,688 kB
  • sloc: ansic: 393,668; sh: 68,068; asm: 47,734; python: 8,160; makefile: 5,125; perl: 859; sed: 41
file content (376 lines) | stat: -rw-r--r-- 9,524 bytes parent folder | download | duplicates (4)
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * SPDX-License-Identifier: MIT
 *
 * Copyright (c) 2023, Rob Norris <robn@despairlabs.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

/*
 * This program is to test the availability and behaviour of copy_file_range,
 * FICLONE, FICLONERANGE and FIDEDUPERANGE in the Linux kernel. It should
 * compile and run even if these features aren't exposed through the libc.
 */

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#ifndef __NR_copy_file_range
#if defined(__x86_64__)
#define	__NR_copy_file_range (326)
#elif defined(__i386__)
#define	__NR_copy_file_range (377)
#elif defined(__s390__)
#define	__NR_copy_file_range (375)
#elif defined(__arm__)
#define	__NR_copy_file_range (391)
#elif defined(__aarch64__)
#define	__NR_copy_file_range (285)
#elif defined(__powerpc__)
#define	__NR_copy_file_range (379)
#else
#error "no definition of __NR_copy_file_range for this platform"
#endif
#endif /* __NR_copy_file_range */

#ifdef __FreeBSD__
#define	loff_t	off_t
#endif

ssize_t
copy_file_range(int, loff_t *, int, loff_t *, size_t, unsigned int)
    __attribute__((weak));

static inline ssize_t
cf_copy_file_range(int sfd, loff_t *soff, int dfd, loff_t *doff,
    size_t len, unsigned int flags)
{
	if (copy_file_range)
		return (copy_file_range(sfd, soff, dfd, doff, len, flags));
	return (
	    syscall(__NR_copy_file_range, sfd, soff, dfd, doff, len, flags));
}

/* Define missing FICLONE */
#ifdef FICLONE
#define	CF_FICLONE	FICLONE
#else
#define	CF_FICLONE	_IOW(0x94, 9, int)
#endif

/* Define missing FICLONERANGE and support structs */
#ifdef FICLONERANGE
#define	CF_FICLONERANGE	FICLONERANGE
typedef struct file_clone_range cf_file_clone_range_t;
#else
typedef struct {
	int64_t		src_fd;
	uint64_t	src_offset;
	uint64_t	src_length;
	uint64_t	dest_offset;
} cf_file_clone_range_t;
#define	CF_FICLONERANGE	_IOW(0x94, 13, cf_file_clone_range_t)
#endif

/* Define missing FIDEDUPERANGE and support structs */
#ifdef FIDEDUPERANGE
#define	CF_FIDEDUPERANGE		FIDEDUPERANGE
#define	CF_FILE_DEDUPE_RANGE_SAME	FILE_DEDUPE_RANGE_SAME
#define	CF_FILE_DEDUPE_RANGE_DIFFERS	FILE_DEDUPE_RANGE_DIFFERS
typedef struct file_dedupe_range_info	cf_file_dedupe_range_info_t;
typedef struct file_dedupe_range	cf_file_dedupe_range_t;
#else
typedef struct {
	int64_t dest_fd;
	uint64_t dest_offset;
	uint64_t bytes_deduped;
	int32_t status;
	uint32_t reserved;
} cf_file_dedupe_range_info_t;
typedef struct {
	uint64_t src_offset;
	uint64_t src_length;
	uint16_t dest_count;
	uint16_t reserved1;
	uint32_t reserved2;
	cf_file_dedupe_range_info_t info[0];
} cf_file_dedupe_range_t;
#define	CF_FIDEDUPERANGE		_IOWR(0x94, 54, cf_file_dedupe_range_t)
#define	CF_FILE_DEDUPE_RANGE_SAME	(0)
#define	CF_FILE_DEDUPE_RANGE_DIFFERS	(1)
#endif

typedef enum {
	CF_MODE_NONE,
	CF_MODE_CLONE,
	CF_MODE_CLONERANGE,
	CF_MODE_COPYFILERANGE,
	CF_MODE_DEDUPERANGE,
} cf_mode_t;

static int
usage(void)
{
	printf(
	    "usage:\n"
	    "  FICLONE:\n"
	    "    clonefile -c <src> <dst>\n"
	    "  FICLONERANGE:\n"
	    "    clonefile -r <src> <dst> <soff> <doff> <len>\n"
	    "  copy_file_range:\n"
	    "    clonefile -f <src> <dst> [<soff> <doff> <len | \"all\">]\n"
	    "  FIDEDUPERANGE:\n"
	    "    clonefile -d <src> <dst> <soff> <doff> <len>\n");
	return (1);
}

int do_clone(int sfd, int dfd);
int do_clonerange(int sfd, int dfd, loff_t soff, loff_t doff, size_t len);
int do_copyfilerange(int sfd, int dfd, loff_t soff, loff_t doff, size_t len);
int do_deduperange(int sfd, int dfd, loff_t soff, loff_t doff, size_t len);

int quiet = 0;

int
main(int argc, char **argv)
{
	cf_mode_t mode = CF_MODE_NONE;

	int c;
	while ((c = getopt(argc, argv, "crfdq")) != -1) {
		switch (c) {
			case 'c':
				mode = CF_MODE_CLONE;
				break;
			case 'r':
				mode = CF_MODE_CLONERANGE;
				break;
			case 'f':
				mode = CF_MODE_COPYFILERANGE;
				break;
			case 'd':
				mode = CF_MODE_DEDUPERANGE;
				break;
			case 'q':
				quiet = 1;
				break;
		}
	}

	switch (mode) {
		case CF_MODE_NONE:
			return (usage());
		case CF_MODE_CLONE:
			if ((argc-optind) != 2)
				return (usage());
			break;
		case CF_MODE_CLONERANGE:
		case CF_MODE_DEDUPERANGE:
			if ((argc-optind) != 5)
				return (usage());
			break;
		case CF_MODE_COPYFILERANGE:
			if ((argc-optind) != 2 && (argc-optind) != 5)
				return (usage());
			break;
		default:
			abort();
	}

	loff_t soff = 0, doff = 0;
	size_t len = SSIZE_MAX;
	unsigned long long len2;
	if ((argc-optind) == 5) {
		soff = strtoull(argv[optind+2], NULL, 10);
		if (soff == ULLONG_MAX) {
			fprintf(stderr, "invalid source offset");
			return (1);
		}
		doff = strtoull(argv[optind+3], NULL, 10);
		if (doff == ULLONG_MAX) {
			fprintf(stderr, "invalid dest offset");
			return (1);
		}
		if (mode == CF_MODE_COPYFILERANGE &&
		    strcmp(argv[optind+4], "all") == 0) {
			len = SSIZE_MAX;
		} else {
			len2 = strtoull(argv[optind+4], NULL, 10);
			if (len2 == ULLONG_MAX) {
				fprintf(stderr, "invalid length");
				return (1);
			}
			if (len2 < SSIZE_MAX)
				len = (size_t)len2;
		}
	}

	int sfd = open(argv[optind], O_RDONLY);
	if (sfd < 0) {
		fprintf(stderr, "open: %s: %s\n",
		    argv[optind], strerror(errno));
		return (1);
	}

	int dfd = open(argv[optind+1], O_WRONLY|O_CREAT,
	    S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
	if (dfd < 0) {
		fprintf(stderr, "open: %s: %s\n",
		    argv[optind+1], strerror(errno));
		close(sfd);
		return (1);
	}

	int err;
	switch (mode) {
		case CF_MODE_CLONE:
			err = do_clone(sfd, dfd);
			break;
		case CF_MODE_CLONERANGE:
			err = do_clonerange(sfd, dfd, soff, doff, len);
			break;
		case CF_MODE_COPYFILERANGE:
			err = do_copyfilerange(sfd, dfd, soff, doff, len);
			break;
		case CF_MODE_DEDUPERANGE:
			err = do_deduperange(sfd, dfd, soff, doff, len);
			break;
		default:
			abort();
	}

	if (!quiet) {
		off_t spos = lseek(sfd, 0, SEEK_CUR);
		off_t slen = lseek(sfd, 0, SEEK_END);
		off_t dpos = lseek(dfd, 0, SEEK_CUR);
		off_t dlen = lseek(dfd, 0, SEEK_END);

		fprintf(stderr, "file offsets: src=%jd/%jd; dst=%jd/%jd\n",
		    spos, slen, dpos, dlen);
	}

	close(dfd);
	close(sfd);

	return (err == 0 ? 0 : 1);
}

int
do_clone(int sfd, int dfd)
{
	if (!quiet)
		fprintf(stderr, "using FICLONE\n");
	int err = ioctl(dfd, CF_FICLONE, sfd);
	if (err < 0) {
		fprintf(stderr, "ioctl(FICLONE): %s\n", strerror(errno));
		return (err);
	}
	return (0);
}

int
do_clonerange(int sfd, int dfd, loff_t soff, loff_t doff, size_t len)
{
	if (!quiet)
		fprintf(stderr, "using FICLONERANGE\n");
	cf_file_clone_range_t fcr = {
		.src_fd = sfd,
		.src_offset = soff,
		.src_length = len,
		.dest_offset = doff,
	};
	int err = ioctl(dfd, CF_FICLONERANGE, &fcr);
	if (err < 0) {
		fprintf(stderr, "ioctl(FICLONERANGE): %s\n", strerror(errno));
		return (err);
	}
	return (0);
}

int
do_copyfilerange(int sfd, int dfd, loff_t soff, loff_t doff, size_t len)
{
	if (!quiet)
		fprintf(stderr, "using copy_file_range\n");
	ssize_t copied = cf_copy_file_range(sfd, &soff, dfd, &doff, len, 0);
	if (copied < 0) {
		fprintf(stderr, "copy_file_range: %s\n", strerror(errno));
		return (1);
	}
	if (len == SSIZE_MAX) {
		struct stat sb;

		if (fstat(sfd, &sb) < 0) {
			fprintf(stderr, "fstat(sfd): %s\n", strerror(errno));
			return (1);
		}
		len = sb.st_size;
	}
	if (copied != len) {
		fprintf(stderr, "copy_file_range: copied less than requested: "
		    "requested=%zu; copied=%zd\n", len, copied);
		return (1);
	}
	return (0);
}

int
do_deduperange(int sfd, int dfd, loff_t soff, loff_t doff, size_t len)
{
	if (!quiet)
		fprintf(stderr, "using FIDEDUPERANGE\n");

	char buf[sizeof (cf_file_dedupe_range_t)+
	    sizeof (cf_file_dedupe_range_info_t)] = {0};
	cf_file_dedupe_range_t *fdr = (cf_file_dedupe_range_t *)&buf[0];
	cf_file_dedupe_range_info_t *fdri =
	    (cf_file_dedupe_range_info_t *)
	    &buf[sizeof (cf_file_dedupe_range_t)];

	fdr->src_offset = soff;
	fdr->src_length = len;
	fdr->dest_count = 1;

	fdri->dest_fd = dfd;
	fdri->dest_offset = doff;

	int err = ioctl(sfd, CF_FIDEDUPERANGE, fdr);
	if (err != 0)
		fprintf(stderr, "ioctl(FIDEDUPERANGE): %s\n", strerror(errno));

	if (fdri->status < 0) {
		fprintf(stderr, "dedup failed: %s\n", strerror(-fdri->status));
		err = -1;
	} else if (fdri->status == CF_FILE_DEDUPE_RANGE_DIFFERS) {
		fprintf(stderr, "dedup failed: range differs\n");
		err = -1;
	}

	return (err);
}