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_expect(int fd, off_t offset, int whence, off_t expect_offset) 51 { 52 errno = 0; 53 off_t seek_offset = lseek(fd, offset, whence); 54 if (seek_offset == expect_offset) 55 return; 56 57 int err = errno; 58 fprintf(stderr, "lseek(fd, %ld, SEEK_%s) = %ld (expected %ld)", 59 offset, (whence == SEEK_DATA ? "DATA" : "HOLE"), 60 seek_offset, expect_offset); 61 if (err != 0) 62 fprintf(stderr, " (errno %d [%s])\n", err, strerror(err)); 63 else 64 fputc('\n', stderr); 65 exit(2); 66 } 67 68 static inline void 69 seek_data(int fd, off_t offset, off_t expected) 70 { 71 seek_expect(fd, offset, SEEK_DATA, expected); 72 } 73 74 static inline void 75 seek_hole(int fd, off_t offset, off_t expected) 76 { 77 seek_expect(fd, offset, SEEK_HOLE, expected); 78 } 79 80 int 81 main(int argc, char **argv) 82 { 83 char *execname = argv[0]; 84 char *file_path = argv[1]; 85 char *buf = NULL; 86 int err; 87 88 if (argc != 4) { 89 (void) printf("usage: %s <file name> <file size> " 90 "<block size>\n", argv[0]); 91 exit(1); 92 } 93 94 int fd = open(file_path, O_RDWR | O_CREAT, 0666); 95 if (fd == -1) { 96 (void) fprintf(stderr, "%s: %s: ", execname, file_path); 97 perror("open"); 98 exit(2); 99 } 100 101 off_t file_size = atoi(argv[2]); 102 off_t block_size = atoi(argv[3]); 103 104 if (block_size * 2 > file_size) { 105 (void) fprintf(stderr, "file size must be at least " 106 "double the block size\n"); 107 exit(2); 108 } 109 110 err = ftruncate(fd, file_size); 111 if (err == -1) { 112 perror("ftruncate"); 113 exit(2); 114 } 115 116 if ((buf = mmap(NULL, file_size, PROT_READ | PROT_WRITE, 117 MAP_SHARED, fd, 0)) == MAP_FAILED) { 118 perror("mmap"); 119 exit(2); 120 } 121 122 /* Verify the file is sparse and reports no data. */ 123 seek_data(fd, 0, -1); 124 125 /* Verify the file is reported as a hole. */ 126 seek_hole(fd, 0, 0); 127 128 /* Verify search beyond end of file is an error. */ 129 seek_data(fd, 2 * file_size, -1); 130 seek_hole(fd, 2 * file_size, -1); 131 132 /* Dirty the first byte. */ 133 memset(buf, 'a', 1); 134 seek_data(fd, 0, 0); 135 seek_data(fd, block_size, -1); 136 seek_hole(fd, 0, block_size); 137 seek_hole(fd, block_size, block_size); 138 139 /* Dirty the first half of the file. */ 140 memset(buf, 'b', file_size / 2); 141 seek_data(fd, 0, 0); 142 seek_data(fd, block_size, block_size); 143 seek_hole(fd, 0, P2ROUNDUP(file_size / 2, block_size)); 144 seek_hole(fd, block_size, P2ROUNDUP(file_size / 2, block_size)); 145 146 /* Dirty the whole file. */ 147 memset(buf, 'c', file_size); 148 seek_data(fd, 0, 0); 149 seek_data(fd, file_size * 3 / 4, 150 P2ROUNDUP(file_size * 3 / 4, block_size)); 151 seek_hole(fd, 0, file_size); 152 seek_hole(fd, file_size / 2, file_size); 153 154 /* Punch a hole (required compression be enabled). */ 155 memset(buf + block_size, 0, block_size); 156 seek_data(fd, 0, 0); 157 seek_data(fd, block_size, 2 * block_size); 158 seek_hole(fd, 0, block_size); 159 seek_hole(fd, block_size, block_size); 160 seek_hole(fd, 2 * block_size, file_size); 161 162 err = munmap(buf, file_size); 163 if (err == -1) { 164 perror("munmap"); 165 exit(2); 166 } 167 168 close(fd); 169 170 return (0); 171 } 172