1# vim: filetype=sh
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 2008 Sun Microsystems, Inc.  All rights reserved.
25# Use is subject to license terms.
26
27. $STF_SUITE/include/libtest.kshlib
28
29#
30# Compare the value of copies property with specified value
31# $1, the dataset name
32# $2, the expected copies value
33#
34function cmp_prop
35{
36	typeset ds=$1
37	typeset	val_expect=$2
38	typeset val_actual
39
40	val_actual=$(get_prop copies $ds)
41	if [[ $val_actual != $val_expect ]]; then
42		log_fail "Expected value ($val_expect) != " \
43			"actual value ($val_actual)"
44	fi
45}
46
47#
48# Get the value of property used via zfs list
49# $1, the dataset name
50#
51function get_used_prop
52{
53	typeset ds=$1
54	typeset used
55
56	used=`$ZFS list -H -o used $ds`
57	used=${used%[m|M]}
58	if [[ $used == *K ]]; then
59		used=0
60	fi
61	$ECHO $used
62}
63
64#
65# Check the used space is charged correctly
66# $1, the number of used space
67# $2, the expected common factor between the used space and the file space
68#
69function check_used
70{
71	typeset charged_spc=$1
72	typeset -i used
73	typeset -i expected_cfactor=$2
74	typeset -i cfactor
75	typeset -i fsize=${FILESIZE%[m|M]}
76
77	(( used = ${charged_spc%[m|M]} ))
78	(( cfactor = used / fsize ))
79	if (( cfactor != expected_cfactor )); then
80		log_fail "The space is not charged correctly while setting"\
81			"copies as $expected_cfactor."
82	fi
83}
84
85#
86# test ncopies on volume
87# $1  test type zfs|ufs, default zfs
88# $2  copies
89# $3  mntp for ufs test
90function do_vol_test
91{
92	typeset type=$1
93	typeset copy=$2
94	typeset mntp=$3
95
96	vol=$TESTPOOL/$TESTVOL1
97	vol_b_path=/dev/zvol/$TESTPOOL/$TESTVOL1
98	vol_r_path=/dev/zvol/$TESTPOOL/$TESTVOL1
99
100	log_must $ZFS create -V $VOLSIZE -o copies=$copy $vol
101	if fs_prop_exist "refreserv" ; then
102		log_must $ZFS set refreservation=none $vol
103	fi
104	if [[ $type == "ufs" ]]; then
105		log_must $ECHO y | $NEWFS $vol_r_path >/dev/null 2>&1
106		log_must $MOUNT -F ufs -o rw $vol_b_path $mntp
107	else
108		log_must $ZPOOL create $TESTPOOL1 $vol_b_path
109		log_must $ZFS create $TESTPOOL1/$TESTFS1
110	fi
111
112	(( nfilesize = copy * ${FILESIZE%m} ))
113	pre_used=$(get_used_prop $vol)
114	(( target_size = pre_used + nfilesize ))
115
116	if [[ $type == "ufs" ]]; then
117		log_must $MKFILE $FILESIZE $mntp/$FILE
118	else
119		log_must $MKFILE $FILESIZE /$TESTPOOL1/$TESTFS1/$FILE
120	fi
121
122	post_used=$(get_used_prop $vol)
123	while (( post_used < target_size )) ; do
124		sleep 1
125		post_used=$(get_used_prop $vol)
126	done
127
128	(( used = post_used - pre_used ))
129	if (( used < nfilesize )); then
130		log_fail "The space is not charged correctly while setting"\
131                          "copies as $copy"
132	fi
133
134	if [[ $type == "ufs" ]]; then
135		$UMOUNT $mntp
136	else
137		log_must $ZPOOL destroy $TESTPOOL1
138	fi
139
140	log_must $ZFS destroy $vol
141}
142