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# $FreeBSD$
24
25#
26# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
27# Use is subject to license terms.
28#
29# ident	"@(#)zfs_copies.kshlib	1.3	08/02/27 SMI"
30#
31
32. $STF_SUITE/include/libtest.kshlib
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) != " \
48			"actual value ($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 -o used $ds`
62	used=${used%[m|M]}
63	if [[ $used == *K ]]; then
64		used=0
65	fi
66	$ECHO $used
67}
68
69#
70# Check the used space is charged correctly
71# $1, the number of used space
72# $2, the expected common factor between the used space and the file space
73#
74function check_used
75{
76	typeset charged_spc=$1
77	typeset -i used
78	typeset -i expected_cfactor=$2
79	typeset -i cfactor
80	typeset -i fsize=${FILESIZE%[m|M]}
81
82	(( used = ${charged_spc%[m|M]} ))
83	(( cfactor = used / fsize ))
84	if (( cfactor != expected_cfactor )); then
85		log_fail "The space is not charged correctly while setting"\
86			"copies as $expected_cfactor."
87	fi
88}
89
90#
91# test ncopies on volume
92# $1  test type zfs|ufs, default zfs
93# $2  copies
94# $3  mntp for ufs test
95function do_vol_test
96{
97	typeset type=$1
98	typeset copy=$2
99	typeset mntp=$3
100
101	vol=$TESTPOOL/$TESTVOL1
102	vol_b_path=/dev/zvol/$TESTPOOL/$TESTVOL1
103	vol_r_path=/dev/zvol/$TESTPOOL/$TESTVOL1
104
105	log_must $ZFS create -V $VOLSIZE -o copies=$copy $vol
106	if fs_prop_exist "refreserv" ; then
107		log_must $ZFS set refreservation=none $vol
108	fi
109	if [[ $type == "ufs" ]]; then
110		log_must $ECHO y | $NEWFS $vol_r_path >/dev/null 2>&1
111		log_must $MOUNT -F ufs -o rw $vol_b_path $mntp
112	else
113		log_must $ZPOOL create $TESTPOOL1 $vol_b_path
114		log_must $ZFS create $TESTPOOL1/$TESTFS1
115	fi
116
117	(( nfilesize = copy * ${FILESIZE%m} ))
118	pre_used=$(get_used_prop $vol)
119	(( target_size = pre_used + nfilesize ))
120
121	if [[ $type == "ufs" ]]; then
122		log_must $MKFILE $FILESIZE $mntp/$FILE
123	else
124		log_must $MKFILE $FILESIZE /$TESTPOOL1/$TESTFS1/$FILE
125	fi
126
127	post_used=$(get_used_prop $vol)
128	while (( post_used < target_size )) ; do
129		sleep 1
130		post_used=$(get_used_prop $vol)
131	done
132
133	(( used = post_used - pre_used ))
134	if (( used < nfilesize )); then
135		log_fail "The space is not charged correctly while setting"\
136                          "copies as $copy"
137	fi
138
139	if [[ $type == "ufs" ]]; then
140		$UMOUNT $mntp
141	else
142		log_must $ZPOOL destroy $TESTPOOL1
143	fi
144
145	log_must $ZFS destroy $vol
146}
147