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
72#
73# Functions to toggle on/off properties
74#
75typeset -a binary_props=('atime' 'devices' 'exec' 'readonly' 'setuid' 'xattr')
76
77if is_freebsd; then
78	binary_props+=('jailed')
79else
80	binary_props+=('zoned')
81fi
82
83# Newer Linuxes dropped non-blocking mandatory locks
84if ! is_linux || [ $(linux_version) -lt $(linux_version "4.4") ]; then
85	binary_props+=('nbmand')
86fi
87
88function toggle_prop
89{
90	typeset ds=$1
91	typeset prop=$2
92
93	typeset val=$(get_prop $prop $ds)
94	typeset newval='off'
95
96	[[ $val = $newval ]] && newval='on'
97	log_must zfs set $prop=$newval $ds
98}
99
100function toggle_binary_props
101{
102	typeset ds=$1
103	typeset prop
104
105	for prop in "${binary_props[@]}"; do
106		toggle_prop $ds $prop
107	done
108}
109
110function randomize_ds_props
111{
112	typeset ds=$1
113	typeset prop proplist val
114
115	if ds_is_volume $ds; then
116		toggle_prop $ds readonly
117		proplist="${vol_props[@]}"
118	elif ds_is_filesystem $ds; then
119		toggle_binary_props $ds
120		proplist="${fs_props[@]}"
121	else
122		log_fail "$ds is neither a volume nor a file system"
123	fi
124
125	for prop in $proplist; do
126		typeset val=$(get_rand_prop_vals $prop 1 0)
127		log_must zfs set $prop=$val $ds
128	done
129}
130