Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/mmap_write_sync.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) 2025, Klara, Inc.24*/2526#include <stdio.h>27#include <unistd.h>28#include <stdlib.h>29#include <fcntl.h>30#include <sys/stat.h>31#include <sys/mman.h>3233#define PAGES (8)3435int36main(int argc, char **argv)37{38if (argc != 2) {39fprintf(stderr, "usage: %s <filename>\n", argv[0]);40exit(1);41}4243long page_size = sysconf(_SC_PAGESIZE);44if (page_size < 0) {45perror("sysconf");46exit(2);47}48size_t map_size = page_size * PAGES;4950int fd = open(argv[1], O_CREAT|O_RDWR, S_IRWXU|S_IRWXG|S_IRWXO);51if (fd < 0) {52perror("open");53exit(2);54}5556if (ftruncate(fd, map_size) < 0) {57perror("ftruncate");58close(fd);59exit(2);60}6162uint64_t *p =63mmap(NULL, map_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);64if (p == MAP_FAILED) {65perror("mmap");66close(fd);67exit(2);68}6970for (int i = 0; i < (map_size / sizeof (uint64_t)); i++)71p[i] = 0x0123456789abcdef;7273if (msync(p, map_size, MS_SYNC) < 0) {74perror("msync");75munmap(p, map_size);76close(fd);77exit(3);78}7980munmap(p, map_size);81close(fd);82exit(0);83}848586