xref: /freebsd/tools/test/stress2/misc/overlap.sh (revision f126890a)
1#!/bin/sh
2
3#
4# Copyright (c) 2011 Peter Holm <pho@FreeBSD.org>
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
30
31# Test that fails on various non FreeBSD file systems.
32# 3.13.0-74-generic #118-Ubuntu SMP: Mismatch @ count 214, 0 != 123
33# OK on OS X (Darwin Kernel Version 15.5.0)
34
35# Scanario by: Michael Ubell ubell mindspring com.
36
37. ../default.cfg
38
39mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
40mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
41
42sed '1,/^EOF/d' < $0 > /tmp/overlap.c
43mycc -o /tmp/overlap -Wall -Wextra -O2 /tmp/overlap.c -lpthread || exit 1
44rm -f /tmp/overlap.c
45
46size="1g"
47mdconfig -a -t swap -s $size -u $mdstart || exit 1
48
49newfs -U md$mdstart > /dev/null
50
51mount /dev/md$mdstart $mntpoint
52chmod 777 $mntpoint
53
54(cd $mntpoint; /tmp/overlap)
55s=$?
56
57while mount | grep $mntpoint | grep -q /dev/md; do
58	umount $mntpoint || sleep 1
59done
60mdconfig -d -u $mdstart
61rm -f /tmp/overlap
62exit $s
63EOF
64#include <err.h>
65#include <fcntl.h>
66#include <pthread.h>
67#include <signal.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <sys/types.h>
71#include <sys/uio.h>
72#include <unistd.h>
73
74char file[128];
75int bsiz, siz;
76
77void
78handler(int s __unused)
79{
80	_exit(0);
81}
82
83void *
84writer(void *arg __unused) {
85	int fdes, count;
86	ssize_t nwrite;
87	int *buf;
88
89	if ((fdes = open(file, O_RDWR|O_CREAT, 0664)) == -1)
90		err(1, "open(%s)", file);
91
92	count = 0;
93	buf = malloc(bsiz * sizeof(int));
94	buf[0] = buf[bsiz - 1] = 0;
95	while ((nwrite = pwrite(fdes, buf, siz, 0)) != -1) {
96		if (nwrite < siz)
97			err(1, "pwrite @ count %d, nwrite %zd", count, nwrite);
98		buf[0] = ++buf[bsiz - 1];
99		count++;
100	}
101
102	err(1, "pwrite()");
103}
104
105void *
106reader(void *arg __unused) {
107	int fdes, count;
108	ssize_t nread;
109	int *buf;
110
111	if ((fdes = open(file, O_RDWR|O_CREAT, 0664)) == -1)
112		err(1, "open(%s)", file);
113	count = 0;
114
115	buf = malloc(bsiz * sizeof(int));
116	while ((nread = pread(fdes, buf, siz, 0)) == 0)
117		continue;
118
119	do {
120		if (nread < siz)
121			err(1, "pread @ count %d, nread %zd", count, nread);
122		if (buf[0] != buf[bsiz - 1]) {
123			printf("Mismatch @ count %d, %d != %d\n",
124			    count, buf[0], buf[bsiz - 1]);
125			abort();
126		}
127		count++;
128	} while ((nread = pread(fdes, buf, siz, 0)) != -1);
129
130	err(1, "pread()");
131}
132
133int
134main(int argc, char **argv) {
135	pthread_t rp, wp;
136	int ret;
137	void *exitstatus;
138
139	snprintf(file, sizeof(file), "test.%0d5", getpid());
140	siz = 65536;
141	if (argc == 2)
142		siz = atoi(argv[1]);
143
144	bsiz = siz / sizeof(int);
145
146	signal(SIGALRM, handler);
147	alarm(300);
148
149	if ((ret = pthread_create(&wp, NULL, writer, NULL)) != 0)
150		errc(1, ret, "pthread_create");
151	if ((ret = pthread_create(&rp, NULL, reader, NULL)) != 0)
152		errc(1, ret, "pthread_create");
153
154	pthread_join(rp, &exitstatus);
155	pthread_join(wp, &exitstatus);
156
157	unlink(file);
158	return (0);
159}
160