1#! /usr/local/bin/ksh93 -p
2#
3# CDDL HEADER START
4#
5# The contents of this file are subject to the terms of the
6# Common Development and Distribution License (the "License").
7# You may not use this file except in compliance with the License.
8#
9# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10# or http://www.opensolaris.org/os/licensing.
11# See the License for the specific language governing permissions
12# and limitations under the License.
13#
14# When distributing Covered Code, include this CDDL HEADER in each
15# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16# If applicable, add the following below this CDDL HEADER, with the
17# fields enclosed by brackets "[]" replaced with your own identifying
18# information: Portions Copyright [yyyy] [name of copyright owner]
19#
20# CDDL HEADER END
21#
22
23#
24# Copyright 2013 Spectra Logic.  All rights reserved.
25# Use is subject to license terms.
26#
27#
28. $STF_SUITE/include/libtest.kshlib
29
30################################################################################
31#
32# __stc_assertion_start
33#
34# ID: snapshot_019_pos
35#
36# DESCRIPTION:
37#	Accessing snapshots and unmounting them in parallel does not panic.
38#	FreeBSD PR kern/184677
39#
40# STRATEGY:
41#	1. Create a dataset
42#	2. Set the snapdir property to visible
43#	3. Do the following in parallel
44#	   a. Repeatedly access the snapshot
45#	   b. Repeatedly unmount the snapshot
46#	4. Verify that the system does not panic
47#
48# TESTABILITY: explicit
49#
50# TEST_AUTOMATION_LEVEL: automated
51#
52# CODING_STATUS: COMPLETED (2013-12-23)
53#
54# __stc_assertion_end
55#
56################################################################################
57
58verify_runnable "both"
59
60KILL_SWITCH=${PWD}/kill_switch
61
62function stat_snapshot
63{
64	while [ ! -f ${KILL_SWITCH} ]; do
65		cd $SNAPDIR # Exercise forced unmount
66		stat "$SNAPDIR" > /dev/null 2>&1
67	done
68}
69
70function ls_snapshot
71{
72	# Pre-generate the argument list.
73	ls_args=""
74	for ((num=0; $num<100; num=$num+1)); do
75		ls_args="$ls_args $SNAPDIR/.."
76	done
77
78	while [ ! -f ${KILL_SWITCH} ]; do
79		ls $ls_args >/dev/null 2>&1
80	done
81}
82
83log_assert "Accessing snapshots and unmounting them in parallel does not panic"
84
85log_must dataset_setprop $TESTPOOL "snapdir" "visible"
86
87# Take snapshots
88log_must $ZFS snapshot "$TESTPOOL/$TESTFS@$TESTSNAP"
89
90# Repeatedly access the snapshot directory
91stat_snapshot &
92stat_pid="$!"
93ls_snapshot &
94ls_pid="$!"
95
96# Repeatedly unmount the snapshot directory
97for ((i=0; $i<100; i=$i+1)); do
98	umount "$SNAPDIR" >/dev/null 2>&1
99	log_note "$i non-forced done"
100	# Sleep just long enough for the other "threads" to remount.
101	sleep 0.1
102	umount -f "$SNAPDIR" >/dev/null 2>&1
103	log_note "$i forced done"
104	sleep 0.1
105done
106
107# Kill the other "threads" and wait for them to die.
108touch $KILL_SWITCH
109log_note "Waiting for all child processes to die..."
110wait
111
112# Test that no reference leaks occurred and we can cleanup without forcing.
113log_must $ZFS unmount $TESTPOOL/$TESTFS
114log_must $ZFS destroy -r $TESTPOOL/$TESTFS
115
116# If we get here, we managed to not panic, deadlock, or leak references.
117log_pass
118