1*df58e8b1SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2*df58e8b1SMartin Matuska /*
3*df58e8b1SMartin Matuska * CDDL HEADER START
4*df58e8b1SMartin Matuska *
5*df58e8b1SMartin Matuska * The contents of this file are subject to the terms of the
6*df58e8b1SMartin Matuska * Common Development and Distribution License (the "License").
7*df58e8b1SMartin Matuska * You may not use this file except in compliance with the License.
8*df58e8b1SMartin Matuska *
9*df58e8b1SMartin Matuska * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*df58e8b1SMartin Matuska * or http://opensource.org/licenses/CDDL-1.0.
11*df58e8b1SMartin Matuska * See the License for the specific language governing permissions
12*df58e8b1SMartin Matuska * and limitations under the License.
13*df58e8b1SMartin Matuska *
14*df58e8b1SMartin Matuska * When distributing Covered Code, include this CDDL HEADER in each
15*df58e8b1SMartin Matuska * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*df58e8b1SMartin Matuska * If applicable, add the following below this CDDL HEADER, with the
17*df58e8b1SMartin Matuska * fields enclosed by brackets "[]" replaced with your own identifying
18*df58e8b1SMartin Matuska * information: Portions Copyright [yyyy] [name of copyright owner]
19*df58e8b1SMartin Matuska *
20*df58e8b1SMartin Matuska * CDDL HEADER END
21*df58e8b1SMartin Matuska */
22*df58e8b1SMartin Matuska
23*df58e8b1SMartin Matuska /*
24*df58e8b1SMartin Matuska * Copyright (c) 2025, Klara, Inc.
25*df58e8b1SMartin Matuska */
26*df58e8b1SMartin Matuska
27*df58e8b1SMartin Matuska /*
28*df58e8b1SMartin Matuska * Tests async writeback behaviour. Creates a file, maps it into memory, and
29*df58e8b1SMartin Matuska * dirties every page within it. Then, calls ftruncate() to collapse the file
30*df58e8b1SMartin Matuska * back down to 0. This causes the kernel to begin writeback on the dirty
31*df58e8b1SMartin Matuska * pages so they can be freed, before it can complete the ftruncate() call.
32*df58e8b1SMartin Matuska * None of these are sync operations, so they should avoid the various "force
33*df58e8b1SMartin Matuska * flush" codepaths.
34*df58e8b1SMartin Matuska */
35*df58e8b1SMartin Matuska
36*df58e8b1SMartin Matuska #include <unistd.h>
37*df58e8b1SMartin Matuska #include <fcntl.h>
38*df58e8b1SMartin Matuska #include <sys/stat.h>
39*df58e8b1SMartin Matuska #include <sys/mman.h>
40*df58e8b1SMartin Matuska #include <stdlib.h>
41*df58e8b1SMartin Matuska #include <stdio.h>
42*df58e8b1SMartin Matuska
43*df58e8b1SMartin Matuska #define _pdfail(f, l, s) \
44*df58e8b1SMartin Matuska do { perror("[" f "#" #l "] " s); exit(2); } while (0)
45*df58e8b1SMartin Matuska #define pdfail(str) _pdfail(__FILE__, __LINE__, str)
46*df58e8b1SMartin Matuska
47*df58e8b1SMartin Matuska int
main(int argc,char ** argv)48*df58e8b1SMartin Matuska main(int argc, char **argv) {
49*df58e8b1SMartin Matuska if (argc != 3) {
50*df58e8b1SMartin Matuska printf("usage: mmap_ftruncate <file> <size>\n");
51*df58e8b1SMartin Matuska exit(2);
52*df58e8b1SMartin Matuska }
53*df58e8b1SMartin Matuska
54*df58e8b1SMartin Matuska const char *file = argv[1];
55*df58e8b1SMartin Matuska
56*df58e8b1SMartin Matuska char *end;
57*df58e8b1SMartin Matuska off_t sz = strtoull(argv[2], &end, 0);
58*df58e8b1SMartin Matuska if (end == argv[2] || *end != '\0' || sz == 0) {
59*df58e8b1SMartin Matuska fprintf(stderr, "E: invalid size");
60*df58e8b1SMartin Matuska exit(2);
61*df58e8b1SMartin Matuska }
62*df58e8b1SMartin Matuska
63*df58e8b1SMartin Matuska int fd = open(file, O_CREAT|O_TRUNC|O_RDWR, S_IRUSR|S_IWUSR);
64*df58e8b1SMartin Matuska if (fd < 0)
65*df58e8b1SMartin Matuska pdfail("open");
66*df58e8b1SMartin Matuska
67*df58e8b1SMartin Matuska if (ftruncate(fd, sz) < 0)
68*df58e8b1SMartin Matuska pdfail("ftruncate");
69*df58e8b1SMartin Matuska
70*df58e8b1SMartin Matuska char *p = mmap(NULL, sz, PROT_WRITE, MAP_SHARED, fd, 0);
71*df58e8b1SMartin Matuska if (p == MAP_FAILED)
72*df58e8b1SMartin Matuska pdfail("mmap");
73*df58e8b1SMartin Matuska
74*df58e8b1SMartin Matuska for (off_t off = 0; off < sz; off += 4096)
75*df58e8b1SMartin Matuska p[off] = 1;
76*df58e8b1SMartin Matuska
77*df58e8b1SMartin Matuska if (ftruncate(fd, 0) < 0)
78*df58e8b1SMartin Matuska pdfail("ftruncate");
79*df58e8b1SMartin Matuska
80*df58e8b1SMartin Matuska if (munmap(p, sz) < 0)
81*df58e8b1SMartin Matuska pdfail("munmap");
82*df58e8b1SMartin Matuska
83*df58e8b1SMartin Matuska close(fd);
84*df58e8b1SMartin Matuska return (0);
85*df58e8b1SMartin Matuska }
86