xref: /freebsd/tools/test/stress2/misc/mmap33.sh (revision 90aac0d83bc9645f51ef0c2aeae6f9c0540bb031)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2021 Peter Holm <pho@FreeBSD.org>
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29# ftruncate+mmap+fsync fails for small maps
30# https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=225586
31
32# Original test scenario by tris_vern@hotmail.com
33
34# Fixed in r328773:
35# On pageout, in vnode generic pager, for partially dirty page, only
36# clear dirty bits for completely invalid blocks.
37
38. ../default.cfg
39
40cat > /tmp/mmap33.c <<EOF
41#include <sys/mman.h>
42
43#include <err.h>
44#include <fcntl.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49
50int
51main (int argc, char *argv[])
52{
53	size_t i, size1, size2;
54	int fd;
55	char *data;
56	char *filename;
57	char pattern = 0x01;
58
59	if (argc != 4) {
60		fprintf(stderr, "Usage: %s filename size1 size2\n", argv[0]);
61		exit(1);
62	}
63
64	filename = argv[1];
65	size1 = atoi(argv[2]);
66	size2 = atoi(argv[3]);
67
68	fd = open(filename, O_RDWR | O_TRUNC | O_CREAT, 0644);
69	for (i = 0; i < size1; i++)
70		write(fd, &pattern, 1);
71	close(fd);
72
73	fd = open(filename, O_RDWR, 0644);
74	if (fd == -1)
75		err(1, "open(%s)", filename);
76	if (ftruncate(fd, size2) == -1)
77		err(1, "ftruncate()");
78	data = mmap(NULL, size2, PROT_READ|PROT_WRITE,MAP_SHARED, fd, 0);
79	if (data == MAP_FAILED)
80		err(1, "mmap()");
81	memset(data, 0xFF, size2);
82
83	if (munmap(data, size2) == -1)
84		err(1, "munmap");
85	close(fd);
86
87	return (0);
88}
89EOF
90cc -o /tmp/mmap33 -Wall -Wextra -O2 -g /tmp/mmap33.c || exit 1
91rm /tmp/mmap33.c
92
93set -e
94mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
95[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
96mdconfig -a -t swap -s 2g -u $mdstart
97newfs $newfs_flags -n md$mdstart > /dev/null
98mount /dev/md$mdstart $mntpoint
99set +e
100
101file=file
102odir=`pwd`
103cd $mntpoint
104/tmp/mmap33 $file 1024 511
105s=$?
106sum1=`md5 < $mntpoint/$file`
107[ -f mmap33.core -a $s -eq 0 ] &&
108    { ls -l mmap33.core; mv mmap33.core /tmp; s=1; }
109cd $odir
110umount $mntpoint
111mount /dev/md$mdstart $mntpoint
112# This fails for truncate size < 512
113sum2=`md5 < $mntpoint/$file`
114[ $sum1 = $sum2 ] ||
115    { s=2; echo "md5 fingerprint differs."; }
116umount $mntpoint
117
118mdconfig -d -u $mdstart
119rm /tmp/mmap33
120exit $s
121