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