1#!/usr/local/bin/ksh93 -p
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 2007 Sun Microsystems, Inc.  All rights reserved.
27# Use is subject to license terms.
28#
29# ident	"@(#)compress_004_pos.ksh	1.3	07/06/06 SMI"
30#
31. $STF_SUITE/include/libtest.kshlib
32
33################################################################################
34#
35# __stc_assertion_start
36#
37# ID: compress_004_pos
38#
39# DESCRIPTION:
40# With 'compression' set, a file with non-power-of-2 blocksize storage space
41# can be freed as will normally.
42#
43# STRATEGY:
44#	1. Set 'compression' or 'compress' to on or lzjb
45#	2. Set different recordsize with ZFS filesystem
46#	3. Repeatedly using 'randfree_file' to create a file and then free its
47#	   storage space with different range, the system should work normally.
48#
49# TESTABILITY: explicit
50#
51# TEST_AUTOMATION_LEVEL: automated
52#
53# CODING_STATUS: COMPLETED (2006-07-26)
54#
55# __stc_assertion_end
56#
57################################################################################
58
59verify_runnable "both"
60
61function cleanup
62{
63	$RM -f $TESTDIR/*
64}
65
66function create_free_testing #<file size> <file>
67{
68	typeset -i fsz=$1
69	typeset file=$2
70	typeset -i start=0
71	typeset -i len=0
72	typeset -i dist=0
73
74	for start in 0 `expr $RANDOM % $fsz`
75	do
76		(( dist = fsz - start ))
77		for len in `expr $RANDOM % $dist` $dist \
78			`expr $start + $dist`; do
79			log_must $RANDFREE_FILE -l fsz -s $start \
80				-n $len $file
81			[[ -e $file ]] && \
82				log_must $RM -f $file
83		done
84	done
85}
86
87
88log_assert "Creating non-power-of-2 blocksize file and freeing the file \
89	storage space at will should work normally with compression setting"
90log_onexit cleanup
91
92fs=$TESTPOOL/$TESTFS
93single_blk_file=$TESTDIR/singleblkfile.${TESTCASE_ID}
94multi_blk_file=$TESTDIR/multiblkfile.${TESTCASE_ID}
95typeset -i blksize=512
96typeset -i fsize=0
97typeset -i avail=0
98typeset -i blknum=0
99
100for propname in "compression" "compress"
101do
102	for value in $(get_compress_opts zfs_compress)
103	do
104		log_must $ZFS set compression=$value $fs
105		real_val=$(get_prop $propname $fs)
106		if [[ $value == "gzip-6" ]]; then
107			value="gzip"
108		fi
109                [[ $real_val != $value ]] && \
110                        log_fail "Set property $propname=$value failed."
111
112		(( blksize = 512 ))
113		while (( blksize <= 131072 )); do
114			log_must $ZFS set recordsize=$blksize $fs
115
116			# doing single block testing
117			(( fsize = $RANDOM ))
118			if (( fsize > blksize )); then
119				(( fsize = fsize % blksize ))
120			fi
121			if (( (fsize % 2) == 0 )); then
122				#make sure fsize is non-power-of-2
123				(( fsize = fsize + 1 ))
124			fi
125			create_free_testing $fsize $single_blk_file
126
127			# doing multiple blocks testing
128			avail=$(get_prop available $fs)
129			(( blknum = avail / blksize ))
130			# we just test <10 multi-blocks to limit testing time
131                	(( blknum = blknum % 9 ))
132                	while (( blknum < 2 )); do
133                        	(( blknum = blknum + $RANDOM % 9 ))
134                	done
135			if (( (blknum % 2) == 0 )); then
136				(( blknum = blknum + 1 )) # keep blknum as odd
137			fi
138			(( fsize = blknum * blksize ))
139			create_free_testing $fsize $multi_blk_file
140
141			(( blksize = blksize * 2 ))
142		done
143	done
144done
145
146log_pass "Creating and freeing non-power-of-2 blocksize file work as expected."
147