xref: /freebsd/tools/test/stress2/misc/suj22.sh (revision 8a272653d9fbd9fc37691c9aad6a05089b4ecb4d)
1*8a272653SPeter Holm#!/bin/sh
2*8a272653SPeter Holm
3*8a272653SPeter Holm#
4*8a272653SPeter Holm# Copyright (c) 2011 Peter Holm <pho@FreeBSD.org>
5*8a272653SPeter Holm# All rights reserved.
6*8a272653SPeter Holm#
7*8a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
8*8a272653SPeter Holm# modification, are permitted provided that the following conditions
9*8a272653SPeter Holm# are met:
10*8a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
11*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
12*8a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
13*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
14*8a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
15*8a272653SPeter Holm#
16*8a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*8a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*8a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8a272653SPeter Holm# SUCH DAMAGE.
27*8a272653SPeter Holm#
28*8a272653SPeter Holm
29*8a272653SPeter Holm# Variation of suj20.sh, with focus on the FS state after a panic / power
30*8a272653SPeter Holm# cycle.
31*8a272653SPeter Holm
32*8a272653SPeter Holm# Demonstrate "multiple references to blocks" in FS after reboot from a
33*8a272653SPeter Holm# looping mksnap_ffs(8).
34*8a272653SPeter Holm
35*8a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
36*8a272653SPeter Holm
37*8a272653SPeter Holm. ../default.cfg
38*8a272653SPeter Holm
39*8a272653SPeter Holm# Scenario by mckusick@
40*8a272653SPeter Holm#
41*8a272653SPeter Holm# create a bunch of files/directories
42*8a272653SPeter Holm# create a snapshot
43*8a272653SPeter Holm# remove many (but not all) of those files/directories
44*8a272653SPeter Holm# create some new files/directories in what remains of those
45*8a272653SPeter Holm#     original files/directories.
46*8a272653SPeter Holm# create another snapshot
47*8a272653SPeter Holm# repeat {
48*8a272653SPeter Holm#         remove many (somewhat different) of those files/directories
49*8a272653SPeter Holm#         create some new files/directories in what remains of those
50*8a272653SPeter Holm#             remaining files/directories.
51*8a272653SPeter Holm#         create a new snapshot
52*8a272653SPeter Holm#         remove oldest snapshot
53*8a272653SPeter Holm# }
54*8a272653SPeter Holm
55*8a272653SPeter Holmsnap () {
56*8a272653SPeter Holm	for i in `jot 5`; do
57*8a272653SPeter Holm		mksnap_ffs $1 $2 2>&1 | grep -v "Resource temporarily unavailable"
58*8a272653SPeter Holm		[ ! -s $2 ] && rm -f $2	|| return 0
59*8a272653SPeter Holm		sleep 1
60*8a272653SPeter Holm	done
61*8a272653SPeter Holm	return 1
62*8a272653SPeter Holm}
63*8a272653SPeter Holm
64*8a272653SPeter HolmD=$diskimage
65*8a272653SPeter Holm
66*8a272653SPeter Holmif [ -n "`find $D -mtime -1h 2>/dev/null`" ]; then
67*8a272653SPeter Holm	# FS left by previous crash
68*8a272653SPeter Holm	mdconfig -a -t vnode -f $D -u $mdstart
69*8a272653SPeter Holm	fsck -t ufs -y md${mdstart}$part
70*8a272653SPeter Holm	fsck -t ufs -y md${mdstart}$part
71*8a272653SPeter Holm	rm -f $D
72*8a272653SPeter Holm	exit 0
73*8a272653SPeter Holmfi
74*8a272653SPeter Holm
75*8a272653SPeter Holmhere=`pwd`
76*8a272653SPeter Holmcd /tmp
77*8a272653SPeter Holmsed '1,/^EOF/d' < $here/$0 > suj22.c
78*8a272653SPeter Holmmycc -o suj22 -Wall -Wextra -O2 suj22.c
79*8a272653SPeter Holmrm -f suj22.c
80*8a272653SPeter Holm
81*8a272653SPeter Holmmount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
82*8a272653SPeter Holmmdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
83*8a272653SPeter Holm
84*8a272653SPeter Holmdd if=/dev/zero of=$D bs=1m count=1k status=none || exit 1
85*8a272653SPeter Holmmdconfig -a -t vnode -f $D -u $mdstart
86*8a272653SPeter Holmbsdlabel -w md$mdstart auto
87*8a272653SPeter Holmnewfs -j md${mdstart}$part > /dev/null
88*8a272653SPeter Holmmount /dev/md${mdstart}$part $mntpoint
89*8a272653SPeter Holm
90*8a272653SPeter Holmcd $mntpoint
91*8a272653SPeter Holmchmod 777 $mntpoint
92*8a272653SPeter Holm/tmp/suj22
93*8a272653SPeter Holmsnap $mntpoint $mntpoint/.snap/snap1
94*8a272653SPeter Holm/tmp/suj22 prune
95*8a272653SPeter Holmsnap $mntpoint $mntpoint/.snap/snap2
96*8a272653SPeter Holm/tmp/suj22
97*8a272653SPeter Holmfor i in `jot 10`; do
98*8a272653SPeter Holm	/tmp/suj22 prune
99*8a272653SPeter Holm	/tmp/suj22
100*8a272653SPeter Holm	snap $mntpoint $mntpoint/.snap/snap$((i + 2)) || break
101*8a272653SPeter Holm	sn=`ls -tU $mntpoint/.snap | tail -1`
102*8a272653SPeter Holm	rm -f $mntpoint/.snap/$sn
103*8a272653SPeter Holmdone
104*8a272653SPeter Holmcd $here
105*8a272653SPeter Holm
106*8a272653SPeter Holmwhile mount | grep -q $mntpoint; do
107*8a272653SPeter Holm	umount $mntpoint || sleep 1
108*8a272653SPeter Holmdone
109*8a272653SPeter Holmfsck -t ufs -y md${mdstart}$part
110*8a272653SPeter Holmfsck -t ufs -y md${mdstart}$part
111*8a272653SPeter Holmmdconfig -d -u $mdstart
112*8a272653SPeter Holmrm -f /tmp/suj22 $D
113*8a272653SPeter Holmexit 0
114*8a272653SPeter HolmEOF
115*8a272653SPeter Holm#include <sys/types.h>
116*8a272653SPeter Holm#include <err.h>
117*8a272653SPeter Holm#include <errno.h>
118*8a272653SPeter Holm#include <fcntl.h>
119*8a272653SPeter Holm#include <stdio.h>
120*8a272653SPeter Holm#include <stdlib.h>
121*8a272653SPeter Holm#include <sys/stat.h>
122*8a272653SPeter Holm#include <unistd.h>
123*8a272653SPeter Holm
124*8a272653SPeter Holmstatic char buf[4096];
125*8a272653SPeter Holm#define ND 100
126*8a272653SPeter Holm#define NF 100
127*8a272653SPeter Holm
128*8a272653SPeter Holmvoid
129*8a272653SPeter Holmsetup(void)
130*8a272653SPeter Holm{
131*8a272653SPeter Holm	int d, f, fd, i, n;
132*8a272653SPeter Holm	char name[128];
133*8a272653SPeter Holm
134*8a272653SPeter Holm	for (d = 0; d < ND; d++) {
135*8a272653SPeter Holm		snprintf(name, sizeof(name), "d%03d", d);
136*8a272653SPeter Holm		if (mkdir(name, 00700) == -1 && errno != EEXIST)
137*8a272653SPeter Holm			err(1, "mkdir(%s)", name);
138*8a272653SPeter Holm		if (chdir(name) == -1)
139*8a272653SPeter Holm			err(1, "chdir(%s)", name);
140*8a272653SPeter Holm		for (f = 0; f < NF; f++) {
141*8a272653SPeter Holm			if (arc4random() % 100 < 33)
142*8a272653SPeter Holm				continue;
143*8a272653SPeter Holm			snprintf(name, sizeof(name), "f%03d", f);
144*8a272653SPeter Holm			if ((fd = open(name, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
145*8a272653SPeter Holm				err(1, "open(%s)", name);
146*8a272653SPeter Holm			n = arc4random() % 10;
147*8a272653SPeter Holm			for (i = 0; i < n; i++) {
148*8a272653SPeter Holm				if (write(fd, buf, sizeof(buf)) != sizeof(buf))
149*8a272653SPeter Holm					err(1, "write()");
150*8a272653SPeter Holm			}
151*8a272653SPeter Holm			close(fd);
152*8a272653SPeter Holm		}
153*8a272653SPeter Holm		if (chdir("..") == -1)
154*8a272653SPeter Holm			err(1, "chdir(%s)", "..");
155*8a272653SPeter Holm	}
156*8a272653SPeter Holm}
157*8a272653SPeter Holmvoid
158*8a272653SPeter Holm
159*8a272653SPeter Holmprune(void)
160*8a272653SPeter Holm{
161*8a272653SPeter Holm	int d, f;
162*8a272653SPeter Holm	char name[128];
163*8a272653SPeter Holm
164*8a272653SPeter Holm	for (d = 0; d < ND; d++) {
165*8a272653SPeter Holm		snprintf(name, sizeof(name), "d%03d", d);
166*8a272653SPeter Holm		if (chdir(name) == -1)
167*8a272653SPeter Holm			err(1, "chdir(%s)", name);
168*8a272653SPeter Holm		for (f = 0; f < NF; f++) {
169*8a272653SPeter Holm			if (arc4random() % 100 < 33)
170*8a272653SPeter Holm				continue;
171*8a272653SPeter Holm			snprintf(name, sizeof(name), "f%03d", f);
172*8a272653SPeter Holm			if (unlink(name) == -1 && errno != ENOENT)
173*8a272653SPeter Holm				err(1, "unlink(%s)", name);
174*8a272653SPeter Holm		}
175*8a272653SPeter Holm		if (chdir("..") == -1)
176*8a272653SPeter Holm			err(1, "chdir(%s)", "..");
177*8a272653SPeter Holm	}
178*8a272653SPeter Holm	for (d = 0; d < ND; d++) {
179*8a272653SPeter Holm		if (arc4random() % 100 > 10)
180*8a272653SPeter Holm			continue;
181*8a272653SPeter Holm		snprintf(name, sizeof(name), "rm -rf d%03d", d);
182*8a272653SPeter Holm		system(name);
183*8a272653SPeter Holm	}
184*8a272653SPeter Holm}
185*8a272653SPeter Holm
186*8a272653SPeter Holmint
187*8a272653SPeter Holmmain(int argc, char **argv __unused)
188*8a272653SPeter Holm{
189*8a272653SPeter Holm	if (argc == 1)
190*8a272653SPeter Holm		setup();
191*8a272653SPeter Holm	if (argc == 2)
192*8a272653SPeter Holm		prune();
193*8a272653SPeter Holm
194*8a272653SPeter Holm	return (0);
195*8a272653SPeter Holm}
196