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 2007 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 unshare [-a] <filesystem|mountpoint>' is aware of legacy share.
37#
38# STRATEGY:
39# 1. Set 'zfs set sharenfs=off'
40# 2. Use 'share' to share given filesystem
41# 3. Verify that 'zfs unshare <filesystem|mountpoint>' is aware of legacy share
42# 4. Verify that 'zfs unshare -a' is aware of legacy share.
43#
44
45verify_runnable "global"
46
47function cleanup
48{
49	typeset -i i=0
50	while (( i < ${#mntp_fs[*]} )); do
51		is_shared ${mntp_fs[i]} && \
52			log_must unshare -F nfs ${mntp_fs[i]}
53
54		((i = i + 2))
55	done
56
57	if mounted $TESTPOOL/$TESTCLONE; then
58		log_must zfs unmount $TESTDIR2
59	fi
60
61	[[ -d $TESTDIR2 ]] && \
62		log_must rm -rf $TESTDIR2
63
64	if datasetexists "$TESTPOOL/$TESTCLONE"; then
65		log_must zfs destroy -f $TESTPOOL/$TESTCLONE
66	fi
67
68	if snapexists "$TESTPOOL/$TESTFS2@snapshot"; then
69		log_must zfs destroy -f $TESTPOOL/$TESTFS2@snapshot
70	fi
71
72	if datasetexists "$TESTPOOL/$TESTFS2"; then
73		log_must zfs destroy -f $TESTPOOL/$TESTFS2
74	fi
75}
76
77#
78# Main test routine.
79#
80# Given a mountpoint and file system this routine will attempt
81# to verify 'zfs unshare' is aware of legacy share.
82#
83function test_legacy_unshare # <mntp> <filesystem>
84{
85        typeset mntp=$1
86        typeset filesystem=$2
87
88	log_must zfs set sharenfs=off $filesystem
89	not_shared $mntp || \
90	    log_fail "'zfs set sharenfs=off' fails to make ZFS " \
91	    "filesystem $filesystem unshared."
92
93	log_must share -F nfs $mntp
94	is_shared $mntp || \
95	    log_fail "'share' command fails to share ZFS file system."
96	#
97	# Verify 'zfs unshare <filesystem>' is aware of legacy share.
98	#
99	log_mustnot zfs unshare $filesystem
100	is_shared $mntp || \
101	    log_fail "'zfs unshare <filesystem>' fails to be aware" \
102	    "of legacy share."
103
104	#
105	# Verify 'zfs unshare <filesystem>' is aware of legacy share.
106	#
107	log_mustnot zfs unshare $mntp
108	is_shared $mntp || \
109	    log_fail "'zfs unshare <mountpoint>' fails to be aware" \
110	    "of legacy share."
111}
112
113
114set -A mntp_fs \
115    "$TESTDIR" "$TESTPOOL/$TESTFS" \
116    "$TESTDIR1" "$TESTPOOL/$TESTCTR/$TESTFS1" \
117    "$TESTDIR2" "$TESTPOOL/$TESTCLONE"
118
119log_assert "Verify that 'zfs unshare [-a]' is aware of legacy share."
120log_onexit cleanup
121
122log_must zfs create $TESTPOOL/$TESTFS2
123log_must zfs snapshot $TESTPOOL/$TESTFS2@snapshot
124log_must zfs clone $TESTPOOL/$TESTFS2@snapshot $TESTPOOL/$TESTCLONE
125log_must zfs set mountpoint=$TESTDIR2 $TESTPOOL/$TESTCLONE
126
127#
128# Invoke 'test_legacy_unshare' routine to verify.
129#
130typeset -i i=0
131while (( i < ${#mntp_fs[*]} )); do
132	test_legacy_unshare ${mntp_fs[i]} ${mntp_fs[((i + 1 ))]}
133
134	((i = i + 2))
135done
136
137
138log_note "Verify 'zfs unshare -a' is aware of legacy share."
139
140#
141# set the 'sharenfs' property to 'off' for each filesystem
142#
143i=0
144while (( i < ${#mntp_fs[*]} )); do
145        log_must zfs set sharenfs=off ${mntp_fs[((i + 1))]}
146        not_shared ${mntp_fs[i]} || \
147                log_fail "'zfs set sharenfs=off' unshares file system failed."
148
149        ((i = i + 2))
150done
151
152#
153# Share each of the file systems via legacy share.
154#
155i=0
156while (( i < ${#mntp_fs[*]} )); do
157        share -F nfs ${mntp_fs[i]}
158        is_shared ${mntp_fs[i]} || \
159                log_fail "'share' shares ZFS filesystem failed."
160
161        ((i = i + 2))
162done
163
164#
165# Verify that 'zfs unshare -a' is aware of legacy share
166#
167log_must zfs unshare -a
168
169#
170# verify ZFS filesystems are still shared
171#
172i=0
173while (( i < ${#mntp_fs[*]} )); do
174        is_shared ${mntp_fs[i]} || \
175            log_fail "'zfs  unshare -a' fails to be aware of legacy share."
176
177        ((i = i + 2))
178done
179
180log_pass "'zfs unshare [-a]' succeeds to be aware of legacy share."
181
182