Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/clone_mmap_cached.c
48529 views
// SPDX-License-Identifier: CDDL-1.01/*2* CDDL HEADER START3*4* The contents of this file are subject to the terms of the5* 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.LICENSE9* or https://opensource.org/licenses/CDDL-1.0.10* See the License for the specific language governing permissions11* and limitations under the License.12*13* When distributing Covered Code, include this CDDL HEADER in each14* file and include the License file at usr/src/OPENSOLARIS.LICENSE.15* If applicable, add the following below this CDDL HEADER, with the16* fields enclosed by brackets "[]" replaced with your own identifying17* information: Portions Copyright [yyyy] [name of copyright owner]18*19* CDDL HEADER END20*/2122/*23* Copyright (c) 2024 by Pawel Jakub Dawidek24*/2526#include <sys/mman.h>27#include <sys/stat.h>2829#include <assert.h>30#include <errno.h>31#include <fcntl.h>32#include <stdbool.h>33#include <stdio.h>34#include <stdlib.h>35#include <string.h>36#include <unistd.h>3738#ifdef __FreeBSD__39#define loff_t off_t40#endif4142ssize_t43copy_file_range(int, loff_t *, int, loff_t *, size_t, unsigned int)44__attribute__((weak));4546static void *47mmap_file(int fd, size_t size)48{49void *p;5051p = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);52if (p == MAP_FAILED) {53(void) fprintf(stderr, "mmap failed: %s\n", strerror(errno));54exit(2);55}5657return (p);58}5960static void61usage(const char *progname)62{6364/*65* -i cache input before copy_file_range(2).66* -o cache input before copy_file_range(2).67*/68(void) fprintf(stderr, "usage: %s [-io] <input> <output>\n", progname);69exit(3);70}7172int73main(int argc, char *argv[])74{75int dfd, sfd;76size_t dsize, ssize;77void *dmem, *smem, *ptr;78off_t doff, soff;79struct stat sb;80bool cache_input, cache_output;81const char *progname;82int c;8384progname = argv[0];85cache_input = cache_output = false;8687while ((c = getopt(argc, argv, "io")) != -1) {88switch (c) {89case 'i':90cache_input = true;91break;92case 'o':93cache_output = true;94break;95default:96usage(progname);97}98}99argc -= optind;100argv += optind;101102if (argc != 2) {103usage(progname);104}105106sfd = open(argv[0], O_RDONLY);107if (fstat(sfd, &sb) == -1) {108(void) fprintf(stderr, "fstat failed: %s\n", strerror(errno));109exit(2);110}111ssize = sb.st_size;112smem = mmap_file(sfd, ssize);113114dfd = open(argv[1], O_RDWR);115if (fstat(dfd, &sb) == -1) {116(void) fprintf(stderr, "fstat failed: %s\n", strerror(errno));117exit(2);118}119dsize = sb.st_size;120dmem = mmap_file(dfd, dsize);121122/*123* Hopefully it won't be compiled out.124*/125if (cache_input) {126ptr = malloc(ssize);127assert(ptr != NULL);128memcpy(ptr, smem, ssize);129free(ptr);130}131if (cache_output) {132ptr = malloc(ssize);133assert(ptr != NULL);134memcpy(ptr, dmem, dsize);135free(ptr);136}137138soff = doff = 0;139if (copy_file_range(sfd, &soff, dfd, &doff, ssize, 0) < 0) {140(void) fprintf(stderr, "copy_file_range failed: %s\n",141strerror(errno));142exit(2);143}144145exit(memcmp(smem, dmem, ssize) == 0 ? 0 : 1);146}147148149