1#
2# This file and its contents are supplied under the terms of the
3# Common Development and Distribution License ("CDDL"), version 1.0.
4# You may only use this file in accordance with the terms of version
5# 1.0 of the CDDL.
6#
7# A full copy of the text of the CDDL should have accompanied this
8# source.  A copy of the CDDL is also available via the Internet at
9# http://www.illumos.org/license/CDDL.
10#
11
12#
13# Copyright (c) 2012, 2016, Delphix. All rights reserved.
14#
15
16. $STF_SUITE/include/libtest.shlib
17
18typeset -a compress_prop_vals=('off' 'lzjb' 'lz4' 'gzip' 'zle' 'zstd')
19typeset -a checksum_prop_vals=('on' 'off' 'fletcher2' 'fletcher4' 'sha256'
20    'noparity' 'sha512' 'skein')
21if ! is_freebsd; then
22	checksum_prop_vals+=('edonr')
23fi
24typeset -a recsize_prop_vals=('512' '1024' '2048' '4096' '8192' '16384'
25    '32768' '65536' '131072' '262144' '524288' '1048576')
26typeset -a canmount_prop_vals=('on' 'off' 'noauto')
27typeset -a copies_prop_vals=('1' '2' '3')
28typeset -a logbias_prop_vals=('latency' 'throughput')
29typeset -a primarycache_prop_vals=('all' 'none' 'metadata')
30typeset -a redundant_metadata_prop_vals=('all' 'most')
31typeset -a secondarycache_prop_vals=('all' 'none' 'metadata')
32typeset -a snapdir_prop_vals=('hidden' 'visible')
33typeset -a sync_prop_vals=('standard' 'always' 'disabled')
34
35typeset -a fs_props=('compress' 'checksum' 'recsize'
36    'canmount' 'copies' 'logbias' 'primarycache' 'redundant_metadata'
37    'secondarycache' 'snapdir' 'sync')
38typeset -a vol_props=('compress' 'checksum' 'copies' 'logbias' 'primarycache'
39    'secondarycache' 'redundant_metadata' 'sync')
40
41#
42# Given the 'prop' passed in, return 'num_vals' elements of the corresponding
43# values array to the user, excluding any elements below 'first.' This allows
44# us to exclude 'off' and 'on' which can be either unwanted, or a duplicate of
45# another property respectively.
46#
47function get_rand_prop_vals
48{
49	typeset prop=$1
50	typeset -i num_vals=$2
51	typeset -i first=$3
52
53	[[ -z $prop || -z $num_vals || -z $first ]] && \
54	    log_fail "get_rand_prop_vals: bad arguments"
55
56	typeset retstr=""
57
58	typeset prop_vals_var=${prop}_prop_vals
59	typeset -a prop_vals=($(eval echo \${${prop_vals_var}[@]}))
60
61	[[ -z $prop_vals ]] && \
62	    log_fail "get_rand_prop_vals: bad prop $prop"
63
64	typeset -i last=$((${#prop_vals[@]} - 1))
65	typeset -i i
66	for i in $(range_shuffle $first $last | head -n $num_vals); do
67		retstr="${prop_vals[$i]} $retstr"
68	done
69	echo $retstr
70}
71
72function get_rand_checksum
73{
74	get_rand_prop_vals checksum $1 2
75}
76
77function get_rand_checksum_any
78{
79	get_rand_prop_vals checksum $1 0
80}
81
82function get_rand_recsize
83{
84	get_rand_prop_vals recsize $1 0
85}
86
87function get_rand_large_recsize
88{
89	get_rand_prop_vals recsize $1 9
90}
91
92#
93# Functions to toggle on/off properties
94#
95typeset -a binary_props=('atime' 'devices' 'exec' 'readonly' 'setuid' 'xattr')
96
97if is_freebsd; then
98	binary_props+=('jailed')
99else
100	binary_props+=('zoned')
101fi
102
103if is_linux; then
104	# Only older kernels support non-blocking mandatory locks
105	if [[ $(linux_version) -lt $(linux_version "4.4") ]]; then
106		binary_props+=('nbmand')
107	fi
108else
109	binary_props+=('nbmand')
110fi
111
112function toggle_prop
113{
114	typeset ds=$1
115	typeset prop=$2
116
117	datasetexists $ds || log_fail "$ds does not exist"
118	typeset val=$(get_prop $prop $ds)
119	typeset newval='off'
120
121	[[ $val = $newval ]] && newval='on'
122	log_must zfs set $prop=$newval $ds
123}
124
125function toggle_binary_props
126{
127	typeset ds=$1
128	typeset prop
129
130	for prop in "${binary_props[@]}"; do
131		toggle_prop $ds $prop
132	done
133}
134
135function randomize_ds_props
136{
137	typeset ds=$1
138	typeset prop proplist val
139
140	datasetexists $ds || log_fail "$ds does not exist"
141	if ds_is_volume $ds; then
142		toggle_prop $ds readonly
143		proplist="${vol_props[@]}"
144	elif ds_is_filesystem $ds; then
145		toggle_binary_props $ds
146		proplist="${fs_props[@]}"
147	else
148		log_fail "$ds is neither a volume nor a file system"
149	fi
150
151	for prop in $proplist; do
152		typeset val=$(get_rand_prop_vals $prop 1 0)
153		log_must zfs set $prop=$val $ds
154	done
155}
156