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