xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/cmd/mmap_seek.c (revision b670c9bafc0e31c7609969bf374b2e80bdc00211)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright (c) 2021 by Lawrence Livermore National Security, LLC.
25  */
26 
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/mman.h>
33 #include <sys/sysmacros.h>
34 #include <errno.h>
35 #ifdef __linux__
36 #include <linux/fs.h>
37 #endif
38 
39 /* some older uClibc's lack the defines, so we'll manually define them */
40 #ifdef	__UCLIBC__
41 #ifndef	SEEK_DATA
42 #define	SEEK_DATA 3
43 #endif
44 #ifndef	SEEK_HOLE
45 #define	SEEK_HOLE 4
46 #endif
47 #endif
48 
49 static void
50 seek_data(int fd, off_t offset, off_t expected)
51 {
52 	off_t data_offset = lseek(fd, offset, SEEK_DATA);
53 	if (data_offset != expected) {
54 		fprintf(stderr, "lseek(fd, %d, SEEK_DATA) = %d (expected %d)\n",
55 		    (int)offset, (int)data_offset, (int)expected);
56 		exit(2);
57 	}
58 }
59 
60 static void
61 seek_hole(int fd, off_t offset, off_t expected)
62 {
63 	off_t hole_offset = lseek(fd, offset, SEEK_HOLE);
64 	if (hole_offset != expected) {
65 		fprintf(stderr, "lseek(fd, %d, SEEK_HOLE) = %d (expected %d)\n",
66 		    (int)offset, (int)hole_offset, (int)expected);
67 		exit(2);
68 	}
69 }
70 
71 int
72 main(int argc, char **argv)
73 {
74 	char *execname = argv[0];
75 	char *file_path = argv[1];
76 	char *buf = NULL;
77 	int err;
78 
79 	if (argc != 4) {
80 		(void) printf("usage: %s <file name> <file size> "
81 		    "<block size>\n", argv[0]);
82 		exit(1);
83 	}
84 
85 	int fd = open(file_path, O_RDWR | O_CREAT, 0666);
86 	if (fd == -1) {
87 		(void) fprintf(stderr, "%s: %s: ", execname, file_path);
88 		perror("open");
89 		exit(2);
90 	}
91 
92 	off_t file_size = atoi(argv[2]);
93 	off_t block_size = atoi(argv[3]);
94 
95 	if (block_size * 2 > file_size) {
96 		(void) fprintf(stderr, "file size must be at least "
97 		    "double the block size\n");
98 		exit(2);
99 	}
100 
101 	err = ftruncate(fd, file_size);
102 	if (err == -1) {
103 		perror("ftruncate");
104 		exit(2);
105 	}
106 
107 	if ((buf = mmap(NULL, file_size, PROT_READ | PROT_WRITE,
108 	    MAP_SHARED, fd, 0)) == MAP_FAILED) {
109 		perror("mmap");
110 		exit(2);
111 	}
112 
113 	/* Verify the file is sparse and reports no data. */
114 	seek_data(fd, 0, -1);
115 
116 	/* Verify the file is reported as a hole. */
117 	seek_hole(fd, 0, 0);
118 
119 	/* Verify search beyond end of file is an error. */
120 	seek_data(fd, 2 * file_size, -1);
121 	seek_hole(fd, 2 * file_size, -1);
122 
123 	/* Dirty the first byte. */
124 	memset(buf, 'a', 1);
125 	seek_data(fd, 0, 0);
126 	seek_data(fd, block_size, -1);
127 	seek_hole(fd, 0, block_size);
128 	seek_hole(fd, block_size, block_size);
129 
130 	/* Dirty the first half of the file. */
131 	memset(buf, 'b', file_size / 2);
132 	seek_data(fd, 0, 0);
133 	seek_data(fd, block_size, block_size);
134 	seek_hole(fd, 0, P2ROUNDUP(file_size / 2, block_size));
135 	seek_hole(fd, block_size, P2ROUNDUP(file_size / 2, block_size));
136 
137 	/* Dirty the whole file. */
138 	memset(buf, 'c', file_size);
139 	seek_data(fd, 0, 0);
140 	seek_data(fd, file_size * 3 / 4,
141 	    P2ROUNDUP(file_size * 3 / 4, block_size));
142 	seek_hole(fd, 0, file_size);
143 	seek_hole(fd, file_size / 2, file_size);
144 
145 	/* Punch a hole (required compression be enabled). */
146 	memset(buf + block_size, 0, block_size);
147 	seek_data(fd, 0, 0);
148 	seek_data(fd, block_size, 2 * block_size);
149 	seek_hole(fd, 0, block_size);
150 	seek_hole(fd, block_size, block_size);
151 	seek_hole(fd, 2 * block_size, file_size);
152 
153 	err = munmap(buf, file_size);
154 	if (err == -1) {
155 		perror("munmap");
156 		exit(2);
157 	}
158 
159 	close(fd);
160 
161 	return (0);
162 }
163