aboutsummaryrefslogtreecommitdiff
path: root/tools/test/stress2/misc/suj22.sh
blob: beff053378c1c835fb9d958603fc9b6db67ceabe (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/bin/sh

#
# Copyright (c) 2011 Peter Holm <pho@FreeBSD.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

# Variation of suj20.sh, with focus on the FS state after a panic / power
# cycle.

# Demonstrate "multiple references to blocks" in FS after reboot from a
# looping mksnap_ffs(8).

[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1

. ../default.cfg

# Scenario by mckusick@
#
# create a bunch of files/directories
# create a snapshot
# remove many (but not all) of those files/directories
# create some new files/directories in what remains of those
#     original files/directories.
# create another snapshot
# repeat {
#         remove many (somewhat different) of those files/directories
#         create some new files/directories in what remains of those
#             remaining files/directories.
#         create a new snapshot
#         remove oldest snapshot
# }

snap () {
	for i in `jot 5`; do
		mksnap_ffs $1 $2 2>&1 | grep -v "Resource temporarily unavailable"
		[ ! -s $2 ] && rm -f $2	|| return 0
		sleep 1
	done
	return 1
}

D=$diskimage

if [ -n "`find $D -mtime -1h 2>/dev/null`" ]; then
	# FS left by previous crash
	mdconfig -a -t vnode -f $D -u $mdstart
	fsck -t ufs -y md${mdstart}$part
	fsck -t ufs -y md${mdstart}$part
	rm -f $D
	exit 0
fi

here=`pwd`
cd /tmp
sed '1,/^EOF/d' < $here/$0 > suj22.c
mycc -o suj22 -Wall -Wextra -O2 suj22.c
rm -f suj22.c

mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart

dd if=/dev/zero of=$D bs=1m count=1k status=none || exit 1
mdconfig -a -t vnode -f $D -u $mdstart
bsdlabel -w md$mdstart auto
newfs -j md${mdstart}$part > /dev/null
mount /dev/md${mdstart}$part $mntpoint

cd $mntpoint
chmod 777 $mntpoint
/tmp/suj22
snap $mntpoint $mntpoint/.snap/snap1
/tmp/suj22 prune
snap $mntpoint $mntpoint/.snap/snap2
/tmp/suj22
for i in `jot 10`; do
	/tmp/suj22 prune
	/tmp/suj22
	snap $mntpoint $mntpoint/.snap/snap$((i + 2)) || break
	sn=`ls -tU $mntpoint/.snap | tail -1`
	rm -f $mntpoint/.snap/$sn
done
cd $here

while mount | grep -q $mntpoint; do
	umount $mntpoint || sleep 1
done
fsck -t ufs -y md${mdstart}$part
fsck -t ufs -y md${mdstart}$part
mdconfig -d -u $mdstart
rm -f /tmp/suj22 $D
exit 0
EOF
#include <sys/types.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>

static char buf[4096];
#define ND 100
#define NF 100

void
setup(void)
{
	int d, f, fd, i, n;
	char name[128];

	for (d = 0; d < ND; d++) {
		snprintf(name, sizeof(name), "d%03d", d);
		if (mkdir(name, 00700) == -1 && errno != EEXIST)
			err(1, "mkdir(%s)", name);
		if (chdir(name) == -1)
			err(1, "chdir(%s)", name);
		for (f = 0; f < NF; f++) {
			if (arc4random() % 100 < 33)
				continue;
			snprintf(name, sizeof(name), "f%03d", f);
			if ((fd = open(name, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
				err(1, "open(%s)", name);
			n = arc4random() % 10;
			for (i = 0; i < n; i++) {
				if (write(fd, buf, sizeof(buf)) != sizeof(buf))
					err(1, "write()");
			}
			close(fd);
		}
		if (chdir("..") == -1)
			err(1, "chdir(%s)", "..");
	}
}
void

prune(void)
{
	int d, f;
	char name[128];

	for (d = 0; d < ND; d++) {
		snprintf(name, sizeof(name), "d%03d", d);
		if (chdir(name) == -1)
			err(1, "chdir(%s)", name);
		for (f = 0; f < NF; f++) {
			if (arc4random() % 100 < 33)
				continue;
			snprintf(name, sizeof(name), "f%03d", f);
			if (unlink(name) == -1 && errno != ENOENT)
				err(1, "unlink(%s)", name);
		}
		if (chdir("..") == -1)
			err(1, "chdir(%s)", "..");
	}
	for (d = 0; d < ND; d++) {
		if (arc4random() % 100 > 10)
			continue;
		snprintf(name, sizeof(name), "rm -rf d%03d", d);
		system(name);
	}
}

int
main(int argc, char **argv __unused)
{
	if (argc == 1)
		setup();
	if (argc == 2)
		prune();

	return (0);
}