Update ABD stats for linear page Linux
[zfs.git] / tests / zfs-tests / cmd / clone_mmap_cached.c
blobc1cdf796cfb4c32f6446852496994901bee1975b
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright (c) 2024 by Pawel Jakub Dawidek
26 #include <sys/mman.h>
27 #include <sys/stat.h>
29 #include <assert.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <stdbool.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #ifdef __FreeBSD__
39 #define loff_t off_t
40 #endif
42 ssize_t
43 copy_file_range(int, loff_t *, int, loff_t *, size_t, unsigned int)
44 __attribute__((weak));
46 static void *
47 mmap_file(int fd, size_t size)
49 void *p;
51 p = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
52 if (p == MAP_FAILED) {
53 (void) fprintf(stderr, "mmap failed: %s\n", strerror(errno));
54 exit(2);
57 return (p);
60 static void
61 usage(const char *progname)
65 * -i cache input before copy_file_range(2).
66 * -o cache input before copy_file_range(2).
68 (void) fprintf(stderr, "usage: %s [-io] <input> <output>\n", progname);
69 exit(3);
72 int
73 main(int argc, char *argv[])
75 int dfd, sfd;
76 size_t dsize, ssize;
77 void *dmem, *smem, *ptr;
78 off_t doff, soff;
79 struct stat sb;
80 bool cache_input, cache_output;
81 const char *progname;
82 int c;
84 progname = argv[0];
85 cache_input = cache_output = false;
87 while ((c = getopt(argc, argv, "io")) != -1) {
88 switch (c) {
89 case 'i':
90 cache_input = true;
91 break;
92 case 'o':
93 cache_output = true;
94 break;
95 default:
96 usage(progname);
99 argc -= optind;
100 argv += optind;
102 if (argc != 2) {
103 usage(progname);
106 sfd = open(argv[0], O_RDONLY);
107 if (fstat(sfd, &sb) == -1) {
108 (void) fprintf(stderr, "fstat failed: %s\n", strerror(errno));
109 exit(2);
111 ssize = sb.st_size;
112 smem = mmap_file(sfd, ssize);
114 dfd = open(argv[1], O_RDWR);
115 if (fstat(dfd, &sb) == -1) {
116 (void) fprintf(stderr, "fstat failed: %s\n", strerror(errno));
117 exit(2);
119 dsize = sb.st_size;
120 dmem = mmap_file(dfd, dsize);
123 * Hopefully it won't be compiled out.
125 if (cache_input) {
126 ptr = malloc(ssize);
127 assert(ptr != NULL);
128 memcpy(ptr, smem, ssize);
129 free(ptr);
131 if (cache_output) {
132 ptr = malloc(ssize);
133 assert(ptr != NULL);
134 memcpy(ptr, dmem, dsize);
135 free(ptr);
138 soff = doff = 0;
139 if (copy_file_range(sfd, &soff, dfd, &doff, ssize, 0) < 0) {
140 (void) fprintf(stderr, "copy_file_range failed: %s\n",
141 strerror(errno));
142 exit(2);
145 exit(memcmp(smem, dmem, ssize) == 0 ? 0 : 1);