xref: /freebsd/tools/test/stress2/misc/overlap.sh (revision 19261079)
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
48bsdlabel -w md$mdstart auto
49
50newfs -U md${mdstart}$part > /dev/null
51
52mount /dev/md${mdstart}$part $mntpoint
53chmod 777 $mntpoint
54
55(cd $mntpoint; /tmp/overlap)
56s=$?
57
58while mount | grep $mntpoint | grep -q /dev/md; do
59	umount $mntpoint || sleep 1
60done
61mdconfig -d -u $mdstart
62rm -f /tmp/overlap
63exit $s
64EOF
65#include <err.h>
66#include <fcntl.h>
67#include <pthread.h>
68#include <signal.h>
69#include <stdio.h>
70#include <stdlib.h>
71#include <sys/types.h>
72#include <sys/uio.h>
73#include <unistd.h>
74
75char file[128];
76int bsiz, siz;
77
78void
79handler(int s __unused)
80{
81	_exit(0);
82}
83
84void *
85writer(void *arg __unused) {
86	int fdes, count;
87	ssize_t nwrite;
88	int *buf;
89
90	if ((fdes = open(file, O_RDWR|O_CREAT, 0664)) == -1)
91		err(1, "open(%s)", file);
92
93	count = 0;
94	buf = malloc(bsiz * sizeof(int));
95	buf[0] = buf[bsiz - 1] = 0;
96	while ((nwrite = pwrite(fdes, buf, siz, 0)) != -1) {
97		if (nwrite < siz)
98			err(1, "pwrite @ count %d, nwrite %zd", count, nwrite);
99		buf[0] = ++buf[bsiz - 1];
100		count++;
101	}
102
103	err(1, "pwrite()");
104}
105
106void *
107reader(void *arg __unused) {
108	int fdes, count;
109	ssize_t nread;
110	int *buf;
111
112	if ((fdes = open(file, O_RDWR|O_CREAT, 0664)) == -1)
113		err(1, "open(%s)", file);
114	count = 0;
115
116	buf = malloc(bsiz * sizeof(int));
117	while ((nread = pread(fdes, buf, siz, 0)) == 0)
118		continue;
119
120	do {
121		if (nread < siz)
122			err(1, "pread @ count %d, nread %zd", count, nread);
123		if (buf[0] != buf[bsiz - 1]) {
124			printf("Mismatch @ count %d, %d != %d\n",
125			    count, buf[0], buf[bsiz - 1]);
126			abort();
127		}
128		count++;
129	} while ((nread = pread(fdes, buf, siz, 0)) != -1);
130
131	err(1, "pread()");
132}
133
134int
135main(int argc, char **argv) {
136	pthread_t rp, wp;
137	int ret;
138	void *exitstatus;
139
140	snprintf(file, sizeof(file), "test.%0d5", getpid());
141	siz = 65536;
142	if (argc == 2)
143		siz = atoi(argv[1]);
144
145	bsiz = siz / sizeof(int);
146
147	signal(SIGALRM, handler);
148	alarm(300);
149
150	if ((ret = pthread_create(&wp, NULL, writer, NULL)) != 0)
151		errc(1, ret, "pthread_create");
152	if ((ret = pthread_create(&rp, NULL, reader, NULL)) != 0)
153		errc(1, ret, "pthread_create");
154
155	pthread_join(rp, &exitstatus);
156	pthread_join(wp, &exitstatus);
157
158	unlink(file);
159	return (0);
160}
161