1f38cb554SJohn Wren Kennedy#!/bin/ksh -p
2f38cb554SJohn Wren Kennedy#
3f38cb554SJohn Wren Kennedy# CDDL HEADER START
4f38cb554SJohn Wren Kennedy#
5f38cb554SJohn Wren Kennedy# The contents of this file are subject to the terms of the
6f38cb554SJohn Wren Kennedy# Common Development and Distribution License (the "License").
7f38cb554SJohn Wren Kennedy# You may not use this file except in compliance with the License.
8f38cb554SJohn Wren Kennedy#
9f38cb554SJohn Wren Kennedy# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10f38cb554SJohn Wren Kennedy# or http://www.opensolaris.org/os/licensing.
11f38cb554SJohn Wren Kennedy# See the License for the specific language governing permissions
12f38cb554SJohn Wren Kennedy# and limitations under the License.
13f38cb554SJohn Wren Kennedy#
14f38cb554SJohn Wren Kennedy# When distributing Covered Code, include this CDDL HEADER in each
15f38cb554SJohn Wren Kennedy# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16f38cb554SJohn Wren Kennedy# If applicable, add the following below this CDDL HEADER, with the
17f38cb554SJohn Wren Kennedy# fields enclosed by brackets "[]" replaced with your own identifying
18f38cb554SJohn Wren Kennedy# information: Portions Copyright [yyyy] [name of copyright owner]
19f38cb554SJohn Wren Kennedy#
20f38cb554SJohn Wren Kennedy# CDDL HEADER END
21f38cb554SJohn Wren Kennedy#
22f38cb554SJohn Wren Kennedy
23f38cb554SJohn Wren Kennedy#
24f38cb554SJohn Wren Kennedy# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
25f38cb554SJohn Wren Kennedy# Use is subject to license terms.
26f38cb554SJohn Wren Kennedy#
27f38cb554SJohn Wren Kennedy
28f38cb554SJohn Wren Kennedy#
29*1d32ba66SJohn Wren Kennedy# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
30f38cb554SJohn Wren Kennedy#
31f38cb554SJohn Wren Kennedy
32f38cb554SJohn Wren Kennedy. $STF_SUITE/include/libtest.shlib
33f38cb554SJohn Wren Kennedy
34f38cb554SJohn Wren Kennedy#
35f38cb554SJohn Wren Kennedy# DESCRIPTION:
36f38cb554SJohn Wren Kennedy# With 'compression' set, a file with non-power-of-2 blocksize storage space
37f38cb554SJohn Wren Kennedy# can be freed as will normally.
38f38cb554SJohn Wren Kennedy#
39f38cb554SJohn Wren Kennedy# STRATEGY:
40f38cb554SJohn Wren Kennedy#	1. Set 'compression' or 'compress' to on or lzjb
41f38cb554SJohn Wren Kennedy#	2. Set different recordsize with ZFS filesystem
42f38cb554SJohn Wren Kennedy#	3. Repeatedly using 'randfree_file' to create a file and then free its
43f38cb554SJohn Wren Kennedy#	   storage space with different range, the system should work normally.
44f38cb554SJohn Wren Kennedy#
45f38cb554SJohn Wren Kennedy
46f38cb554SJohn Wren Kennedyverify_runnable "both"
47f38cb554SJohn Wren Kennedy
48f38cb554SJohn Wren Kennedyfunction cleanup
49f38cb554SJohn Wren Kennedy{
50*1d32ba66SJohn Wren Kennedy	rm -f $TESTDIR/*
51f38cb554SJohn Wren Kennedy}
52f38cb554SJohn Wren Kennedy
53f38cb554SJohn Wren Kennedyfunction create_free_testing #<file size> <file>
54f38cb554SJohn Wren Kennedy{
55f38cb554SJohn Wren Kennedy	typeset -i fsz=$1
56f38cb554SJohn Wren Kennedy	typeset file=$2
57f38cb554SJohn Wren Kennedy	typeset -i start=0
58f38cb554SJohn Wren Kennedy	typeset -i len=0
59f38cb554SJohn Wren Kennedy	typeset -i dist=0
60f38cb554SJohn Wren Kennedy
61f38cb554SJohn Wren Kennedy	for start in 0 `expr $RANDOM % $fsz`
62f38cb554SJohn Wren Kennedy	do
63f38cb554SJohn Wren Kennedy		(( dist = fsz - start ))
64f38cb554SJohn Wren Kennedy		for len in `expr $RANDOM % $dist` $dist \
65f38cb554SJohn Wren Kennedy			`expr $start + $dist`; do
66*1d32ba66SJohn Wren Kennedy			log_must randfree_file -l fsz -s $start \
67f38cb554SJohn Wren Kennedy				-n $len $file
68f38cb554SJohn Wren Kennedy			[[ -e $file ]] && \
69*1d32ba66SJohn Wren Kennedy				log_must rm -f $file
70f38cb554SJohn Wren Kennedy		done
71f38cb554SJohn Wren Kennedy	done
72f38cb554SJohn Wren Kennedy}
73f38cb554SJohn Wren Kennedy
74f38cb554SJohn Wren Kennedy
75f38cb554SJohn Wren Kennedylog_assert "Creating non-power-of-2 blocksize file and freeing the file \
76f38cb554SJohn Wren Kennedy	storage space at will should work normally with compression setting"
77f38cb554SJohn Wren Kennedylog_onexit cleanup
78f38cb554SJohn Wren Kennedy
79f38cb554SJohn Wren Kennedyfs=$TESTPOOL/$TESTFS
80f38cb554SJohn Wren Kennedysingle_blk_file=$TESTDIR/singleblkfile.$$
81f38cb554SJohn Wren Kennedymulti_blk_file=$TESTDIR/multiblkfile.$$
82f38cb554SJohn Wren Kennedytypeset -i blksize=512
83f38cb554SJohn Wren Kennedytypeset -i fsize=0
84f38cb554SJohn Wren Kennedytypeset -i avail=0
85f38cb554SJohn Wren Kennedytypeset -i blknum=0
86f38cb554SJohn Wren Kennedy
87f38cb554SJohn Wren Kennedyfor propname in "compression" "compress"
88f38cb554SJohn Wren Kennedydo
89f38cb554SJohn Wren Kennedy	for value in $(get_compress_opts zfs_compress)
90f38cb554SJohn Wren Kennedy	do
91*1d32ba66SJohn Wren Kennedy		log_must zfs set compression=$value $fs
92f38cb554SJohn Wren Kennedy		real_val=$(get_prop $propname $fs)
93f38cb554SJohn Wren Kennedy		if [[ $value == "gzip-6" ]]; then
94f38cb554SJohn Wren Kennedy			value="gzip"
95f38cb554SJohn Wren Kennedy		fi
96f38cb554SJohn Wren Kennedy                [[ $real_val != $value ]] && \
97f38cb554SJohn Wren Kennedy                        log_fail "Set property $propname=$value failed."
98f38cb554SJohn Wren Kennedy
99f38cb554SJohn Wren Kennedy		(( blksize = 512 ))
100f38cb554SJohn Wren Kennedy		while (( blksize <= 131072 )); do
101*1d32ba66SJohn Wren Kennedy			log_must zfs set recordsize=$blksize $fs
102f38cb554SJohn Wren Kennedy
103f38cb554SJohn Wren Kennedy			# doing single block testing
104f38cb554SJohn Wren Kennedy			(( fsize = $RANDOM ))
105f38cb554SJohn Wren Kennedy			if (( fsize > blksize )); then
106f38cb554SJohn Wren Kennedy				(( fsize = fsize % blksize ))
107f38cb554SJohn Wren Kennedy			fi
108f38cb554SJohn Wren Kennedy			if (( (fsize % 2) == 0 )); then
109f38cb554SJohn Wren Kennedy				#make sure fsize is non-power-of-2
110f38cb554SJohn Wren Kennedy				(( fsize = fsize + 1 ))
111f38cb554SJohn Wren Kennedy			fi
112f38cb554SJohn Wren Kennedy			create_free_testing $fsize $single_blk_file
113f38cb554SJohn Wren Kennedy
114f38cb554SJohn Wren Kennedy			# doing multiple blocks testing
115f38cb554SJohn Wren Kennedy			avail=$(get_prop available $fs)
116f38cb554SJohn Wren Kennedy			(( blknum = avail / blksize ))
117f38cb554SJohn Wren Kennedy			# we just test <10 multi-blocks to limit testing time
118f38cb554SJohn Wren Kennedy			(( blknum = blknum % 9 ))
119f38cb554SJohn Wren Kennedy			while (( blknum < 2 )); do
120f38cb554SJohn Wren Kennedy				(( blknum = blknum + $RANDOM % 9 ))
121f38cb554SJohn Wren Kennedy			done
122f38cb554SJohn Wren Kennedy			if (( (blknum % 2) == 0 )); then
123f38cb554SJohn Wren Kennedy				(( blknum = blknum + 1 )) # keep blknum as odd
124f38cb554SJohn Wren Kennedy			fi
125f38cb554SJohn Wren Kennedy			(( fsize = blknum * blksize ))
126f38cb554SJohn Wren Kennedy			create_free_testing $fsize $multi_blk_file
127f38cb554SJohn Wren Kennedy
128f38cb554SJohn Wren Kennedy			(( blksize = blksize * 2 ))
129f38cb554SJohn Wren Kennedy		done
130f38cb554SJohn Wren Kennedy	done
131f38cb554SJohn Wren Kennedydone
132f38cb554SJohn Wren Kennedy
133f38cb554SJohn Wren Kennedylog_pass "Creating and freeing non-power-of-2 blocksize file work as expected."
134