#! /bin/ksh -p
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# Copyright (c) 2012 by Delphix. All rights reserved.
#

. $STF_SUITE/include/libtest.shlib
. $STF_SUITE/tests/functional/cli_root/zpool_expand/zpool_expand.cfg

#
# DESCRIPTION:
# vdev retains asize when 2nd last mirror child is removed while autoexpand=off
#
#
# STRATEGY:
# 1) Create a pool
# 2) Create different sized volumes on top of the pool
# 3) Create pool with autoexpand=off using the zvols in a 2-way mirror
# 4) Detach the smaller of the 2 vdev's from the mirror 
# 5) Ensure that smaller vdev can be re-attached
# 6) Detach the smaller vdev again to ensure no expand at import 
# 7) Export pool
# 8) Import pool 
# 9) Check that the pool size has not expanded
# 10) Ensure that smaller vdev can be re-attached
#

verify_runnable "global"

function cleanup
{
	if poolexists $TESTPOOL1; then
		log_must $ZPOOL destroy $TESTPOOL1
	fi

	for i in 1 2; do
		if datasetexists $VFS/vol$i; then
			log_must $ZFS destroy $VFS/vol$i
		fi
	done
}

log_onexit cleanup

log_assert "vdev retains asize when 2nd last mirror child is removed while " \
    "autoexpand=off"

log_must $ZFS create -V $org_size $VFS/vol1
log_must $ZFS create -V $exp_size $VFS/vol2

log_must $ZPOOL create -f -o autoexpand=off $TESTPOOL1 mirror \
    /dev/zvol/dsk/$VFS/vol1  /dev/zvol/dsk/$VFS/vol2

typeset autoexp=$(get_pool_prop autoexpand $TESTPOOL1)
if [[ $autoexp != "off" ]]; then
	log_fail "zpool $TESTPOOL1 autoexpand should off but is $autoexp"
fi

typeset prev_size=$(get_pool_prop size $TESTPOOL1)
typeset zfs_prev_size=$($ZFS get -p avail $TESTPOOL1 | $TAIL -1 | \
    $AWK '{print $3}')

log_must $ZPOOL detach $TESTPOOL1 /dev/zvol/dsk/$VFS/vol1 

$SYNC
$SLEEP 10
$SYNC

log_must $ZPOOL attach $TESTPOOL1 /dev/zvol/dsk/$VFS/vol2 \
    /dev/zvol/dsk/$VFS/vol1

log_must $ZPOOL detach $TESTPOOL1 /dev/zvol/dsk/$VFS/vol1 

log_must $ZPOOL export $TESTPOOL1

log_must $ZPOOL import -d /dev/zvol/dsk/$VFS $TESTPOOL1

typeset post_size=$(get_pool_prop size $TESTPOOL1)
typeset zfs_post_size=$($ZFS get -p avail $TESTPOOL1 | $TAIL -1 | \
    $AWK '{print $3}')

log_note "$TESTPOOL1 mirror had previous size: $prev_size and " \
    "after detach has size: $post_size"
# compare available pool size from zfs
if [[ $zfs_post_size > $zfs_prev_size ]]; then
	log_fail "pool $TESTPOOL1 has autoexpanded with " \
	    "autoexpand=off when smaller mirror child removed"
fi

log_must $ZPOOL attach $TESTPOOL1 /dev/zvol/dsk/$VFS/vol2 /dev/zvol/dsk/$VFS/vol1 

log_must $ZPOOL destroy $TESTPOOL1

log_pass "pool $TESTPOOL1 retained size after smaller child " \
    "was removed from mirror with autoexpand=off"
