xref: /freebsd/tools/test/stress2/misc/umountf7.sh (revision fe6060f10f634930ff71b7c50291ddc610da2475)
1#!/bin/sh
2
3#
4# Copyright (c) 2013 EMC Corp.
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# "panic: handle_written_inodeblock: live inodedep 0xcc731200" seen.
30# http://people.freebsd.org/~pho/stress/log/umountf7.txt
31# https://people.freebsd.org/~pho/stress/log/kostik824.txt
32# Problem only seen with SU+J.
33
34[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
35
36. ../default.cfg
37
38dir=/tmp
39odir=`pwd`
40cd $dir
41sed '1,/^EOF/d' < $odir/$0 > $dir/umountf7.c
42mycc -o umountf7  -Wall -Wextra umountf7.c -lpthread || exit 1
43rm -f umountf7.c
44cd $odir
45
46mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
47mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
48mdconfig -a -t swap -s 3g -u $mdstart || exit 1
49bsdlabel -w md$mdstart auto
50[ "$newfs_flags" = "-U" ] && opt="-j"
51newfs $opt md${mdstart}$part > /dev/null
52mount /dev/md${mdstart}$part $mntpoint
53
54daemon sh -c '(cd ../testcases/swap; ./swap -t 2m -i 4)'
55parallel=4
56for j in `jot $parallel`; do
57	[ -d $mntpoint/$j ] || mkdir $mntpoint/$j
58done
59for j in `jot $parallel`; do
60	(cd $mntpoint/$j; /tmp/umountf7 100000) &
61done
62sleep 30
63umount -f $mntpoint
64pkill umountf7
65wait
66while pkill -9 swap; do
67	:
68done
69find $mntpoint -type f
70
71while mount | grep "on $mntpoint " | grep -q /dev/md; do
72	umount $mntpoint || sleep 1
73done
74mdconfig -d -u $mdstart
75rm -f /tmp/umountf7
76exit
77
78EOF
79#include <err.h>
80#include <fcntl.h>
81#include <pthread.h>
82#include <stdio.h>
83#include <stdlib.h>
84#include <unistd.h>
85
86pid_t pid;
87volatile int n, n2;
88int mx;
89
90void *
91cr(void *arg __unused)
92{
93	char file[80];
94	int fd, i;
95
96	for (i = 0; i < mx; i++) {
97		snprintf(file, sizeof(file), "f%06d.%06d", pid, i);
98		if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
99			err(1, "open(%s)", file);
100		close(fd);
101		n++;
102	}
103        return (0);
104
105}
106
107void *
108mv(void *arg __unused)
109{
110	char from[80], to[80];
111	int i;
112
113	for (i = 0; i < mx; i++) {
114		while (n == -1 || i > n)
115			pthread_yield();
116		snprintf(from, sizeof(from), "f%06d.%06d", pid, i);
117		snprintf(to  , sizeof(to  ), "g%06d.%06d", pid, i);
118		if (rename(from, to) == -1)
119			warn("rename(%s, %s)", from, to);
120		n2++;
121	}
122
123        return (0);
124}
125
126void *
127rm(void *arg __unused)
128{
129	char file[80];
130	int i;
131
132	for (i = 0; i < mx; i++) {
133		while (n2 == -1 || i > n2)
134			pthread_yield();
135		snprintf(file, sizeof(file), "g%06d.%06d", pid, i);
136		if (unlink(file) == -1)
137			warn("unlink(%s)", file);
138	}
139
140        return (0);
141}
142
143int
144main(int argc, char **argv)
145{
146        pthread_t rp[3];
147	int e, i;
148
149	if (argc != 2)
150		errx(1, "Usage: %s <number of files>", argv[0]);
151	mx = atoi(argv[1]);
152	n = n2 = -1;
153	pid = getpid();
154
155	if ((e = pthread_create(&rp[0], NULL, cr, NULL)) != 0)
156		errc(1, e, "pthread_create");
157	usleep(arc4random() % 1000);
158	if ((e = pthread_create(&rp[1], NULL, mv, NULL)) != 0)
159		errc(1, e, "pthread_mv");
160	usleep(arc4random() % 1000);
161	if ((e = pthread_create(&rp[2], NULL, rm, NULL)) != 0)
162		errc(1, e, "pthread_rm");
163
164        for (i = 0; i < 3; i++)
165                pthread_join(rp[i], NULL);
166
167	return (0);
168}
169