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]
23 * Copyright (c) 2021 by Lawrence Livermore National Security, LLC.
32 #include <sys/sysmacros.h>
38 /* some older uClibc's lack the defines, so we'll manually define them */
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
);
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
);
71 main(int argc
, char **argv
)
73 char *execname
= argv
[0];
74 char *file_path
= argv
[1];
79 (void) printf("usage: %s <file name> <file size> "
80 "<block size>\n", argv
[0]);
84 int fd
= open(file_path
, O_RDWR
| O_CREAT
, 0666);
86 (void) fprintf(stderr
, "%s: %s: ", execname
, file_path
);
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");
100 err
= ftruncate(fd
, file_size
);
106 if ((buf
= mmap(NULL
, file_size
, PROT_READ
| PROT_WRITE
,
107 MAP_SHARED
, fd
, 0)) == MAP_FAILED
) {
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. */
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. */
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);
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
);
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
);
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
);