Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-src
Path: blob/main/sys/contrib/openzfs/tests/zfs-tests/cmd/mmap_write_sync.c
48529 views
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) 2025, Klara, Inc.
25
*/
26
27
#include <stdio.h>
28
#include <unistd.h>
29
#include <stdlib.h>
30
#include <fcntl.h>
31
#include <sys/stat.h>
32
#include <sys/mman.h>
33
34
#define PAGES (8)
35
36
int
37
main(int argc, char **argv)
38
{
39
if (argc != 2) {
40
fprintf(stderr, "usage: %s <filename>\n", argv[0]);
41
exit(1);
42
}
43
44
long page_size = sysconf(_SC_PAGESIZE);
45
if (page_size < 0) {
46
perror("sysconf");
47
exit(2);
48
}
49
size_t map_size = page_size * PAGES;
50
51
int fd = open(argv[1], O_CREAT|O_RDWR, S_IRWXU|S_IRWXG|S_IRWXO);
52
if (fd < 0) {
53
perror("open");
54
exit(2);
55
}
56
57
if (ftruncate(fd, map_size) < 0) {
58
perror("ftruncate");
59
close(fd);
60
exit(2);
61
}
62
63
uint64_t *p =
64
mmap(NULL, map_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
65
if (p == MAP_FAILED) {
66
perror("mmap");
67
close(fd);
68
exit(2);
69
}
70
71
for (int i = 0; i < (map_size / sizeof (uint64_t)); i++)
72
p[i] = 0x0123456789abcdef;
73
74
if (msync(p, map_size, MS_SYNC) < 0) {
75
perror("msync");
76
munmap(p, map_size);
77
close(fd);
78
exit(3);
79
}
80
81
munmap(p, map_size);
82
close(fd);
83
exit(0);
84
}
85
86