aboutsummaryrefslogtreecommitdiff
path: root/tools/test/stress2/misc/vunref.sh
blob: f08035a95694c41ff6180905ae59b9012a1d90d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/bin/sh

#
# Copyright (c) 2010 Peter Holm <pho@FreeBSD.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

# Vnode reference leak test scenario by kib@.
# Will fail with  "umount: unmount of /mnt5 failed: Device busy"
# vnode leak not seen on HEAD.

[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1

. ../default.cfg
here=`pwd`
mounts=2		# Number of parallel scripts
D=$diskimage

[ -d "$RUNDIR" ] || mkdir $RUNDIR
cd $RUNDIR

if [ $# -eq 0 ]; then
	sed '1,/^EOF/d' < $here/$0 > vunref.c
	mycc -o /tmp/vunref -Wall -Wextra -O2 vunref.c
	rm -f vunref.c
	cd $here

	rm -f $RUNDIR/active.*
	for i in `jot $mounts`; do
		m=$(( i + mdstart - 1 ))
		[ ! -d ${mntpoint}$m ] && mkdir ${mntpoint}$m
		mount | grep "$mntpoint" | grep -q md$m && umount -f ${mntpoint}$m
		mdconfig -l | grep -q md$m && mdconfig -d -u $m

		dd if=/dev/zero of=$D.$m bs=1m count=10 status=none
		mdconfig -a -t vnode -f $D.$m -u $m
		newfs md${m} > /dev/null 2>&1
	done

	# start the parallel tests
	for i in `jot $mounts`; do
		m=$((i + mdstart - 1))
		$0 mmap $m &
		sleep 0.2
		$0 $m &
	done

	sleep 2

	while [ ! -z "`ls $RUNDIR/active.* 2>/dev/null`" ] ; do
		../testcases/swap/swap -t 2m -i 20
	done
	wait

	for i in `jot $mounts`; do
		m=$((i + mdstart - 1))
		mdconfig -d -u $m
		rm -f $D$m
	done
	rm -f /tmp/vunref $RUNDIR/active.* $diskimage.* ${mntpoint}*/p*
else
	if [ $1 = mmap ]; then
		touch $RUNDIR/active.$2
		start=`date +%s`
		while [ $((`date +%s`- start)) -lt 300 ]; do
			cd ${mntpoint}$2
			/tmp/vunref > /dev/null 2>&1
			cd /
			[ -f $RUNDIR/active.$2 ] || exit
			sleep 0.1
		done
		rm -f $RUNDIR/active.$2
	else
		# The test: Parallel mount and unmounts
		m=$1
		mount $opt /dev/md${m} ${mntpoint}$m
		while [ -f $RUNDIR/active.$m ] ; do
			sleep 0.1
			n=0
			while mount | grep -qw $mntpoint$m; do
				umount ${mntpoint}$m > /dev/null 2>&1 && n=0
				if [ $((n += 1)) -gt 600 ]; then
					echo "*** Leak detected ***"
					fstat $mntpoint$m
					rm -f $RUNDIR/active.*
					exit 1
				fi
				sleep 0.1
			done
			mount $opt /dev/md${m} ${mntpoint}$m
		done
		mount | grep "$mntpoint" | grep -q md$m && umount ${mntpoint}$m
	fi
fi
exit
EOF
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define INPUTFILE "/bin/date"

int
test(void)
{
	int i;
	pid_t pid;
	char file[128];
	int fdin, fdout;
	char *src, *dst;
	struct stat statbuf;

	pid = getpid();
	for (i = 0; i < 100; i++) {
		sprintf(file,"p%05d.%05d", pid, i);

		if ((fdin = open(INPUTFILE, O_RDONLY)) < 0)
			err(1, INPUTFILE);

		if ((fdout = open(file, O_RDWR | O_CREAT | O_TRUNC, 0600)) < 0)
			err(1, "%s", file);

		if (fstat(fdin, &statbuf) < 0)
			err(1, "fstat error");

		if (lseek(fdout, statbuf.st_size - 1, SEEK_SET) == -1)
			err(1, "lseek error");

		/* write a dummy byte at the last location */
		if (write(fdout, "", 1) != 1)
			err(1, "write error");

		if ((src = mmap(0, statbuf.st_size, PROT_READ, MAP_SHARED, fdin, 0)) ==
			(caddr_t) - 1)
			err(1, "mmap error for input");

		if ((dst = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE,
			MAP_SHARED, fdout, 0)) == (caddr_t) - 1)
			err(1, "mmap error for output");

		memcpy(dst, src, statbuf.st_size);

		if (munmap(src, statbuf.st_size) == -1)
			err(1, "munmap");
		close(fdin);

		if (munmap(dst, statbuf.st_size) == -1)
			err(1, "munmap");
		close(fdout);

		if (unlink(file) == -1)
			err(3, "unlink(%s)", file);
	}

	return (0);
}

int
main()
{
	int i;
	char path[MAXPATHLEN+1];
	struct statfs buf;

	if (getcwd(path, sizeof(path)) == NULL)
		err(1, "getcwd()");

	if (statfs(path, &buf) < 0)
		err(1, "statfs(%s)", path);

	if (!strcmp(buf.f_mntonname, "/"))
			return (1);

        for (i = 0; i < 2; i++) {
                if (fork() == 0)
                        test();
        }
        for (i = 0; i < 2; i++)
                wait(NULL);

        return (0);
}