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# Copyright (c) 2022 Hewlett Packard Enterprise Development LP.
15#
16
17. $STF_SUITE/include/libtest.shlib
18
19typeset -a compress_prop_vals=('off' 'lzjb' 'lz4' 'gzip' 'zle' 'zstd')
20typeset -a checksum_prop_vals=('on' 'off' 'fletcher2' 'fletcher4' 'sha256'
21    'noparity' 'sha512' 'skein' 'blake3')
22if ! is_freebsd; then
23	checksum_prop_vals+=('edonr')
24fi
25typeset -a recsize_prop_vals=('512' '1024' '2048' '4096' '8192' '16384'
26    '32768' '65536' '131072' '262144' '524288' '1048576')
27typeset -a canmount_prop_vals=('on' 'off' 'noauto')
28typeset -a copies_prop_vals=('1' '2' '3')
29typeset -a logbias_prop_vals=('latency' 'throughput')
30typeset -a primarycache_prop_vals=('all' 'none' 'metadata')
31typeset -a redundant_metadata_prop_vals=('all' 'most' 'some' 'none')
32typeset -a secondarycache_prop_vals=('all' 'none' 'metadata')
33typeset -a snapdir_prop_vals=('hidden' 'visible')
34typeset -a sync_prop_vals=('standard' 'always' 'disabled')
35
36typeset -a fs_props=('compress' 'checksum' 'recsize'
37    'canmount' 'copies' 'logbias' 'primarycache' 'redundant_metadata'
38    'secondarycache' 'snapdir' 'sync')
39typeset -a vol_props=('compress' 'checksum' 'copies' 'logbias' 'primarycache'
40    'secondarycache' 'redundant_metadata' 'sync')
41
42#
43# Given the 'prop' passed in, return 'num_vals' elements of the corresponding
44# values array to the user, excluding any elements below 'first.' This allows
45# us to exclude 'off' and 'on' which can be either unwanted, or a duplicate of
46# another property respectively.
47#
48function get_rand_prop_vals
49{
50	typeset prop=$1
51	typeset -i num_vals=$2
52	typeset -i first=$3
53
54	[[ -z $prop || -z $num_vals || -z $first ]] && \
55	    log_fail "get_rand_prop_vals: bad arguments"
56
57	typeset retstr=""
58
59	typeset prop_vals_var=${prop}_prop_vals
60	typeset -a prop_vals=($(eval echo \${${prop_vals_var}[@]}))
61
62	[[ -z $prop_vals ]] && \
63	    log_fail "get_rand_prop_vals: bad prop $prop"
64
65	typeset -i last=$((${#prop_vals[@]} - 1))
66	typeset -i i
67	for i in $(range_shuffle $first $last | head -n $num_vals); do
68		retstr="${prop_vals[$i]} $retstr"
69	done
70	echo $retstr
71}
72
73#
74# Functions to toggle on/off properties
75#
76typeset -a binary_props=('atime' 'devices' 'exec' 'readonly' 'setuid' 'xattr')
77
78if is_freebsd; then
79	binary_props+=('jailed')
80else
81	binary_props+=('zoned')
82fi
83
84# Newer Linuxes dropped non-blocking mandatory locks
85if ! is_linux || [ $(linux_version) -lt $(linux_version "4.4") ]; then
86	binary_props+=('nbmand')
87fi
88
89function toggle_prop
90{
91	typeset ds=$1
92	typeset prop=$2
93
94	typeset val=$(get_prop $prop $ds)
95	typeset newval='off'
96
97	[[ $val = $newval ]] && newval='on'
98	log_must zfs set $prop=$newval $ds
99}
100
101function toggle_binary_props
102{
103	typeset ds=$1
104	typeset prop
105
106	for prop in "${binary_props[@]}"; do
107		toggle_prop $ds $prop
108	done
109}
110
111function randomize_ds_props
112{
113	typeset ds=$1
114	typeset prop proplist val
115
116	if ds_is_volume $ds; then
117		toggle_prop $ds readonly
118		proplist="${vol_props[@]}"
119	elif ds_is_filesystem $ds; then
120		toggle_binary_props $ds
121		proplist="${fs_props[@]}"
122	else
123		log_fail "$ds is neither a volume nor a file system"
124	fi
125
126	for prop in $proplist; do
127		typeset val=$(get_rand_prop_vals $prop 1 0)
128		log_must zfs set $prop=$val $ds
129	done
130}
131