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 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 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		if snapexists $snap; then
59			log_must zfs destroy $snap
60		fi
61	done
62
63	if ! poolexists $TESTPOOL && is_global_zone; then
64		log_must zpool create $TESTPOOL $DISK
65	fi
66
67	if ! datasetexists $TESTPOOL/$TESTFS; then
68		log_must zfs create $TESTPOOL/$TESTFS
69		log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
70	fi
71}
72
73function restore_dataset
74{
75	if ! datasetexists $TESTPOOL/$TESTFS ; then
76		log_must zfs create $TESTPOOL/$TESTFS
77		log_must zfs set mountpoint=$TESTDIR $TESTPOOL/$TESTFS
78		log_must cd $TESTDIR
79		echo hello > world
80		log_must zfs snapshot $TESTPOOL/$TESTFS@$TESTSNAP
81		log_must cd .zfs/snapshot/$TESTSNAP
82	fi
83}
84
85
86log_assert "zfs fource unmount and destroy in snapshot directory will not cause error."
87log_onexit cleanup
88
89for fs in $TESTPOOL/$TESTFS $TESTPOOL ; do
90	typeset snap=$fs@$TESTSNAP
91	typeset mtpt=$(get_prop mountpoint $fs)
92
93	log_must cd $mtpt
94	echo hello > world
95	log_must zfs snapshot $snap
96	log_must cd .zfs/snapshot/$TESTSNAP
97
98	log_mustnot zfs unmount -a
99	log_must zfs unmount -fa
100	log_mustnot ls
101	log_must cd /
102
103	log_must zfs mount -a
104	log_must cd $mtpt
105	log_must cd .zfs/snapshot/$TESTSNAP
106
107	if is_global_zone || [[ $fs != $TESTPOOL ]] ; then
108		log_must zfs destroy -rf $fs
109		log_mustnot ls
110		log_must cd /
111	fi
112
113	restore_dataset
114done
115
116if is_global_zone ; then
117	log_must zpool destroy -f $TESTPOOL
118	log_mustnot ls
119	log_must cd /
120fi
121
122log_must eval zfs list > /dev/null 2>&1
123log_must eval zpool list > /dev/null 2>&1
124log_must eval zpool status > /dev/null 2>&1
125zpool iostat > /dev/null 2>&1
126
127log_pass "zfs fource unmount and destroy in snapshot directory will not cause error."
128