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 by Delphix. All rights reserved.
14#
15
16typeset -a compress_prop_vals=('on' 'off' 'lzjb' 'gzip' 'gzip-1' 'gzip-2'
17    'gzip-3' 'gzip-4' 'gzip-5' 'gzip-6' 'gzip-7' 'gzip-8' 'gzip-9' 'zle' 'lz4')
18typeset -a checksum_prop_vals=('on' 'off' 'fletcher2' 'fletcher4' 'sha256'
19    'noparity' 'sha512' 'skein' 'edonr')
20typeset -a recsize_prop_vals=('512' '1024' '2048' '4096' '8192' '16384'
21    '32768' '65536' '131072' '262144' '524288' '1048576')
22typeset -a aclinherit_prop_vals=('discard' 'noallow' 'restricted' 'passthrough'
23    'passthrough-x')
24typeset -a aclmode_prop_vals=('discard' 'groupmask' 'passthrough' 'restricted')
25typeset -a canmount_prop_vals=('on' 'off' 'noauto')
26typeset -a copies_prop_vals=('1' '2' '3')
27typeset -a logbias_prop_vals=('latency' 'throughput')
28typeset -a primarycache_prop_vals=('all' 'none' 'metadata')
29typeset -a redundant_metadata_prop_vals=('all' 'most')
30typeset -a secondarycache_prop_vals=('all' 'none' 'metadata')
31typeset -a snapdir_prop_vals=('hidden' 'visible')
32typeset -a sync_prop_vals=('standard' 'always' 'disabled')
33
34typeset -a fs_props=('compress' 'checksum' 'recsize' 'aclinherit' 'aclmode'
35    'canmount' 'copies' 'logbias' 'primarycache' 'redundant_metadata'
36    'secondarycache' 'snapdir' 'sync')
37typeset -a vol_props=('compress' 'checksum' 'copies' 'logbias' 'primarycache'
38    'secondarycache' 'redundant_metadata' 'sync')
39
40#
41# Given the property array passed in, return 'num_props' elements to the
42# user, excluding any elements below 'start.' This allows us to exclude
43# 'off' and 'on' which can be either unwanted, or a duplicate of another
44# property respectively.
45#
46function get_rand_prop
47{
48	typeset prop_array=($(eval echo \${$1[@]}))
49	typeset -i num_props=$2
50	typeset -i start=$3
51	typeset retstr=""
52
53	[[ -z $prop_array || -z $num_props || -z $start ]] && \
54	    log_fail "get_rand_prop: bad arguments"
55
56	typeset prop_max=$((${#prop_array[@]} - 1))
57	typeset -i i
58	for i in $(shuf -i $start-$prop_max -n $num_props); do
59		retstr="${prop_array[$i]} $retstr"
60	done
61	echo $retstr
62}
63
64function get_rand_compress
65{
66	get_rand_prop compress_prop_vals $1 2
67}
68
69function get_rand_compress_any
70{
71	get_rand_prop compress_prop_vals $1 0
72}
73
74function get_rand_checksum
75{
76	get_rand_prop checksum_prop_vals $1 2
77}
78
79function get_rand_checksum_any
80{
81	get_rand_prop checksum_prop_vals $1 0
82}
83
84function get_rand_recsize
85{
86	get_rand_prop recsize_prop_vals $1 0
87}
88
89function get_rand_large_recsize
90{
91	get_rand_prop recsize_prop_vals $1 9
92}
93
94#
95# Functions to toggle on/off properties
96#
97typeset -a binary_props=('atime' 'devices' 'exec' 'nbmand' 'readonly' 'setuid'
98    'xattr' 'zoned')
99
100function toggle_prop
101{
102	typeset ds=$1
103	typeset prop=$2
104
105	datasetexists $ds || log_fail "$ds does not exist"
106	typeset val=$(get_prop $prop $ds)
107	typeset newval='off'
108
109	[[ $val = $newval ]] && newval='on'
110	log_must zfs set $prop=$newval $ds
111}
112
113function toggle_binary_props
114{
115	typeset ds=$1
116	typeset prop
117
118	for prop in "${binary_props[@]}"; do
119		toggle_prop $ds $prop
120	done
121}
122
123function randomize_ds_props
124{
125	typeset ds=$1
126	typeset prop proplist val
127
128	datasetexists $ds || log_fail "$ds does not exist"
129	if ds_is_volume $ds; then
130		toggle_prop $ds readonly
131		proplist="${vol_props[@]}"
132	elif ds_is_filesystem $ds; then
133		toggle_binary_props $ds
134		proplist="${fs_props[@]}"
135	else
136		log_fail "$ds is neither a volume nor a file system"
137	fi
138
139	for prop in $proplist; do
140		typeset val=$(get_rand_prop "${prop}_prop_vals" 1 0)
141		log_must zfs set $prop=$val $ds
142	done
143}
144