1272343Sngie# $NetBSD: t_umountstress.sh,v 1.5 2013/05/31 14:40:48 gson Exp $
2272343Sngie#
3272343Sngie# Copyright (c) 2013 The NetBSD Foundation, Inc.
4272343Sngie# All rights reserved.
5272343Sngie#
6272343Sngie# Redistribution and use in source and binary forms, with or without
7272343Sngie# modification, are permitted provided that the following conditions
8272343Sngie# are met:
9272343Sngie# 1. Redistributions of source code must retain the above copyright
10272343Sngie#    notice, this list of conditions and the following disclaimer.
11272343Sngie# 2. Redistributions in binary form must reproduce the above copyright
12272343Sngie#    notice, this list of conditions and the following disclaimer in the
13272343Sngie#    documentation and/or other materials provided with the distribution.
14272343Sngie#
15272343Sngie# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16272343Sngie# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17272343Sngie# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18272343Sngie# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19272343Sngie# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20272343Sngie# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21272343Sngie# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22272343Sngie# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23272343Sngie# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24272343Sngie# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25272343Sngie# POSSIBILITY OF SUCH DAMAGE.
26272343Sngie#
27272343Sngie
28272343SngieTMPMP=umount-stress_mount
29272343SngieTMPIM=umount-stress.im
30272343Sngie
31272343SngieVND=vnd0
32272343SngieBVND=/dev/${VND}
33272343SngieCVND=/dev/r${VND}
34272343SngieMPART=a
35272343Sngie
36272343Sngieatf_test_case fileop cleanup
37272343Sngiefileop_head()
38272343Sngie{
39272343Sngie	atf_set "descr" "Checks unmounting a filesystem doing file operations"
40272343Sngie	atf_set "require.user" "root"
41272343Sngie}
42272343Sngiefileop_body()
43272343Sngie{
44272343Sngie	cat >disktab <<EOF
45272343Sngiefloppy288|2.88MB 3.5in Extra High Density Floppy:\
46272343Sngie	:ty=floppy:se#512:nt#2:rm#300:ns#36:nc#80:\
47272343Sngie	:pa#5760:oa#0:ba#4096:fa#512:ta=4.2BSD:\
48272343Sngie	:pb#5760:ob#0:\
49272343Sngie	:pc#5760:oc#0:
50272343SngieEOF
51272343Sngie
52272343Sngie	echo "*** Creating a dummy directory tree at" \
53272343Sngie	     "${TMPMP} mounted on ${TMPIM}"
54272343Sngie
55272343Sngie	atf_check -o ignore -e ignore mkdir ${TMPMP}
56272343Sngie	atf_check -o ignore -e ignore dd if=/dev/zero of=${TMPIM} count=5860
57272343Sngie	atf_check -o ignore -e ignore vnconfig -v ${VND} ${TMPIM}
58272343Sngie	atf_check -o ignore -e ignore disklabel -f disktab -rw ${VND} floppy288
59272343Sngie	atf_check -o ignore -e ignore newfs -i 500 -b 8192 -f 1024 ${CVND}${MPART}
60272343Sngie	atf_check -o ignore -e ignore mount -o async ${BVND}${MPART} ${TMPMP}
61272343Sngie
62272343Sngie	echo "*** Testing fileops"
63272343Sngie
64272343Sngie	touch ${TMPMP}/hold
65272343Sngie	exec 9< ${TMPMP}/hold
66272343Sngie
67272343Sngie	(
68272343Sngie		for j in 0 1 2; do
69272343Sngie		for k in 0 1 2 3 4 5 6 7 8 9; do
70272343Sngie			if ! dd msgfmt=quiet if=/dev/zero \
71272343Sngie				count=1 of=${TMPMP}/test$i$j$k; then
72272343Sngie				echo 1 >result
73272343Sngie				exit
74272343Sngie			fi
75272343Sngie		done
76272343Sngie		done
77272343Sngie		echo 0 >result
78272343Sngie	) &
79272343Sngie	busypid=$!
80272343Sngie
81272343Sngie	while ! test -f result; do
82272343Sngie		if err=$(umount ${TMPMP} 2>&1); then
83272343Sngie			kill $busypid
84272343Sngie			exec 9<&-
85272343Sngie			wait
86272343Sngie			atf_fail "Unmount succeeded while busy"
87272343Sngie			return
88272343Sngie		fi
89272343Sngie
90272343Sngie		case $err in
91272343Sngie		*:\ Device\ busy)
92272343Sngie			;;
93272343Sngie		*)
94272343Sngie			kill $busypid
95272343Sngie			exec 9<&-
96272343Sngie			wait
97272343Sngie			atf_fail "Unmount failed: $err"
98272343Sngie			return
99272343Sngie			;;
100272343Sngie		esac
101272343Sngie	done
102272343Sngie
103272343Sngie	exec 9<&-
104272343Sngie	wait
105272343Sngie
106272343Sngie	rc=`cat result`
107272343Sngie	rm -f result
108272343Sngie
109272343Sngie	case $rc in
110272343Sngie	0) ;;
111272343Sngie	*) atf_fail "File operation failed"
112272343Sngie	esac
113272343Sngie}
114272343Sngiefileop_cleanup()
115272343Sngie{
116272343Sngie	echo "*** Cleaning up ${TMPMP}, ${TMPIM}."
117272343Sngie	umount -f "${TMPMP}"
118272343Sngie	vnconfig -u "${VND}"
119272343Sngie}
120272343Sngie
121272343Sngieatf_test_case mountlist cleanup
122272343Sngiemountlist_head()
123272343Sngie{
124272343Sngie	atf_set "descr" "Checks unmounting a filesystem using mountlist"
125272343Sngie	atf_set "require.user" "root"
126272343Sngie}
127272343Sngiemountlist_body()
128272343Sngie{
129272343Sngie	cat >disktab <<EOF
130272343Sngiefloppy288|2.88MB 3.5in Extra High Density Floppy:\
131272343Sngie	:ty=floppy:se#512:nt#2:rm#300:ns#36:nc#80:\
132272343Sngie	:pa#5760:oa#0:ba#4096:fa#512:ta=4.2BSD:\
133272343Sngie	:pb#5760:ob#0:\
134272343Sngie	:pc#5760:oc#0:
135272343SngieEOF
136272343Sngie
137272343Sngie	echo "*** Creating a dummy directory tree at" \
138272343Sngie	     "${TMPMP} mounted on ${TMPIM}"
139272343Sngie
140272343Sngie	atf_check -o ignore -e ignore mkdir ${TMPMP}
141272343Sngie	atf_check -o ignore -e ignore dd if=/dev/zero of=${TMPIM} count=5860
142272343Sngie	atf_check -o ignore -e ignore vnconfig -v ${VND} ${TMPIM}
143272343Sngie	atf_check -o ignore -e ignore disklabel -f disktab -rw ${VND} floppy288
144272343Sngie	atf_check -o ignore -e ignore newfs -i 500 -b 8192 -f 1024 ${CVND}${MPART}
145272343Sngie	atf_check -o ignore -e ignore mount -o async ${BVND}${MPART} ${TMPMP}
146272343Sngie
147272343Sngie	echo "*** Testing mountlist"
148272343Sngie
149272343Sngie	(
150272343Sngie		for j in 0 1 2 3 4 5 6 7 8 9; do
151272343Sngie		for k in 0 1 2 3 4 5 6 7 8 9; do
152272343Sngie			if ! out=$(mount); then
153272343Sngie				echo 1 >result
154272343Sngie				exit
155272343Sngie			fi
156272343Sngie		done
157272343Sngie		done
158272343Sngie		echo 0 >result
159272343Sngie	) &
160272343Sngie	busypid=$!
161272343Sngie
162272343Sngie	while ! test -f result; do
163272343Sngie		if err=$(umount ${TMPMP} 2>&1); then
164272343Sngie			if ! mount -o async ${BVND}${MPART} ${TMPMP}; then
165272343Sngie				kill $busypid
166272343Sngie				exec 9<&-
167272343Sngie				wait
168272343Sngie				atf_fail "Remount failed"
169272343Sngie				return
170272343Sngie			fi
171272343Sngie			continue
172272343Sngie		fi
173272343Sngie
174272343Sngie		case $err in
175272343Sngie		*:\ Device\ busy)
176272343Sngie			;;
177272343Sngie		*)
178272343Sngie			kill $busypid
179272343Sngie			exec 9<&-
180272343Sngie			wait
181272343Sngie			atf_fail "Unmount failed: $err"
182272343Sngie			return
183272343Sngie			;;
184272343Sngie		esac
185272343Sngie	done
186272343Sngie
187272343Sngie	exec 9<&-
188272343Sngie	wait
189272343Sngie
190272343Sngie	rc=`cat result`
191272343Sngie	rm -f result
192272343Sngie
193272343Sngie	case $rc in
194272343Sngie	0) ;;
195272343Sngie	*) atf_fail "Mountlist operation failed"
196272343Sngie	esac
197272343Sngie}
198272343Sngiemountlist_cleanup()
199272343Sngie{
200272343Sngie	echo "*** Cleaning up ${TMPMP}, ${TMPIM}."
201272343Sngie	umount -f "${TMPMP}"
202272343Sngie	vnconfig -u "${VND}"
203272343Sngie}
204272343Sngie
205272343Sngieatf_init_test_cases()
206272343Sngie{
207272343Sngie	atf_add_test_case fileop
208272343Sngie	atf_add_test_case mountlist
209272343Sngie}
210