1#!/bin/ksh -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 https://opensource.org/licenses/CDDL-1.0.
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 2009 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26#
27
28#
29# Copyright (c) 2016 by Delphix. All rights reserved.
30#
31
32. $STF_SUITE/include/libtest.shlib
33
34#
35# DESCRIPTION:
36# Verify that zfs unmount and destroy in a snapshot directory will not cause error.
37#
38# STRATEGY:
39# 1. Create a file in a zfs filesystem, snapshot it and change directory to snapshot directory
40# 2. Verify that 'zfs unmount -a'  will fail and 'zfs unmount -fa' will succeed
41# 3. Verify 'ls' and 'cd /' will succeed
42# 4. 'zfs mount -a' and change directory to snapshot directory again
43# 5. Verify that zfs destroy snapshot will succeed
44# 6. Verify 'ls' and 'cd /' will succeed
45# 7. Create zfs filesystem, create a file, snapshot it and change to snapshot directory
46# 8. Verify that zpool destroy the pool will succeed
47# 9. Verify 'ls' 'cd /' 'zpool list' and etc will succeed
48#
49
50verify_runnable "both"
51
52function cleanup
53{
54	DISK=${DISKS%% *}
55
56	for fs in $TESTPOOL/$TESTFS $TESTPOOL ; do
57		typeset snap=$fs@$TESTSNAP
58		snapexists $snap && destroy_dataset $snap
59	done
60
61	if ! poolexists $TESTPOOL && is_global_zone; then
62		log_must zpool create $TESTPOOL $DISK
63	fi
64
65	if ! datasetexists $TESTPOOL/$TESTFS; then
66		log_must zfs create $TESTPOOL/$TESTFS
67		log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
68	fi
69}
70
71function restore_dataset
72{
73	if ! datasetexists $TESTPOOL/$TESTFS ; then
74		log_must zfs create $TESTPOOL/$TESTFS
75		log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
76		log_must cd $TESTDIR
77		echo hello > world
78		log_must zfs snapshot $TESTPOOL/$TESTFS@$TESTSNAP
79		log_must cd .zfs/snapshot/$TESTSNAP
80	fi
81}
82
83
84log_assert "zfs force unmount and destroy in snapshot directory will not cause error."
85log_onexit cleanup
86
87for fs in $TESTPOOL/$TESTFS $TESTPOOL ; do
88	typeset snap=$fs@$TESTSNAP
89	typeset mtpt=$(get_prop mountpoint $fs)
90
91	log_must cd $mtpt
92	echo hello > world
93	log_must zfs snapshot $snap
94	log_must cd .zfs/snapshot/$TESTSNAP
95
96	log_mustnot zfs unmount -a
97	if is_linux; then
98		log_mustnot zfs unmount -fa
99		log_must ls
100	else
101		log_must zfs unmount -fa
102		log_mustnot ls
103	fi
104	log_must cd /
105
106	log_must zfs mount -a
107	log_must cd $mtpt
108	log_must cd .zfs/snapshot/$TESTSNAP
109
110	if is_global_zone || [[ $fs != $TESTPOOL ]] ; then
111		if is_linux; then
112			log_mustnot zfs destroy -rf $fs
113			log_must ls
114		else
115			log_must zfs destroy -rf $fs
116			log_mustnot ls
117		fi
118		log_must cd /
119	fi
120
121	restore_dataset
122done
123
124if is_global_zone ; then
125	if is_linux; then
126		log_mustnot zpool destroy -f $TESTPOOL
127		log_must ls
128	else
129		log_must zpool destroy -f $TESTPOOL
130		log_mustnot ls
131	fi
132	log_must cd /
133fi
134
135log_must eval zfs list > /dev/null 2>&1
136log_must eval zpool list > /dev/null 2>&1
137log_must eval zpool status > /dev/null 2>&1
138zpool iostat > /dev/null 2>&1
139
140log_pass "zfs force unmount and destroy in snapshot directory will not cause error."
141