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