xref: /freebsd/tools/test/stress2/misc/suj5.sh (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
1#!/bin/sh
2
3#
4# Copyright (c) 2010 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# Deadlock seen
32
33. ../default.cfg
34
35here=`pwd`
36cd /tmp
37sed '1,/^EOF/d' < $here/$0 > suj5.c
38mycc -o suj5 -Wall -O2 suj5.c
39rm -f suj5.c
40cd $here
41
42mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
43mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
44mdconfig -a -t swap -s 1g -u $mdstart
45newfs -j md$mdstart > /dev/null
46mount /dev/md$mdstart $mntpoint
47chmod 777 $mntpoint
48
49su $testuser -c "cd $mntpoint; /tmp/suj5" > /dev/null
50
51while mount | grep $mntpoint | grep -q /dev/md; do
52	umount $mntpoint || sleep 1
53done
54mdconfig -d -u $mdstart
55rm -f /tmp/suj5
56exit
57EOF
58#include <sys/param.h>
59#include <sys/mount.h>
60#include <sys/stat.h>
61#include <sys/wait.h>
62#include <err.h>
63#include <fcntl.h>
64#include <stdio.h>
65#include <stdlib.h>
66#include <unistd.h>
67
68#define PARALLEL 10
69
70static int
71random_int(int mi, int ma)
72{
73	return (arc4random()  % (ma - mi + 1) + mi);
74}
75
76static int64_t
77df(void)
78{
79	char path[MAXPATHLEN+1];
80	struct statfs buf;
81
82	if (getcwd(path, sizeof(path)) == NULL)
83		err(1, "getcwd()");
84
85	if (statfs(path, &buf) < 0)
86		err(1, "statfs(%s)", path);
87	printf("Free space on %s: %jd Mb\n", path, buf.f_bavail * buf.f_bsize / 1024 / 1024);
88	return (buf.f_bavail * buf.f_bsize);
89}
90
91static void
92test(int size)
93{
94	int buf[1024], index, to;
95#ifdef TEST
96	int i;
97#endif
98	int fd;
99	char file[128];
100
101	sprintf(file,"p%05d", getpid());
102	if ((fd = creat(file, 0660)) == -1)
103		err(1, "creat(%s)", file);
104
105	to = sizeof(buf);
106	index = 0;
107	while (index < size) {
108		if (index + to > size)
109			to = size - index;
110#ifdef TEST
111		for (i = 0; i < to; i++)
112			buf[i] = index + i;
113#endif
114		index += to;
115		if (write(fd, buf, to) != to)
116			err(1, "write(%s), %s:%d", file, __FILE__, __LINE__);
117	}
118	if (close(fd) == -1)
119		err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);
120
121	if ((fd = open(file, O_RDONLY)) == -1)
122		err(1, "open(%s), %s:%d", file, __FILE__, __LINE__);
123
124	index = 0;
125	while (index < size) {
126		if (index + to > size)
127			to = size - index;
128		if (read(fd, buf, to) != to)
129			err(1, "rw read. %s.%d", __FILE__, __LINE__);
130#ifdef TEST
131		for (i = 0; i < to; i++) {
132			if (buf[i] != index + i) {
133				fprintf(stderr,
134					"%s, pid %d: expected %d @ %d, got %d\n",
135					getprogname(), getpid(), index+i, index+i,
136					buf[i]);
137				exit(EXIT_FAILURE);
138			}
139		}
140#endif
141		index += to;
142	}
143	if (close(fd) == -1)
144		err(1, "close(%s), %s:%d", file, __FILE__, __LINE__);
145	if (unlink(file) == -1)
146		err(1, "unlink(%s), %s:%d", file, __FILE__, __LINE__);
147	exit(0);
148}
149
150int
151main()
152{
153	int i, j, pct;
154	int size;	/* in k */
155	int64_t bl;
156
157	bl = df();
158	if (bl > (int64_t)INT_MAX * PARALLEL)
159		bl = (int64_t)INT_MAX * PARALLEL;
160	size = bl / PARALLEL / 1024;
161
162	pct = random_int(1, 50);
163	size = size / 100 * pct + 1;
164	if (random_int(1, 100) <= 50)
165		size = 34 * 1024;	/* Known good deadlock value */
166	printf("Max file size: %d Mb\n", size / 1024);
167
168	for (i = 0; i < 100; i++) {
169		for (j = 0; j < PARALLEL; j++) {
170			if (fork() == 0) {
171				test(random_int(1, size) * 1024);
172			}
173		}
174		for (j = 0; j < PARALLEL; j++)
175			wait(NULL);
176	}
177
178	return (0);
179}
180