Linux: Fix detection of register_sysctl_sz
[zfs.git] / tests / zfs-tests / cmd / mmap_seek.c
blob2d250554a13f5d824230a3f50ecd2e39b7574ddf
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) 2021 by Lawrence Livermore National Security, LLC.
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/mman.h>
32 #include <sys/sysmacros.h>
33 #include <errno.h>
34 #ifdef __linux__
35 #include <linux/fs.h>
36 #endif
38 /* some older uClibc's lack the defines, so we'll manually define them */
39 #ifdef __UCLIBC__
40 #ifndef SEEK_DATA
41 #define SEEK_DATA 3
42 #endif
43 #ifndef SEEK_HOLE
44 #define SEEK_HOLE 4
45 #endif
46 #endif
48 static void
49 seek_data(int fd, off_t offset, off_t expected)
51 off_t data_offset = lseek(fd, offset, SEEK_DATA);
52 if (data_offset != expected) {
53 fprintf(stderr, "lseek(fd, %d, SEEK_DATA) = %d (expected %d)\n",
54 (int)offset, (int)data_offset, (int)expected);
55 exit(2);
59 static void
60 seek_hole(int fd, off_t offset, off_t expected)
62 off_t hole_offset = lseek(fd, offset, SEEK_HOLE);
63 if (hole_offset != expected) {
64 fprintf(stderr, "lseek(fd, %d, SEEK_HOLE) = %d (expected %d)\n",
65 (int)offset, (int)hole_offset, (int)expected);
66 exit(2);
70 int
71 main(int argc, char **argv)
73 char *execname = argv[0];
74 char *file_path = argv[1];
75 char *buf = NULL;
76 int err;
78 if (argc != 4) {
79 (void) printf("usage: %s <file name> <file size> "
80 "<block size>\n", argv[0]);
81 exit(1);
84 int fd = open(file_path, O_RDWR | O_CREAT, 0666);
85 if (fd == -1) {
86 (void) fprintf(stderr, "%s: %s: ", execname, file_path);
87 perror("open");
88 exit(2);
91 off_t file_size = atoi(argv[2]);
92 off_t block_size = atoi(argv[3]);
94 if (block_size * 2 > file_size) {
95 (void) fprintf(stderr, "file size must be at least "
96 "double the block size\n");
97 exit(2);
100 err = ftruncate(fd, file_size);
101 if (err == -1) {
102 perror("ftruncate");
103 exit(2);
106 if ((buf = mmap(NULL, file_size, PROT_READ | PROT_WRITE,
107 MAP_SHARED, fd, 0)) == MAP_FAILED) {
108 perror("mmap");
109 exit(2);
112 /* Verify the file is sparse and reports no data. */
113 seek_data(fd, 0, -1);
115 /* Verify the file is reported as a hole. */
116 seek_hole(fd, 0, 0);
118 /* Verify search beyond end of file is an error. */
119 seek_data(fd, 2 * file_size, -1);
120 seek_hole(fd, 2 * file_size, -1);
122 /* Dirty the first byte. */
123 memset(buf, 'a', 1);
124 seek_data(fd, 0, 0);
125 seek_data(fd, block_size, -1);
126 seek_hole(fd, 0, block_size);
127 seek_hole(fd, block_size, block_size);
129 /* Dirty the first half of the file. */
130 memset(buf, 'b', file_size / 2);
131 seek_data(fd, 0, 0);
132 seek_data(fd, block_size, block_size);
133 seek_hole(fd, 0, P2ROUNDUP(file_size / 2, block_size));
134 seek_hole(fd, block_size, P2ROUNDUP(file_size / 2, block_size));
136 /* Dirty the whole file. */
137 memset(buf, 'c', file_size);
138 seek_data(fd, 0, 0);
139 seek_data(fd, file_size * 3 / 4,
140 P2ROUNDUP(file_size * 3 / 4, block_size));
141 seek_hole(fd, 0, file_size);
142 seek_hole(fd, file_size / 2, file_size);
144 /* Punch a hole (required compression be enabled). */
145 memset(buf + block_size, 0, block_size);
146 seek_data(fd, 0, 0);
147 seek_data(fd, block_size, 2 * block_size);
148 seek_hole(fd, 0, block_size);
149 seek_hole(fd, block_size, block_size);
150 seek_hole(fd, 2 * block_size, file_size);
152 err = munmap(buf, file_size);
153 if (err == -1) {
154 perror("munmap");
155 exit(2);
158 close(fd);
160 return (0);