1#!/bin/ksh -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#
24# Copyright 2016, loli10K. All rights reserved.
25# Copyright (c) 2017 Datto Inc.
26#
27
28. $STF_SUITE/include/libtest.shlib
29. $STF_SUITE/tests/functional/cli_root/zpool_create/zpool_create.shlib
30
31#
32# DESCRIPTION:
33#	'zpool create -o ashift=<n> ...' should work with different ashift
34#	values.
35#
36# STRATEGY:
37#	1. Create various pools with different ashift values.
38#	2. Verify -o ashift=<n> works only with allowed values (9-16).
39#	   Also verify that the lowest number of uberblocks in a label is 16 and
40#	   smallest uberblock size is 8K even with higher ashift values.
41#
42
43verify_runnable "global"
44
45function cleanup
46{
47	poolexists $TESTPOOL && destroy_pool $TESTPOOL
48	rm -f $disk
49}
50
51#
52# Fill the uberblock ring in every <device> label: we do this by committing
53# TXGs to the provided <pool> until every slot contains a valid uberblock.
54# NOTE: We use 'zpool sync' here because we can't force it via sync(1) like on
55# illumos
56#
57function write_device_uberblocks # <device> <pool>
58{
59	typeset device=$1
60	typeset pool=$2
61
62	while [ "$(zdb -quuul $device | grep -c 'invalid')" -ne 0 ]
63	do
64		sync_pool $pool true
65	done
66}
67
68#
69# Verify every label on <device> contains <count> (valid) uberblocks
70#
71function verify_device_uberblocks # <device> <count>
72{
73	typeset device=$1
74	typeset ubcount=$2
75
76	zdb -quuul $device | awk -v ubcount=$ubcount '
77	    /Uberblock/ && ! /invalid/ { uberblocks[$0]++ }
78	    END {
79	        count = 0
80	        for (i in uberblocks) {
81	            if (uberblocks[i] != 4) {
82	                printf "%s count: %s != 4\n", i, uberblocks[i]
83	                exit 1
84	            }
85	            count++;
86	        }
87	        if (count != ubcount) {
88	            printf "Total uberblock count: %s != %s\n", count, ubcount
89	            exit 1
90	        }
91	    }'
92
93	return $?
94}
95
96log_assert "zpool create -o ashift=<n>' works with different ashift values"
97log_onexit cleanup
98
99disk=$(create_blockfile $SIZE)
100
101typeset ashifts=("9" "10" "11" "12" "13" "14" "15" "16")
102# since Illumos 4958 the largest uberblock is 8K so we have at least of 16/label
103typeset ubcount=("128" "128" "64" "32" "16" "16" "16" "16")
104typeset -i i=0;
105while [ $i -lt "${#ashifts[@]}" ]
106do
107	typeset ashift=${ashifts[$i]}
108	log_must zpool create -o ashift=$ashift $TESTPOOL $disk
109	typeset pprop=$(get_pool_prop ashift $TESTPOOL)
110	verify_ashift $disk $ashift
111	if [[ $? -ne 0 || "$pprop" != "$ashift" ]]
112	then
113		log_fail "Pool was created without setting ashift value to "\
114		    "$ashift (current = $pprop)"
115	fi
116	write_device_uberblocks $disk $TESTPOOL
117	verify_device_uberblocks $disk ${ubcount[$i]}
118	if [[ $? -ne 0 ]]
119	then
120		log_fail "Pool was created with unexpected number of uberblocks"
121	fi
122	# clean things for the next run
123	log_must zpool destroy $TESTPOOL
124	log_must zpool labelclear $disk
125	log_must verify_device_uberblocks $disk 0
126	((i = i + 1))
127done
128
129typeset badvals=("off" "on" "1" "8" "17" "1b" "ff" "-")
130for badval in ${badvals[@]}
131do
132	log_mustnot zpool create -o ashift="$badval" $TESTPOOL $disk
133done
134
135log_pass "zpool create -o ashift=<n>' works with different ashift values"
136