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 unmount should fail with bad parameters or scenarios:
37#	1. bad option;
38#	2. too many arguments;
39#	3. null arguments;
40#	4. invalid datasets;
41#	5. invalid mountpoint;
42#	6. already unmounted zfs filesystem;
43#	7. legacy mounted zfs filesystem
44#
45# STRATEGY:
46# 1. Make an array of bad parameters
47# 2. Use zfs unmount to unmount the filesystem
48# 3. Verify that zfs unmount returns error
49#
50
51verify_runnable "both"
52
53function cleanup
54{
55	for ds in $vol $fs1; do
56		if datasetexists $ds; then
57			log_must zfs destroy -f $ds
58		fi
59	done
60
61	if snapexists $snap; then
62		log_must zfs destroy $snap
63	fi
64
65	if [[ -e /tmp/$file ]]; then
66		rm -f /tmp/$file
67	fi
68	if [[ -d /tmp/$dir ]]; then
69		rm -rf /tmp/$dir
70	fi
71
72}
73
74log_assert "zfs unmount fails with bad parameters or scenarios"
75log_onexit cleanup
76
77fs=$TESTPOOL/$TESTFS
78vol=$TESTPOOL/vol.$$
79snap=$TESTPOOL/$TESTFS@snap.$$
80set -A badargs "A" "-A" "F" "-F" "-" "-x" "-?"
81
82if ! ismounted $fs; then
83	log_must zfs mount $fs
84fi
85
86log_must zfs snapshot $snap
87if is_global_zone; then
88	log_must zfs create -V 10m $vol
89else
90	vol=""
91fi
92
93# Testing bad options
94for arg in ${badargs[@]}; do
95	log_mustnot eval "zfs unmount $arg $fs >/dev/null 2>&1"
96done
97
98
99#Testing invalid datasets
100for ds in $snap $vol "blah"; do
101	for opt in "" "-f"; do
102		log_mustnot eval "zfs unmount $opt $ds >/dev/null 2>&1"
103	done
104done
105
106#Testing invalid mountpoint
107dir=foodir.$$
108file=foo.$$
109fs1=$TESTPOOL/fs.$$
110mkdir /tmp/$dir
111touch /tmp/$file
112log_must zfs create -o mountpoint=/tmp/$dir $fs1
113curpath=`dirname $0`
114cd /tmp
115for mpt in "./$dir" "./$file" "/tmp"; do
116	for opt in "" "-f"; do
117		log_mustnot eval "zfs unmount $opt $mpt >/dev/null 2>&1"
118	done
119done
120cd $curpath
121
122#Testing null argument and too many arguments
123for opt in "" "-f"; do
124	log_mustnot eval "zfs unmount $opt >/dev/null 2>&1"
125	log_mustnot eval "zfs unmount $opt $fs $fs1 >/dev/null 2>&1"
126done
127
128#Testing already unmounted filesystem
129log_must zfs unmount $fs1
130for opt in "" "-f"; do
131	log_mustnot eval "zfs unmount $opt $fs1 >/dev/null 2>&1"
132	log_mustnot eval "zfs unmount /tmp/$dir >/dev/null 2>&1"
133done
134
135#Testing legacy mounted filesystem
136log_must zfs set mountpoint=legacy $fs1
137log_must mount -F zfs $fs1 /tmp/$dir
138for opt in "" "-f"; do
139	log_mustnot eval "zfs unmount $opt $fs1 >/dev/null 2>&1"
140done
141umount /tmp/$dir
142
143log_pass "zfs unmount fails with bad parameters or scenarios as expected."
144