diff --git a/tests/zfs-tests/cmd/mmap_seek.c b/tests/zfs-tests/cmd/mmap_seek.c index 7be92d109565..2d250554a13f 100644 --- a/tests/zfs-tests/cmd/mmap_seek.c +++ b/tests/zfs-tests/cmd/mmap_seek.c @@ -1,151 +1,161 @@ /* * CDDL HEADER START * * The contents of this file are subject to the terms of the * Common Development and Distribution License (the "License"). * You may not use this file except in compliance with the License. * * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE * or https://opensource.org/licenses/CDDL-1.0. * See the License for the specific language governing permissions * and limitations under the License. * * When distributing Covered Code, include this CDDL HEADER in each * file and include the License file at usr/src/OPENSOLARIS.LICENSE. * If applicable, add the following below this CDDL HEADER, with the * fields enclosed by brackets "[]" replaced with your own identifying * information: Portions Copyright [yyyy] [name of copyright owner] * * CDDL HEADER END */ /* * Copyright (c) 2021 by Lawrence Livermore National Security, LLC. */ #include #include #include #include #include #include #include #include #ifdef __linux__ #include #endif +/* some older uClibc's lack the defines, so we'll manually define them */ +#ifdef __UCLIBC__ +#ifndef SEEK_DATA +#define SEEK_DATA 3 +#endif +#ifndef SEEK_HOLE +#define SEEK_HOLE 4 +#endif +#endif + static void seek_data(int fd, off_t offset, off_t expected) { off_t data_offset = lseek(fd, offset, SEEK_DATA); if (data_offset != expected) { fprintf(stderr, "lseek(fd, %d, SEEK_DATA) = %d (expected %d)\n", (int)offset, (int)data_offset, (int)expected); exit(2); } } static void seek_hole(int fd, off_t offset, off_t expected) { off_t hole_offset = lseek(fd, offset, SEEK_HOLE); if (hole_offset != expected) { fprintf(stderr, "lseek(fd, %d, SEEK_HOLE) = %d (expected %d)\n", (int)offset, (int)hole_offset, (int)expected); exit(2); } } int main(int argc, char **argv) { char *execname = argv[0]; char *file_path = argv[1]; char *buf = NULL; int err; if (argc != 4) { (void) printf("usage: %s " "\n", argv[0]); exit(1); } int fd = open(file_path, O_RDWR | O_CREAT, 0666); if (fd == -1) { (void) fprintf(stderr, "%s: %s: ", execname, file_path); perror("open"); exit(2); } off_t file_size = atoi(argv[2]); off_t block_size = atoi(argv[3]); if (block_size * 2 > file_size) { (void) fprintf(stderr, "file size must be at least " "double the block size\n"); exit(2); } err = ftruncate(fd, file_size); if (err == -1) { perror("ftruncate"); exit(2); } if ((buf = mmap(NULL, file_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED) { perror("mmap"); exit(2); } /* Verify the file is sparse and reports no data. */ seek_data(fd, 0, -1); /* Verify the file is reported as a hole. */ seek_hole(fd, 0, 0); /* Verify search beyond end of file is an error. */ seek_data(fd, 2 * file_size, -1); seek_hole(fd, 2 * file_size, -1); /* Dirty the first byte. */ memset(buf, 'a', 1); seek_data(fd, 0, 0); seek_data(fd, block_size, -1); seek_hole(fd, 0, block_size); seek_hole(fd, block_size, block_size); /* Dirty the first half of the file. */ memset(buf, 'b', file_size / 2); seek_data(fd, 0, 0); seek_data(fd, block_size, block_size); seek_hole(fd, 0, P2ROUNDUP(file_size / 2, block_size)); seek_hole(fd, block_size, P2ROUNDUP(file_size / 2, block_size)); /* Dirty the whole file. */ memset(buf, 'c', file_size); seek_data(fd, 0, 0); seek_data(fd, file_size * 3 / 4, P2ROUNDUP(file_size * 3 / 4, block_size)); seek_hole(fd, 0, file_size); seek_hole(fd, file_size / 2, file_size); /* Punch a hole (required compression be enabled). */ memset(buf + block_size, 0, block_size); seek_data(fd, 0, 0); seek_data(fd, block_size, 2 * block_size); seek_hole(fd, 0, block_size); seek_hole(fd, block_size, block_size); seek_hole(fd, 2 * block_size, file_size); err = munmap(buf, file_size); if (err == -1) { perror("munmap"); exit(2); } close(fd); return (0); } diff --git a/tests/zfs-tests/tests/functional/cp_files/seekflood.c b/tests/zfs-tests/tests/functional/cp_files/seekflood.c index 02c2c8e6eca5..f832db85970d 100644 --- a/tests/zfs-tests/tests/functional/cp_files/seekflood.c +++ b/tests/zfs-tests/tests/functional/cp_files/seekflood.c @@ -1,180 +1,187 @@ /* * SPDX-License-Identifier: MIT * * Copyright (c) 2023, Rob Norris * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to * deal in the Software without restriction, including without limitation the * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or * sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS * IN THE SOFTWARE. * */ #ifndef _GNU_SOURCE #define _GNU_SOURCE #endif #include #include #include #include #include #include #include #include +/* some older uClibc's lack the defines, so we'll manually define them */ +#ifdef __UCLIBC__ +#ifndef SEEK_DATA +#define SEEK_DATA 3 +#endif +#endif + #define DATASIZE (4096) char data[DATASIZE]; static int _open_file(int n, int wr) { char buf[256]; int fd; snprintf(buf, sizeof (buf), "testdata_%d_%d", getpid(), n); if ((fd = open(buf, wr ? (O_WRONLY | O_CREAT) : O_RDONLY, wr ? (S_IRUSR | S_IWUSR) : 0)) < 0) { fprintf(stderr, "Error: open '%s' (%s): %s\n", buf, wr ? "write" : "read", strerror(errno)); exit(1); } return (fd); } static void _write_file(int n, int fd) { /* write a big ball of stuff */ ssize_t nwr = write(fd, data, DATASIZE); if (nwr < 0) { fprintf(stderr, "Error: write '%d_%d': %s\n", getpid(), n, strerror(errno)); exit(1); } else if (nwr < DATASIZE) { fprintf(stderr, "Error: write '%d_%d': short write\n", getpid(), n); exit(1); } } static int _seek_file(int n, int fd) { struct stat st; if (fstat(fd, &st) < 0) { fprintf(stderr, "Error: fstat '%d_%d': %s\n", getpid(), n, strerror(errno)); exit(1); } /* * A zero-sized file correctly has no data, so seeking the file is * pointless. */ if (st.st_size == 0) return (0); /* size is real, and we only write, so SEEK_DATA must find something */ if (lseek(fd, 0, SEEK_DATA) < 0) { if (errno == ENXIO) return (1); fprintf(stderr, "Error: lseek '%d_%d': %s\n", getpid(), n, strerror(errno)); exit(2); } return (0); } int main(int argc, char **argv) { int nfiles = 0; int nthreads = 0; if (argc < 3 || (nfiles = atoi(argv[1])) == 0 || (nthreads = atoi(argv[2])) == 0) { printf("usage: seekflood \n"); exit(1); } memset(data, 0x5a, DATASIZE); /* fork off some flood threads */ for (int i = 0; i < nthreads; i++) { if (!fork()) { /* thread main */ /* create zero file */ int fd = _open_file(0, 1); _write_file(0, fd); close(fd); int count = 0; int h = 0, i, j, rfd, wfd; for (i = 0; i < nfiles; i += 2, h++) { j = i+1; /* seek h, write i */ rfd = _open_file(h, 0); wfd = _open_file(i, 1); count += _seek_file(h, rfd); _write_file(i, wfd); close(rfd); close(wfd); /* seek i, write j */ rfd = _open_file(i, 0); wfd = _open_file(j, 1); count += _seek_file(i, rfd); _write_file(j, wfd); close(rfd); close(wfd); } /* return count of failed seeks to parent */ exit(count < 256 ? count : 255); } } /* wait for threads, take their seek fail counts from exit code */ int count = 0, crashed = 0; for (int i = 0; i < nthreads; i++) { int wstatus; wait(&wstatus); if (WIFEXITED(wstatus)) count += WEXITSTATUS(wstatus); else crashed++; } if (crashed) { fprintf(stderr, "Error: child crashed; test failed\n"); exit(1); } if (count) { fprintf(stderr, "Error: %d seek failures; test failed\n", count); exit(1); } exit(0); }