Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/clone_mmap_cached.c
109057 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#if defined(_GNU_SOURCE) && defined(__linux__)39_Static_assert(sizeof (loff_t) == sizeof (off_t),40"loff_t and off_t must be the same size");41#endif4243ssize_t44copy_file_range(int, off_t *, int, off_t *, size_t, unsigned int)45__attribute__((weak));4647static void *48mmap_file(int fd, size_t size)49{50void *p;5152p = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);53if (p == MAP_FAILED) {54(void) fprintf(stderr, "mmap failed: %s\n", strerror(errno));55exit(2);56}5758return (p);59}6061static void62usage(const char *progname)63{6465/*66* -i cache input before copy_file_range(2).67* -o cache input before copy_file_range(2).68*/69(void) fprintf(stderr, "usage: %s [-io] <input> <output>\n", progname);70exit(3);71}7273int74main(int argc, char *argv[])75{76int dfd, sfd;77size_t dsize, ssize;78void *dmem, *smem, *ptr;79off_t doff, soff;80struct stat sb;81bool cache_input, cache_output;82const char *progname;83int c;8485progname = argv[0];86cache_input = cache_output = false;8788while ((c = getopt(argc, argv, "io")) != -1) {89switch (c) {90case 'i':91cache_input = true;92break;93case 'o':94cache_output = true;95break;96default:97usage(progname);98}99}100argc -= optind;101argv += optind;102103if (argc != 2) {104usage(progname);105}106107sfd = open(argv[0], O_RDONLY);108if (fstat(sfd, &sb) == -1) {109(void) fprintf(stderr, "fstat failed: %s\n", strerror(errno));110exit(2);111}112ssize = sb.st_size;113smem = mmap_file(sfd, ssize);114115dfd = open(argv[1], O_RDWR);116if (fstat(dfd, &sb) == -1) {117(void) fprintf(stderr, "fstat failed: %s\n", strerror(errno));118exit(2);119}120dsize = sb.st_size;121dmem = mmap_file(dfd, dsize);122123/*124* Hopefully it won't be compiled out.125*/126if (cache_input) {127ptr = malloc(ssize);128assert(ptr != NULL);129memcpy(ptr, smem, ssize);130free(ptr);131}132if (cache_output) {133ptr = malloc(ssize);134assert(ptr != NULL);135memcpy(ptr, dmem, dsize);136free(ptr);137}138139soff = doff = 0;140if (copy_file_range(sfd, &soff, dfd, &doff, ssize, 0) < 0) {141(void) fprintf(stderr, "copy_file_range failed: %s\n",142strerror(errno));143exit(2);144}145146exit(memcmp(smem, dmem, ssize) == 0 ? 0 : 1);147}148149150