xref: /freebsd/sys/contrib/openzfs/tests/zfs-tests/cmd/mmap_seek.c (revision 2e3507c25e42292b45a5482e116d278f5515d04d)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2021 by Lawrence Livermore National Security, LLC.
24  */
25 
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
37 
38 static void
39 seek_data(int fd, off_t offset, off_t expected)
40 {
41 	off_t data_offset = lseek(fd, offset, SEEK_DATA);
42 	if (data_offset != expected) {
43 		fprintf(stderr, "lseek(fd, %d, SEEK_DATA) = %d (expected %d)\n",
44 		    (int)offset, (int)data_offset, (int)expected);
45 		exit(2);
46 	}
47 }
48 
49 static void
50 seek_hole(int fd, off_t offset, off_t expected)
51 {
52 	off_t hole_offset = lseek(fd, offset, SEEK_HOLE);
53 	if (hole_offset != expected) {
54 		fprintf(stderr, "lseek(fd, %d, SEEK_HOLE) = %d (expected %d)\n",
55 		    (int)offset, (int)hole_offset, (int)expected);
56 		exit(2);
57 	}
58 }
59 
60 int
61 main(int argc, char **argv)
62 {
63 	char *execname = argv[0];
64 	char *file_path = argv[1];
65 	char *buf = NULL;
66 	int err;
67 
68 	if (argc != 4) {
69 		(void) printf("usage: %s <file name> <file size> "
70 		    "<block size>\n", argv[0]);
71 		exit(1);
72 	}
73 
74 	int fd = open(file_path, O_RDWR | O_CREAT, 0666);
75 	if (fd == -1) {
76 		(void) fprintf(stderr, "%s: %s: ", execname, file_path);
77 		perror("open");
78 		exit(2);
79 	}
80 
81 	off_t file_size = atoi(argv[2]);
82 	off_t block_size = atoi(argv[3]);
83 
84 	if (block_size * 2 > file_size) {
85 		(void) fprintf(stderr, "file size must be at least "
86 		    "double the block size\n");
87 		exit(2);
88 	}
89 
90 	err = ftruncate(fd, file_size);
91 	if (err == -1) {
92 		perror("ftruncate");
93 		exit(2);
94 	}
95 
96 	if ((buf = mmap(NULL, file_size, PROT_READ | PROT_WRITE,
97 	    MAP_SHARED, fd, 0)) == MAP_FAILED) {
98 		perror("mmap");
99 		exit(2);
100 	}
101 
102 	/* Verify the file is sparse and reports no data. */
103 	seek_data(fd, 0, -1);
104 
105 	/* Verify the file is reported as a hole. */
106 	seek_hole(fd, 0, 0);
107 
108 	/* Verify search beyond end of file is an error. */
109 	seek_data(fd, 2 * file_size, -1);
110 	seek_hole(fd, 2 * file_size, -1);
111 
112 	/* Dirty the first byte. */
113 	memset(buf, 'a', 1);
114 	seek_data(fd, 0, 0);
115 	seek_data(fd, block_size, -1);
116 	seek_hole(fd, 0, block_size);
117 	seek_hole(fd, block_size, block_size);
118 
119 	/* Dirty the first half of the file. */
120 	memset(buf, 'b', file_size / 2);
121 	seek_data(fd, 0, 0);
122 	seek_data(fd, block_size, block_size);
123 	seek_hole(fd, 0, P2ROUNDUP(file_size / 2, block_size));
124 	seek_hole(fd, block_size, P2ROUNDUP(file_size / 2, block_size));
125 
126 	/* Dirty the whole file. */
127 	memset(buf, 'c', file_size);
128 	seek_data(fd, 0, 0);
129 	seek_data(fd, file_size * 3 / 4,
130 	    P2ROUNDUP(file_size * 3 / 4, block_size));
131 	seek_hole(fd, 0, file_size);
132 	seek_hole(fd, file_size / 2, file_size);
133 
134 	/* Punch a hole (required compression be enabled). */
135 	memset(buf + block_size, 0, block_size);
136 	seek_data(fd, 0, 0);
137 	seek_data(fd, block_size, 2 * block_size);
138 	seek_hole(fd, 0, block_size);
139 	seek_hole(fd, block_size, block_size);
140 	seek_hole(fd, 2 * block_size, file_size);
141 
142 	err = munmap(buf, file_size);
143 	if (err == -1) {
144 		perror("munmap");
145 		exit(2);
146 	}
147 
148 	close(fd);
149 
150 	return (0);
151 }
152