xref: /freebsd/tools/test/stress2/misc/suj20.sh (revision 716fd348)
1#!/bin/sh
2
3#
4# Copyright (c) 2011 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# Looping mksnap_ffs seen.
30
31[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
32
33. ../default.cfg
34
35# Scenario by mckusick@
36#
37# create a bunch of files/directories
38# create a snapshot
39# remove many (but not all) of those files/directories
40# create some new files/directories in what remains of those
41#     original files/directories.
42# create another snapshot
43# repeat {
44#         remove many (somewhat different) of those files/directories
45#         create some new files/directories in what remains of those
46#             remaining files/directories.
47#         create a new snapshot
48#         remove oldest snapshot
49# }
50
51snap () {
52	for i in `jot 5`; do
53		mksnap_ffs $1 $2 2>&1 | grep -v "Resource temporarily unavailable"
54		[ ! -s $2 ] && rm -f $2	|| return 0
55		sleep 1
56	done
57	return 1
58}
59
60here=`pwd`
61cd /tmp
62sed '1,/^EOF/d' < $here/$0 > suj20.c
63mycc -o suj20 -Wall -Wextra -g -O2 suj20.c
64rm -f suj20.c
65
66mount | grep "$mntpoint" | grep -q md$mdstart && umount $mntpoint
67mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
68
69mdconfig -a -t swap -s 1g -u $mdstart
70bsdlabel -w md$mdstart auto
71newfs -j md${mdstart}$part > /dev/null
72mount /dev/md${mdstart}$part $mntpoint
73
74cd $mntpoint
75chmod 777 $mntpoint
76/tmp/suj20
77snap $mntpoint $mntpoint/.snap/snap1
78/tmp/suj20 prune
79snap $mntpoint $mntpoint/.snap/snap2
80/tmp/suj20
81for i in `jot 10`; do
82	/tmp/suj20 prune
83	/tmp/suj20
84	snap $mntpoint $mntpoint/.snap/snap$((i + 2))
85	sn=`ls -tU $mntpoint/.snap | tail -1`
86	rm -f $mntpoint/.snap/$sn
87done
88cd $here
89
90while mount | grep -q $mntpoint; do
91	umount $mntpoint || sleep 1
92done
93mdconfig -d -u $mdstart
94rm -f /tmp/suj20
95exit 0
96EOF
97#include <sys/types.h>
98#include <err.h>
99#include <errno.h>
100#include <fcntl.h>
101#include <stdio.h>
102#include <stdlib.h>
103#include <sys/stat.h>
104#include <unistd.h>
105
106static char buf[4096];
107#define ND 100
108#define NF 100
109
110void
111setup(void)
112{
113	int d, f, fd, i, n;
114	char name[128];
115
116	for (d = 0; d < ND; d++) {
117		snprintf(name, sizeof(name), "d%03d", d);
118		if (mkdir(name, 00700) == -1 && errno != EEXIST)
119			err(1, "mkdir(%s)", name);
120		if (chdir(name) == -1)
121			err(1, "chdir(%s)", name);
122		for (f = 0; f < NF; f++) {
123			if (arc4random() % 100 < 33)
124				continue;
125			snprintf(name, sizeof(name), "f%03d", f);
126			if ((fd = open(name, O_RDWR | O_CREAT | O_TRUNC, 0640)) == -1)
127				err(1, "open(%s)", name);
128			n = arc4random() % 10;
129			for (i = 0; i < n; i++) {
130				if (write(fd, buf, sizeof(buf)) != sizeof(buf))
131					err(1, "write()");
132			}
133			close(fd);
134		}
135		if (chdir("..") == -1)
136			err(1, "chdir(%s)", "..");
137	}
138}
139void
140
141prune(void)
142{
143	int d, f;
144	char name[128];
145
146	for (d = 0; d < ND; d++) {
147		snprintf(name, sizeof(name), "d%03d", d);
148		if (chdir(name) == -1)
149			err(1, "chdir(%s)", name);
150		for (f = 0; f < NF; f++) {
151			if (arc4random() % 100 < 33)
152				continue;
153			snprintf(name, sizeof(name), "f%03d", f);
154			if (unlink(name) == -1 && errno != ENOENT)
155				err(1, "unlink(%s)", name);
156		}
157		if (chdir("..") == -1)
158			err(1, "chdir(%s)", "..");
159	}
160	for (d = 0; d < ND; d++) {
161		if (arc4random() % 100 > 10)
162			continue;
163		snprintf(name, sizeof(name), "rm -rf d%03d", d);
164		system(name);
165	}
166}
167
168int
169main(int argc, char **argv __unused)
170{
171	if (argc == 1)
172		setup();
173	if (argc == 2)
174		prune();
175
176	return (0);
177}
178