aboutsummaryrefslogtreecommitdiff
path: root/sys/contrib/openzfs/tests/zfs-tests/tests/functional/cli_root/zfs_copies/zfs_copies.kshlib
blob: e886de432af412b1c1077b5d2048fa2eaa6d2f96 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#
# 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 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

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

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

#
# Compare the value of copies property with specified value
# $1, the dataset name
# $2, the expected copies value
#
function cmp_prop
{
	typeset ds=$1
	typeset	val_expect=$2
	typeset val_actual

	val_actual=$(get_prop copies $ds)
	if [[ $val_actual != $val_expect ]]; then
		log_fail "Expected value ($val_expect) != actual value " \
		    "($val_actual)"
	fi
}

#
# Check the used space is charged correctly
# $1, the number of used space
# $2, the expected common factor between the used space and the file space
#
function check_used
{
	typeset charged_spc=$1
	typeset -i used
	typeset -i expected_cfactor=$2
	typeset -i cfactor
	typeset -i fsize=${FILESIZE%[m|M]}

	((used = $charged_spc / 1024 / 1024))
	((cfactor = used / fsize))
	if ((cfactor != expected_cfactor)); then
		log_fail "The space is not charged correctly while setting" \
		    "copies as $expected_cfactor."
	fi
}

#
# test ncopies on volume
# $1  test type zfs|ufs|ext2
# $2  copies
# $3  mntp for ufs|ext2 test
function do_vol_test
{
	typeset type=$1
	typeset copies=$2
	typeset mntp=$3

	vol=$TESTPOOL/$TESTVOL1
	vol_b_path=$ZVOL_DEVDIR/$TESTPOOL/$TESTVOL1
	vol_r_path=$ZVOL_RDEVDIR/$TESTPOOL/$TESTVOL1

	log_must zfs create -V $VOLSIZE -o copies=$copies $vol
	log_must zfs set refreservation=none $vol
	block_device_wait

	case "$type" in
	"ext2")
		if is_freebsd; then
			log_unsupported "ext2 test not implemented for freebsd"
		fi
		log_must eval "new_fs $vol_r_path >/dev/null 2>&1"
		log_must mount -o rw $vol_b_path $mntp
		;;
	"ufs")
		if is_linux; then
			log_unsupported "ufs test not implemented for linux"
		fi
		log_must eval "new_fs $vol_r_path >/dev/null 2>&1"
		log_must mount $vol_b_path $mntp
		;;
	"zfs")
		if is_freebsd; then
			# Pool creation on zvols is forbidden by default.
			# Save and restore the current setting.
			typeset _saved=$(get_tunable VOL_RECURSIVE)
			log_must set_tunable64 VOL_RECURSIVE 1 # Allow
			zpool create $TESTPOOL1 $vol_b_path
			typeset _zpool_create_result=$?
			log_must set_tunable64 VOL_RECURSIVE $_saved # Restore
			log_must test $_zpool_create_result = 0
		else
			log_must zpool create $TESTPOOL1 $vol_b_path
		fi
		log_must zfs create $TESTPOOL1/$TESTFS1
		;;
	*)
		log_unsupported "$type test not implemented"
		;;
	esac

	((nfilesize = copies * ${FILESIZE%m}))
	pre_used=$(get_prop used $vol)
	((target_size = pre_used + nfilesize))

	if [[ $type == "zfs" ]]; then
		log_must mkfile $FILESIZE /$TESTPOOL1/$TESTFS1/$FILE
	else
		log_must mkfile $FILESIZE $mntp/$FILE
	fi

	post_used=$(get_prop used $vol)
	((retries = 0))
	while ((post_used < target_size && retries++ < 42)); do
		sleep 1
		post_used=$(get_prop used $vol)
	done

	((used = post_used - pre_used))
	if ((used < nfilesize)); then
		log_fail "The space is not charged correctly while setting" \
		    "copies as $copies ($used < $nfilesize)" \
		    "pre=${pre_used} post=${post_used}"
	fi

	if [[ $type == "zfs" ]]; then
		log_must zpool destroy $TESTPOOL1
	else
		log_must umount $mntp
	fi

	log_must zfs destroy $vol
}