1f38cb554SJohn Wren Kennedy#
2f38cb554SJohn Wren Kennedy# CDDL HEADER START
3f38cb554SJohn Wren Kennedy#
4f38cb554SJohn Wren Kennedy# The contents of this file are subject to the terms of the
5f38cb554SJohn Wren Kennedy# Common Development and Distribution License (the "License").
6f38cb554SJohn Wren Kennedy# You may not use this file except in compliance with the License.
7f38cb554SJohn Wren Kennedy#
8f38cb554SJohn Wren Kennedy# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9f38cb554SJohn Wren Kennedy# or http://www.opensolaris.org/os/licensing.
10f38cb554SJohn Wren Kennedy# See the License for the specific language governing permissions
11f38cb554SJohn Wren Kennedy# and limitations under the License.
12f38cb554SJohn Wren Kennedy#
13f38cb554SJohn Wren Kennedy# When distributing Covered Code, include this CDDL HEADER in each
14f38cb554SJohn Wren Kennedy# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15f38cb554SJohn Wren Kennedy# If applicable, add the following below this CDDL HEADER, with the
16f38cb554SJohn Wren Kennedy# fields enclosed by brackets "[]" replaced with your own identifying
17f38cb554SJohn Wren Kennedy# information: Portions Copyright [yyyy] [name of copyright owner]
18f38cb554SJohn Wren Kennedy#
19f38cb554SJohn Wren Kennedy# CDDL HEADER END
20f38cb554SJohn Wren Kennedy#
21f38cb554SJohn Wren Kennedy
22f38cb554SJohn Wren Kennedy#
23f38cb554SJohn Wren Kennedy# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24f38cb554SJohn Wren Kennedy# Use is subject to license terms.
25f38cb554SJohn Wren Kennedy#
26f38cb554SJohn Wren Kennedy
27f38cb554SJohn Wren Kennedy#
28*1d32ba66SJohn Wren Kennedy# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
29f38cb554SJohn Wren Kennedy#
30f38cb554SJohn Wren Kennedy
31f38cb554SJohn Wren Kennedy. $STF_SUITE/include/libtest.shlib
32f38cb554SJohn Wren Kennedy
33f38cb554SJohn Wren Kennedyexport USEDTEST=$TESTPOOL/$TESTFS/usedtest-snapused
34f38cb554SJohn Wren Kennedy
35f38cb554SJohn Wren Kennedyfunction _check_used # dataset
36f38cb554SJohn Wren Kennedy{
37f38cb554SJohn Wren Kennedy        typeset dataset=$1
38f38cb554SJohn Wren Kennedy
39f38cb554SJohn Wren Kennedy	if [[ "$(get_prop type $dataset)" == "snapshot" ]]; then
40f38cb554SJohn Wren Kennedy		return
41f38cb554SJohn Wren Kennedy	fi
42f38cb554SJohn Wren Kennedy
43f38cb554SJohn Wren Kennedy	used=$(get_prop used $dataset)
44f38cb554SJohn Wren Kennedy	usedbychildren=$(get_prop usedbychildren $dataset)
45f38cb554SJohn Wren Kennedy	usedbydataset=$(get_prop usedbydataset $dataset)
46f38cb554SJohn Wren Kennedy	usedbyrefreservation=$(get_prop usedbyrefreservation $dataset)
47f38cb554SJohn Wren Kennedy	usedbysnapshots=$(get_prop usedbysnapshots $dataset)
48f38cb554SJohn Wren Kennedy	((used_sum = usedbychildren + usedbydataset + \
49f38cb554SJohn Wren Kennedy		usedbyrefreservation + usedbysnapshots))
50f38cb554SJohn Wren Kennedy	if ((used != used_sum)); then
51f38cb554SJohn Wren Kennedy		log_fail "$dataset: used($used) is not the sum($used_sum) of usedby*"
52f38cb554SJohn Wren Kennedy	fi
53f38cb554SJohn Wren Kennedy}
54f38cb554SJohn Wren Kennedy
55f38cb554SJohn Wren Kennedyfunction check_used # dataset
56f38cb554SJohn Wren Kennedy{
57f38cb554SJohn Wren Kennedy	typeset dataset=$1
58*1d32ba66SJohn Wren Kennedy	for child in $(zfs list -rH -t filesystem,volume -o name $dataset)
59f38cb554SJohn Wren Kennedy	do
60f38cb554SJohn Wren Kennedy		_check_used $child
61f38cb554SJohn Wren Kennedy	done
62f38cb554SJohn Wren Kennedy}
63f38cb554SJohn Wren Kennedy
64f38cb554SJohn Wren Kennedyfunction check_usedbychildren # dataset
65f38cb554SJohn Wren Kennedy{
66f38cb554SJohn Wren Kennedy	typeset dataset=$1
67f38cb554SJohn Wren Kennedy	typeset -i usedbychildren_sum=0
68f38cb554SJohn Wren Kennedy	typeset -i parent_usedbychildren=0
69*1d32ba66SJohn Wren Kennedy	for child in $(zfs list -rH -t filesystem,volume -o name $dataset)
70f38cb554SJohn Wren Kennedy	do
71f38cb554SJohn Wren Kennedy		if [[ "$(get_prop type $child)" == "snapshot" ]]; then
72f38cb554SJohn Wren Kennedy			continue
73f38cb554SJohn Wren Kennedy		fi
74f38cb554SJohn Wren Kennedy
75f38cb554SJohn Wren Kennedy		# parent
76f38cb554SJohn Wren Kennedy		if [[ "$child" == "$dataset" ]]; then
77f38cb554SJohn Wren Kennedy			parent_usedbychildren=$(get_prop usedbychildren $child)
78f38cb554SJohn Wren Kennedy		else #child
79f38cb554SJohn Wren Kennedy			reservation=$(get_prop reservation $child)
80f38cb554SJohn Wren Kennedy			used=$(get_prop used $child)
81f38cb554SJohn Wren Kennedy			if ((reservation > used)); then
82f38cb554SJohn Wren Kennedy				((usedbychildren_sum += reservation))
83f38cb554SJohn Wren Kennedy			else
84f38cb554SJohn Wren Kennedy				((usedbychildren_sum += used))
85f38cb554SJohn Wren Kennedy			fi
86f38cb554SJohn Wren Kennedy		fi
87f38cb554SJohn Wren Kennedy	done
88f38cb554SJohn Wren Kennedy
89f38cb554SJohn Wren Kennedy        if ((parent_usedbychildren != usedbychildren_sum)); then
90f38cb554SJohn Wren Kennedy                log_fail "$dataset: usedbychildren($parent_usedbychildren) is not the sum($usedbychildren_sum) of used by children"
91f38cb554SJohn Wren Kennedy        fi
92f38cb554SJohn Wren Kennedy}
93f38cb554SJohn Wren Kennedy
94f38cb554SJohn Wren Kennedyfunction _check_usedbydataset # dataset
95f38cb554SJohn Wren Kennedy{
96f38cb554SJohn Wren Kennedy        typeset dataset=$1
97f38cb554SJohn Wren Kennedy	if [[ "$(get_prop type $dataset)" == "snapshot" ]]; then
98f38cb554SJohn Wren Kennedy		return
99f38cb554SJohn Wren Kennedy	fi
100f38cb554SJohn Wren Kennedy
101f38cb554SJohn Wren Kennedy	usedbydataset=$(get_prop usedbydataset $dataset)
102f38cb554SJohn Wren Kennedy	referenced=$(get_prop referenced $dataset)
103f38cb554SJohn Wren Kennedy
104f38cb554SJohn Wren Kennedy	is_cloned=$(get_prop is:cloned $dataset)
105f38cb554SJohn Wren Kennedy
106f38cb554SJohn Wren Kennedy	if [[ "$is_cloned" == "yes" ]]; then
107f38cb554SJohn Wren Kennedy		if ((usedbydataset > referenced)); then
108f38cb554SJohn Wren Kennedy			log_fail "$dataset(cloned): usedbydataset($usedbydataset) is more than referenced($referenced)"
109f38cb554SJohn Wren Kennedy		fi
110f38cb554SJohn Wren Kennedy	else
111f38cb554SJohn Wren Kennedy		#
112f38cb554SJohn Wren Kennedy		# if non-clones, should usedbydataset == referenced
113f38cb554SJohn Wren Kennedy		#
114f38cb554SJohn Wren Kennedy		if ((usedbydataset != referenced)); then
115f38cb554SJohn Wren Kennedy			log_fail "$dataset: usedbydataset($usedbydataset) is not equal to referenced($referenced)"
116f38cb554SJohn Wren Kennedy		fi
117f38cb554SJohn Wren Kennedy	fi
118f38cb554SJohn Wren Kennedy}
119f38cb554SJohn Wren Kennedy
120f38cb554SJohn Wren Kennedyfunction check_usedbydataset # dataset
121f38cb554SJohn Wren Kennedy{
122f38cb554SJohn Wren Kennedy	typeset dataset=$1
123*1d32ba66SJohn Wren Kennedy	for child in $(zfs list -rH -t filesystem,volume -o name $dataset)
124f38cb554SJohn Wren Kennedy	do
125f38cb554SJohn Wren Kennedy		_check_usedbydataset $child
126f38cb554SJohn Wren Kennedy	done
127f38cb554SJohn Wren Kennedy}
128f38cb554SJohn Wren Kennedy
129f38cb554SJohn Wren Kennedyfunction _check_usedbyrefreservation # dataset
130f38cb554SJohn Wren Kennedy{
131f38cb554SJohn Wren Kennedy        typeset dataset=$1
132f38cb554SJohn Wren Kennedy	if [[ "$(get_prop type $dataset)" == "snapshot" ]]; then
133f38cb554SJohn Wren Kennedy		return
134f38cb554SJohn Wren Kennedy	fi
135f38cb554SJohn Wren Kennedy
136f38cb554SJohn Wren Kennedy	usedbyrefreservation=$(get_prop usedbyrefreservation $dataset)
137f38cb554SJohn Wren Kennedy	referenced=$(get_prop referenced $dataset)
138f38cb554SJohn Wren Kennedy	refreservation=$(get_prop refreservation $dataset)
139f38cb554SJohn Wren Kennedy	((diff_ref = refreservation - referenced))
140f38cb554SJohn Wren Kennedy	if ((usedbyrefreservation > refreservation || \
141f38cb554SJohn Wren Kennedy		usedbyrefreservation < diff_ref)); then
142f38cb554SJohn Wren Kennedy		log_fail "$dataset: usedbyrefreservation($usedbyrefreservation) checking is not ok"
143f38cb554SJohn Wren Kennedy	fi
144f38cb554SJohn Wren Kennedy}
145f38cb554SJohn Wren Kennedy
146f38cb554SJohn Wren Kennedyfunction check_usedbyrefreservation # dataset
147f38cb554SJohn Wren Kennedy{
148f38cb554SJohn Wren Kennedy	typeset dataset=$1
149*1d32ba66SJohn Wren Kennedy	for child in $(zfs list -rH -t filesystem,volume -o name $dataset)
150f38cb554SJohn Wren Kennedy	do
151f38cb554SJohn Wren Kennedy		_check_usedbyrefreservation $child
152f38cb554SJohn Wren Kennedy	done
153f38cb554SJohn Wren Kennedy}
154f38cb554SJohn Wren Kennedy
155f38cb554SJohn Wren Kennedyfunction check_usedbysnapshots # dataset
156f38cb554SJohn Wren Kennedy{
157f38cb554SJohn Wren Kennedy	typeset dataset=$1
158f38cb554SJohn Wren Kennedy	typeset -i usedbysnapshots_sum=0
159f38cb554SJohn Wren Kennedy	typeset -i parent_usedbysnapshots=0
160*1d32ba66SJohn Wren Kennedy	for child in $(zfs list -rH -t filesystem,volume,snapshot -o name $dataset)
161f38cb554SJohn Wren Kennedy	do
162f38cb554SJohn Wren Kennedy		# parent
163f38cb554SJohn Wren Kennedy		if [[ "$child" == "$dataset" ]]; then
164f38cb554SJohn Wren Kennedy			parent_usedbysnapshots=$(get_prop usedbysnapshots $child)
165f38cb554SJohn Wren Kennedy			continue
166f38cb554SJohn Wren Kennedy		fi
167f38cb554SJohn Wren Kennedy
168f38cb554SJohn Wren Kennedy		if [[ "$(get_prop type $child)" != "snapshot" ]]; then
169f38cb554SJohn Wren Kennedy			continue
170f38cb554SJohn Wren Kennedy		fi
171f38cb554SJohn Wren Kennedy
172f38cb554SJohn Wren Kennedy		if [[ "$child" != "$dataset@"* ]]; then
173f38cb554SJohn Wren Kennedy			continue
174f38cb554SJohn Wren Kennedy		fi
175f38cb554SJohn Wren Kennedy
176f38cb554SJohn Wren Kennedy		# snapshot
177f38cb554SJohn Wren Kennedy		used=$(get_prop used $child)
178f38cb554SJohn Wren Kennedy		((usedbysnapshots_sum += used))
179f38cb554SJohn Wren Kennedy	done
180f38cb554SJohn Wren Kennedy
181f38cb554SJohn Wren Kennedy        if ((parent_usedbysnapshots < usedbysnapshots_sum)); then
182f38cb554SJohn Wren Kennedy                log_fail "$dataset: usedbysnapshots($parent_usedbysnapshots) is not more than or equal to" \
183f38cb554SJohn Wren Kennedy				"the sum($usedbysnapshots_sum) of used of snapshots"
184f38cb554SJohn Wren Kennedy        fi
185f38cb554SJohn Wren Kennedy}
186