xref: /freebsd/tools/test/stress2/misc/suj22.sh (revision 608c97bff921c8936eb71f4b1a177625cfa60250)
18a272653SPeter Holm#!/bin/sh
28a272653SPeter Holm
38a272653SPeter Holm#
48a272653SPeter Holm# Copyright (c) 2011 Peter Holm <pho@FreeBSD.org>
58a272653SPeter Holm# All rights reserved.
68a272653SPeter Holm#
78a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
88a272653SPeter Holm# modification, are permitted provided that the following conditions
98a272653SPeter Holm# are met:
108a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
118a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
128a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
138a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
148a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
158a272653SPeter Holm#
168a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
178a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
208a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268a272653SPeter Holm# SUCH DAMAGE.
278a272653SPeter Holm#
288a272653SPeter Holm
298a272653SPeter Holm# Variation of suj20.sh, with focus on the FS state after a panic / power
308a272653SPeter Holm# cycle.
318a272653SPeter Holm
328a272653SPeter Holm# Demonstrate "multiple references to blocks" in FS after reboot from a
338a272653SPeter Holm# looping mksnap_ffs(8).
348a272653SPeter Holm
358a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
368a272653SPeter Holm
378a272653SPeter Holm. ../default.cfg
388a272653SPeter Holm
398a272653SPeter Holm# Scenario by mckusick@
408a272653SPeter Holm#
418a272653SPeter Holm# create a bunch of files/directories
428a272653SPeter Holm# create a snapshot
438a272653SPeter Holm# remove many (but not all) of those files/directories
448a272653SPeter Holm# create some new files/directories in what remains of those
458a272653SPeter Holm#     original files/directories.
468a272653SPeter Holm# create another snapshot
478a272653SPeter Holm# repeat {
488a272653SPeter Holm#         remove many (somewhat different) of those files/directories
498a272653SPeter Holm#         create some new files/directories in what remains of those
508a272653SPeter Holm#             remaining files/directories.
518a272653SPeter Holm#         create a new snapshot
528a272653SPeter Holm#         remove oldest snapshot
538a272653SPeter Holm# }
548a272653SPeter Holm
558a272653SPeter Holmsnap () {
568a272653SPeter Holm	for i in `jot 5`; do
578a272653SPeter Holm		mksnap_ffs $1 $2 2>&1 | grep -v "Resource temporarily unavailable"
588a272653SPeter Holm		[ ! -s $2 ] && rm -f $2	|| return 0
598a272653SPeter Holm		sleep 1
608a272653SPeter Holm	done
618a272653SPeter Holm	return 1
628a272653SPeter Holm}
638a272653SPeter Holm
648a272653SPeter HolmD=$diskimage
658a272653SPeter Holm
668a272653SPeter Holmif [ -n "`find $D -mtime -1h 2>/dev/null`" ]; then
678a272653SPeter Holm	# FS left by previous crash
688a272653SPeter Holm	mdconfig -a -t vnode -f $D -u $mdstart
69*608c97bfSPeter Holm	fsck -t ufs -y md$mdstart
70*608c97bfSPeter Holm	fsck -t ufs -y md$mdstart
718a272653SPeter Holm	rm -f $D
728a272653SPeter Holm	exit 0
738a272653SPeter Holmfi
748a272653SPeter Holm
758a272653SPeter Holmhere=`pwd`
768a272653SPeter Holmcd /tmp
778a272653SPeter Holmsed '1,/^EOF/d' < $here/$0 > suj22.c
788a272653SPeter Holmmycc -o suj22 -Wall -Wextra -O2 suj22.c
798a272653SPeter Holmrm -f suj22.c
808a272653SPeter Holm
818a272653SPeter Holmmount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
828a272653SPeter Holmmdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
838a272653SPeter Holm
848a272653SPeter Holmdd if=/dev/zero of=$D bs=1m count=1k status=none || exit 1
858a272653SPeter Holmmdconfig -a -t vnode -f $D -u $mdstart
86*608c97bfSPeter Holmnewfs -j md$mdstart > /dev/null
87*608c97bfSPeter Holmmount /dev/md$mdstart $mntpoint
888a272653SPeter Holm
898a272653SPeter Holmcd $mntpoint
908a272653SPeter Holmchmod 777 $mntpoint
918a272653SPeter Holm/tmp/suj22
928a272653SPeter Holmsnap $mntpoint $mntpoint/.snap/snap1
938a272653SPeter Holm/tmp/suj22 prune
948a272653SPeter Holmsnap $mntpoint $mntpoint/.snap/snap2
958a272653SPeter Holm/tmp/suj22
968a272653SPeter Holmfor i in `jot 10`; do
978a272653SPeter Holm	/tmp/suj22 prune
988a272653SPeter Holm	/tmp/suj22
998a272653SPeter Holm	snap $mntpoint $mntpoint/.snap/snap$((i + 2)) || break
1008a272653SPeter Holm	sn=`ls -tU $mntpoint/.snap | tail -1`
1018a272653SPeter Holm	rm -f $mntpoint/.snap/$sn
1028a272653SPeter Holmdone
1038a272653SPeter Holmcd $here
1048a272653SPeter Holm
1058a272653SPeter Holmwhile mount | grep -q $mntpoint; do
1068a272653SPeter Holm	umount $mntpoint || sleep 1
1078a272653SPeter Holmdone
108*608c97bfSPeter Holmfsck -t ufs -y md$mdstart
109*608c97bfSPeter Holmfsck -t ufs -y md$mdstart
1108a272653SPeter Holmmdconfig -d -u $mdstart
1118a272653SPeter Holmrm -f /tmp/suj22 $D
1128a272653SPeter Holmexit 0
1138a272653SPeter HolmEOF
1148a272653SPeter Holm#include <sys/types.h>
1158a272653SPeter Holm#include <err.h>
1168a272653SPeter Holm#include <errno.h>
1178a272653SPeter Holm#include <fcntl.h>
1188a272653SPeter Holm#include <stdio.h>
1198a272653SPeter Holm#include <stdlib.h>
1208a272653SPeter Holm#include <sys/stat.h>
1218a272653SPeter Holm#include <unistd.h>
1228a272653SPeter Holm
1238a272653SPeter Holmstatic char buf[4096];
1248a272653SPeter Holm#define ND 100
1258a272653SPeter Holm#define NF 100
1268a272653SPeter Holm
1278a272653SPeter Holmvoid
1288a272653SPeter Holmsetup(void)
1298a272653SPeter Holm{
1308a272653SPeter Holm	int d, f, fd, i, n;
1318a272653SPeter Holm	char name[128];
1328a272653SPeter Holm
1338a272653SPeter Holm	for (d = 0; d < ND; d++) {
1348a272653SPeter Holm		snprintf(name, sizeof(name), "d%03d", d);
1358a272653SPeter Holm		if (mkdir(name, 00700) == -1 && errno != EEXIST)
1368a272653SPeter Holm			err(1, "mkdir(%s)", name);
1378a272653SPeter Holm		if (chdir(name) == -1)
1388a272653SPeter Holm			err(1, "chdir(%s)", name);
1398a272653SPeter Holm		for (f = 0; f < NF; f++) {
1408a272653SPeter Holm			if (arc4random() % 100 < 33)
1418a272653SPeter Holm				continue;
1428a272653SPeter Holm			snprintf(name, sizeof(name), "f%03d", f);
1438a272653SPeter Holm			if ((fd = open(name, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
1448a272653SPeter Holm				err(1, "open(%s)", name);
1458a272653SPeter Holm			n = arc4random() % 10;
1468a272653SPeter Holm			for (i = 0; i < n; i++) {
1478a272653SPeter Holm				if (write(fd, buf, sizeof(buf)) != sizeof(buf))
1488a272653SPeter Holm					err(1, "write()");
1498a272653SPeter Holm			}
1508a272653SPeter Holm			close(fd);
1518a272653SPeter Holm		}
1528a272653SPeter Holm		if (chdir("..") == -1)
1538a272653SPeter Holm			err(1, "chdir(%s)", "..");
1548a272653SPeter Holm	}
1558a272653SPeter Holm}
1568a272653SPeter Holmvoid
1578a272653SPeter Holm
1588a272653SPeter Holmprune(void)
1598a272653SPeter Holm{
1608a272653SPeter Holm	int d, f;
1618a272653SPeter Holm	char name[128];
1628a272653SPeter Holm
1638a272653SPeter Holm	for (d = 0; d < ND; d++) {
1648a272653SPeter Holm		snprintf(name, sizeof(name), "d%03d", d);
1658a272653SPeter Holm		if (chdir(name) == -1)
1668a272653SPeter Holm			err(1, "chdir(%s)", name);
1678a272653SPeter Holm		for (f = 0; f < NF; f++) {
1688a272653SPeter Holm			if (arc4random() % 100 < 33)
1698a272653SPeter Holm				continue;
1708a272653SPeter Holm			snprintf(name, sizeof(name), "f%03d", f);
1718a272653SPeter Holm			if (unlink(name) == -1 && errno != ENOENT)
1728a272653SPeter Holm				err(1, "unlink(%s)", name);
1738a272653SPeter Holm		}
1748a272653SPeter Holm		if (chdir("..") == -1)
1758a272653SPeter Holm			err(1, "chdir(%s)", "..");
1768a272653SPeter Holm	}
1778a272653SPeter Holm	for (d = 0; d < ND; d++) {
1788a272653SPeter Holm		if (arc4random() % 100 > 10)
1798a272653SPeter Holm			continue;
1808a272653SPeter Holm		snprintf(name, sizeof(name), "rm -rf d%03d", d);
1818a272653SPeter Holm		system(name);
1828a272653SPeter Holm	}
1838a272653SPeter Holm}
1848a272653SPeter Holm
1858a272653SPeter Holmint
1868a272653SPeter Holmmain(int argc, char **argv __unused)
1878a272653SPeter Holm{
1888a272653SPeter Holm	if (argc == 1)
1898a272653SPeter Holm		setup();
1908a272653SPeter Holm	if (argc == 2)
1918a272653SPeter Holm		prune();
1928a272653SPeter Holm
1938a272653SPeter Holm	return (0);
1948a272653SPeter Holm}
195