1#
2# CDDL HEADER START
3#
4# The contents of this file are subject to the terms of the
5# Common Development and Distribution License (the "License").
6# You may not use this file except in compliance with the License.
7#
8# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9# or http://www.opensolaris.org/os/licensing.
10# See the License for the specific language governing permissions
11# and limitations under the License.
12#
13# When distributing Covered Code, include this CDDL HEADER in each
14# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15# If applicable, add the following below this CDDL HEADER, with the
16# fields enclosed by brackets "[]" replaced with your own identifying
17# information: Portions Copyright [yyyy] [name of copyright owner]
18#
19# CDDL HEADER END
20#
21
22#
23# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24# Use is subject to license terms.
25#
26
27#
28# Copyright (c) 2012, 2016 by Delphix. All rights reserved.
29#
30
31. $STF_SUITE/include/libtest.shlib
32. $STF_SUITE/tests/functional/cli_root/zfs_copies/zfs_copies.cfg
33
34#
35# Compare the value of copies property with specified value
36# $1, the dataset name
37# $2, the expected copies value
38#
39function cmp_prop
40{
41	typeset ds=$1
42	typeset	val_expect=$2
43	typeset val_actual
44
45	val_actual=$(get_prop copies $ds)
46	if [[ $val_actual != $val_expect ]]; then
47		log_fail "Expected value ($val_expect) != actual value " \
48		    "($val_actual)"
49	fi
50}
51
52#
53# Get the value of property used via zfs list
54# $1, the dataset name
55#
56function get_used_prop
57{
58	typeset ds=$1
59	typeset used
60
61	used=`zfs list -H -p -o used $ds`
62	echo $used
63}
64
65#
66# Check the used space is charged correctly
67# $1, the number of used space
68# $2, the expected common factor between the used space and the file space
69#
70function check_used
71{
72	typeset charged_spc=$1
73	typeset -i used
74	typeset -i expected_cfactor=$2
75	typeset -i cfactor
76	typeset -i fsize=${FILESIZE%[m|M]}
77
78	((used = $charged_spc / 1024 / 1024))
79	((cfactor = used / fsize))
80	if ((cfactor != expected_cfactor)); then
81		log_fail "The space is not charged correctly while setting" \
82		    "copies as $expected_cfactor."
83	fi
84}
85
86#
87# test ncopies on volume
88# $1  test type zfs|ufs, default zfs
89# $2  copies
90# $3  mntp for ufs test
91function do_vol_test
92{
93	typeset type=$1
94	typeset copy=$2
95	typeset mntp=$3
96
97	vol=$TESTPOOL/$TESTVOL1
98	vol_b_path=/dev/zvol/dsk/$TESTPOOL/$TESTVOL1
99	vol_r_path=/dev/zvol/rdsk/$TESTPOOL/$TESTVOL1
100
101	log_must zfs create -V $VOLSIZE -o copies=$copy $vol
102	log_must zfs set refreservation=none $vol
103	if [[ $type == "ufs" ]]; then
104		log_must echo y | newfs $vol_r_path >/dev/null 2>&1
105		log_must mount -F ufs -o rw $vol_b_path $mntp
106	else
107		log_must zpool create $TESTPOOL1 $vol_b_path
108		log_must zfs create $TESTPOOL1/$TESTFS1
109	fi
110
111	((nfilesize = copy * ${FILESIZE%m}))
112	pre_used=$(get_used_prop $vol)
113	((target_size = pre_used + nfilesize))
114
115	if [[ $type == "ufs" ]]; then
116		log_must mkfile $FILESIZE $mntp/$FILE
117	else
118		log_must mkfile $FILESIZE /$TESTPOOL1/$TESTFS1/$FILE
119	fi
120
121	post_used=$(get_used_prop $vol)
122	while ((post_used < target_size)) ; do
123		sleep 1
124		post_used=$(get_used_prop $vol)
125	done
126
127	((used = post_used - pre_used))
128	if ((used < nfilesize)); then
129		log_fail "The space is not charged correctly while setting" \
130		    "copies as $copy"
131	fi
132
133	if [[ $type == "ufs" ]]; then
134		umount $mntp
135	else
136		log_must zpool destroy $TESTPOOL1
137	fi
138
139	log_must zfs destroy $vol
140}
141