xref: /freebsd/tools/test/stress2/misc/indir.sh (revision d9a42747950146bf03cda7f6e25d219253f8a57a)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# Hunt for "panic: ufsdirhash_dirtrunc: bad offset"
31# by making the directory inode grow into the indirect blocks.
32
33# Truncate directories larger than 12 * block_size bytes
34# default is 12 * 32768 = 393216, 384k
35
36# No problems seen.
37
38. ../default.cfg
39[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
40
41dir=/tmp
42odir=`pwd`
43cd $dir
44sed '1,/^EOF/d' < $odir/$0 > $dir/indir.c
45sed -i '' -e "s#MNTPOINT#$mntpoint#g" $dir/indir.c
46mycc -o indir -Wall -Wextra -O0 -g indir.c || exit 1
47rm -f indir.c
48cd $odir
49
50set -e
51mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
52[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
53mdconfig -a -t swap -s 1g -u $mdstart
54newfs -j -n -b 16384 -f 2048 -i 2048 md$mdstart > /dev/null
55mount /dev/md$mdstart $mntpoint
56ifree=`df -i $mntpoint | tail -1 | awk '{print $7 - 50}'`
57set +e
58
59if [ $dtrace ]; then
60	dtrace -w -n '*ufsdirhash_dirtrunc:entry {@rw[execname,probefunc] = \
61	    count(); }' &
62	dpid=$!
63	sleep 2
64fi
65
66/tmp/indir $mntpoint $ifree
67
68if [ $dtrace ]; then
69	kill -s TERM $dpid
70	wait $dpid
71fi
72
73for i in `jot 6`; do
74	mount | grep -q "on $mntpoint " || break
75	umount $mntpoint && break || sleep 10
76	[ $i -eq 6 ] &&
77	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
78done
79mdconfig -d -u $mdstart
80rm -f /tmp/indir
81if [ $dtrace ]; then
82	while pgrep -q dtrace; do sleep 1; done
83	kldstat | grep -q dtraceall &&
84	    kldunload dtraceall.ko
85fi
86exit 0
87EOF
88#include <sys/param.h>
89#include <sys/mman.h>
90#include <sys/stat.h>
91#include <sys/wait.h>
92
93#include <err.h>
94#include <errno.h>
95#include <fcntl.h>
96#include <stdatomic.h>
97#include <stdio.h>
98#include <stdlib.h>
99#include <time.h>
100#include <unistd.h>
101
102static long files;
103static char *path;
104
105#define PARALLEL 64
106#define RUNTIME (10 * 60)
107
108static void
109test(int idx)
110{
111	long j;
112	int fd;
113	pid_t pid;
114	char dir[128], file[128], new[128];
115
116	sleep(1);
117
118	snprintf(dir, sizeof(dir), "%s/d%d", path, idx);
119	if (mkdir(dir, 0755) == -1) {
120		if (errno != EEXIST)
121			err(1, "mkdir(%s)", dir);
122	}
123	if (chdir(dir) == -1)
124		err(1, "chdir(%s)", dir);
125
126	pid = getpid();
127	for (j = 0; j < files; j++) {
128		sprintf(file,"p%05d.%05ld", pid, j);
129		if ((fd = creat(file, 0660)) == -1)
130			err(1, "creat(%s). %s:%d", file, __FILE__, __LINE__);
131		if (fd != -1 && close(fd) == -1)
132			err(2, "close(%ld)", j);
133	}
134
135	for (j = 0; j < files; j++) {
136		sprintf(file,"p%05d.%05ld", pid, j);
137		sprintf(new,"p%05d.%05ld.old", pid, j);
138		if (rename(file, new) == -1)
139			err(1, "rename(%s, %s)", file, new);
140	}
141
142	for (j = 0; j < files; j++) {
143		sprintf(file,"p%05d.%05ld", pid, j);
144		sprintf(new,"p%05d.%05ld.old", pid, j);
145		if (rename(new, file) == -1)
146			err(1, "rename(%s, %s)", new, file);
147	}
148
149	for (j = 0; j < files; j++) {
150		sprintf(file,"p%05d.%05ld", pid, j);
151		if (unlink(file) == -1)
152			warn("unlink(%s)", file);
153
154	}
155	if (chdir("..") == -1)
156		err(1, "chdir ..");
157
158	_exit(0);
159}
160
161int
162main(int argc, char *argv[])
163{
164	pid_t pids[PARALLEL];
165	time_t start;
166	int e, i, n, status;
167
168	if (argc != 3) {
169		fprintf(stderr, "Usage %s <path> <files>\n", argv[0]);
170		exit(1);
171	}
172	path = argv[1];
173	files = atol(argv[2]) / PARALLEL;
174	fprintf(stderr, "Using %ld inodes per dir. %d threads. %ld inodes in total\n",
175		files, PARALLEL, files * PARALLEL);
176	e = n = 0;
177	start = time(NULL);
178	while ((time(NULL) - start) < RUNTIME && e == 0) {
179		fprintf(stderr, "Loop #%d\n", ++n);
180		for (i = 0; i < PARALLEL; i++) {
181			if ((pids[i] = fork()) == 0)
182				test(i);
183			if (pids[i] == -1)
184				err(1, "fork()");
185		}
186		for (i = 0; i < PARALLEL; i++) {
187			if (waitpid(pids[i], &status, 0) == -1)
188				err(1, "waitpid(%d)", pids[i]);
189			if (status != 0) {
190				if (WIFSIGNALED(status))
191					fprintf(stderr,
192					    "pid %d exit signal %d\n",
193					    pids[i], WTERMSIG(status));
194			}
195			e += status == 0 ? 0 : 1;
196		}
197//		system("(cd MNTPOINT; umount MNTPOINT) > /dev/null 2>&1");
198	}
199
200	return (e);
201}
202