xref: /illumos-gate/usr/src/test/zfs-tests/tests/functional/cli_root/zfs_copies/zfs_copies.kshlib (revision 5e989a96186a37eb528fb7bb4d28a150874ec799)
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 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 -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/dsk/$TESTPOOL/$TESTVOL1
103	vol_r_path=/dev/zvol/rdsk/$TESTPOOL/$TESTVOL1
104
105	log_must $ZFS create -V $VOLSIZE -o copies=$copy $vol
106	log_must $ZFS set refreservation=none $vol
107	if [[ $type == "ufs" ]]; then
108		log_must $ECHO y | $NEWFS $vol_r_path >/dev/null 2>&1
109		log_must $MOUNT -F ufs -o rw $vol_b_path $mntp
110	else
111		log_must $ZPOOL create $TESTPOOL1 $vol_b_path
112		log_must $ZFS create $TESTPOOL1/$TESTFS1
113	fi
114
115	((nfilesize = copy * ${FILESIZE%m}))
116	pre_used=$(get_used_prop $vol)
117	((target_size = pre_used + nfilesize))
118
119	if [[ $type == "ufs" ]]; then
120		log_must $MKFILE $FILESIZE $mntp/$FILE
121	else
122		log_must $MKFILE $FILESIZE /$TESTPOOL1/$TESTFS1/$FILE
123	fi
124
125	post_used=$(get_used_prop $vol)
126	while ((post_used < target_size)) ; do
127		sleep 1
128		post_used=$(get_used_prop $vol)
129	done
130
131	((used = post_used - pre_used))
132	if ((used < nfilesize)); then
133		log_fail "The space is not charged correctly while setting" \
134		    "copies as $copy"
135	fi
136
137	if [[ $type == "ufs" ]]; then
138		$UMOUNT $mntp
139	else
140		log_must $ZPOOL destroy $TESTPOOL1
141	fi
142
143	log_must $ZFS destroy $vol
144}
145