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