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_ftruncate.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 http://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
/*
28
* Tests async writeback behaviour. Creates a file, maps it into memory, and
29
* dirties every page within it. Then, calls ftruncate() to collapse the file
30
* back down to 0. This causes the kernel to begin writeback on the dirty
31
* pages so they can be freed, before it can complete the ftruncate() call.
32
* None of these are sync operations, so they should avoid the various "force
33
* flush" codepaths.
34
*/
35
36
#include <unistd.h>
37
#include <fcntl.h>
38
#include <sys/stat.h>
39
#include <sys/mman.h>
40
#include <stdlib.h>
41
#include <stdio.h>
42
43
#define _pdfail(f, l, s) \
44
do { perror("[" f "#" #l "] " s); exit(2); } while (0)
45
#define pdfail(str) _pdfail(__FILE__, __LINE__, str)
46
47
int
48
main(int argc, char **argv) {
49
if (argc != 3) {
50
printf("usage: mmap_ftruncate <file> <size>\n");
51
exit(2);
52
}
53
54
const char *file = argv[1];
55
56
char *end;
57
off_t sz = strtoull(argv[2], &end, 0);
58
if (end == argv[2] || *end != '\0' || sz == 0) {
59
fprintf(stderr, "E: invalid size");
60
exit(2);
61
}
62
63
int fd = open(file, O_CREAT|O_TRUNC|O_RDWR, S_IRUSR|S_IWUSR);
64
if (fd < 0)
65
pdfail("open");
66
67
if (ftruncate(fd, sz) < 0)
68
pdfail("ftruncate");
69
70
char *p = mmap(NULL, sz, PROT_WRITE, MAP_SHARED, fd, 0);
71
if (p == MAP_FAILED)
72
pdfail("mmap");
73
74
for (off_t off = 0; off < sz; off += 4096)
75
p[off] = 1;
76
77
if (ftruncate(fd, 0) < 0)
78
pdfail("ftruncate");
79
80
if (munmap(p, sz) < 0)
81
pdfail("munmap");
82
83
close(fd);
84
return (0);
85
}
86
87