1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
#! /bin/ksh -p
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Copyright (c) 2013, 2016 by Delphix. All rights reserved.
# Copyright 2020 Joyent, Inc.
#
. $STF_SUITE/include/libtest.shlib
. $STF_SUITE/tests/functional/inheritance/inherit.kshlib
#
# DESCRIPTION:
# Test that properties are correctly inherited using 'zfs set',
# 'zfs inherit' and 'zfs inherit -r'.
#
# STRATEGY:
# 1) Read a configX.cfg file and create the specified datasets
# 2) Read a stateX.cfg file and execute the commands within it
# and verify that the properties have the correct values
# 3) Repeat steps 1-2 for each configX and stateX files found.
#
verify_runnable "global"
log_assert "Test properties are inherited correctly"
#
# Simple function to create specified datasets.
#
function create_dataset { #name type disks
typeset dataset=$1
typeset type=$2
typeset disks=$3
if [[ $type == "POOL" ]]; then
create_pool "$dataset" "$disks"
elif [[ $type == "CTR" ]]; then
log_must zfs create $dataset
log_must zfs set canmount=off $dataset
elif [[ $type == "FS" ]]; then
log_must zfs create $dataset
else
log_fail "Unrecognised type $type"
fi
list="$list $dataset"
}
#
# Function to walk through all the properties in a
# dataset, setting them to a 'local' value if required.
#
function init_props { #dataset init_code
typeset dataset=$1
typeset init_code=$2
typeset dir=$3
typeset -i i=0
#
# Though the effect of '-' and 'default' is the same we
# call them out via a log_note to aid in debugging the
# config files
#
if [[ $init_code == "-" ]]; then
log_note "Leaving properties for $dataset unchanged."
[[ $def_recordsize == 0 ]] && \
update_recordsize $dataset $init_code
return;
elif [[ $init_code == "default" ]]; then
log_note "Leaving properties for $dataset at default values."
[[ $def_recordsize == 0 ]] && \
update_recordsize $dataset $init_code
return;
elif [[ $init_code == "local" ]]; then
log_note "Setting properties for $dataset to local values."
while (( i < ${#prop[*]} )); do
if [[ ${prop[i]} == "recordsize" ]]; then
update_recordsize $dataset $init_code
else
if [[ ${prop[i]} == "mountpoint" ]]; then
set_n_verify_prop ${prop[i]} \
${local_val[((i/2))]}.$dir $dataset
else
set_n_verify_prop ${prop[i]} \
${local_val[((i/2))]} $dataset
fi
fi
((i = i + 2))
done
else
log_fail "Unrecognised init code $init_code"
fi
}
#
# We enter this function either to update the recordsize value
# in the default array, or to update the local value array.
#
function update_recordsize { #dataset init_code
typeset dataset=$1
typeset init_code=$2
typeset idx=0
typeset record_val
#
# First need to find where the recordsize property is
# located in the arrays
#
while (( idx < ${#prop[*]} )); do
[[ ${prop[idx]} == "recordsize" ]] && break
((idx = idx + 2))
done
((idx = idx / 2))
record_val=`get_prop recordsize $dataset`
if [[ $init_code == "-" || $init_code == "default" ]]; then
def_val[idx]=$record_val
def_recordsize=1
elif [[ $init_code == "local" ]]; then
log_must zfs set recordsize=$record_val $dataset
local_val[idx]=$record_val
fi
}
#
# The mountpoint property is slightly different from other properties and
# so is handled here. For all other properties if they are set to a specific
# value at a higher level in the data hierarchy (i.e. checksum=on) then that
# value propogates down the hierarchy unchanged, with the source field being
# set to 'inherited from <higher dataset>'.
#
# The mountpoint property is different in that while the value propogates
# down the hierarchy, the value at each level is determined by a combination
# of the top-level value and the current level in the hierarchy.
#
# For example consider the case where we have a pool (called pool1), containing
# a dataset (ctr) which in turn contains a filesystem (fs). If we set the
# mountpoint of the pool to '/mnt2' then the mountpoints for the dataset and
# filesystem are '/mnt2/ctr' and /mnt2/ctr/fs' respectively, with the 'source'
# field being set to 'inherited from pool1'.
#
# So at the filesystem level to calculate what our mountpoint property should
# be set to we walk back up the hierarchy sampling the mountpoint property at
# each level and forming up the expected mountpoint value piece by piece until
# we reach the level specified in the 'source' field, which in this example is
# the top-level pool.
#
function get_mntpt_val #dataset src index
{
typeset dataset=$1
typeset src=$2
typeset idx=$3
typeset new_path=""
typeset dset
typeset mntpt=""
if [[ $src == "local" ]]; then
# Extract mount points specific to datasets
if [[ $dataset == "TESTPOOL" ]]; then
mntpt=${local_val[idx]}.1
elif [[ $dataset == "TESTPOOL/TESTCTR" ]]; then
mntpt=${local_val[idx]}.2
else
mntpt=${local_val[idx]}.3
fi
elif [[ $src == "default" ]]; then
mntpt="/$dataset"
else
# Walk back up the hierarchy building up the
# expected mountpoint property value.
obj_name=${dataset##*/}
while [[ $src != $dataset ]]; do
dset=${dataset%/*}
mnt_val=`get_prop mountpoint $dset`
if [[ $dset == $src ]]; then
new_path=$mnt_val$new_path
else
mod_prop_val=${mnt_val##*/}
new_path="/"$mod_prop_val$new_path
fi
dataset=$dset
done
mntpt=$new_path"/"$obj_name
fi
echo $mntpt
}
#
# Simple function to verify that a property has the
# expected value.
#
function verify_prop_val #property dataset src index
{
typeset prop=$1
typeset dataset=$2
typeset src=$3
typeset idx=$4
typeset new_path=""
typeset dset
typeset exp_val
typeset prop_val
prop_val=`get_prop $prop $dataset`
# mountpoint property is handled as a special case
if [[ $prop == "mountpoint" ]]; then
exp_val=`get_mntpt_val $dataset $src $idx`
else
if [[ $src == "local" ]]; then
exp_val=${local_val[idx]}
elif [[ $src == "default" ]]; then
exp_val=${def_val[idx]}
else
#
# We are inheriting the value from somewhere
# up the hierarchy.
#
exp_val=`get_prop $prop $src`
fi
fi
if [[ $prop_val != $exp_val ]]; then
# After putback PSARC/2008/231 Apr,09,2008,
# the default value of aclinherit has changed to be
# 'restricted' instead of 'secure',
# but the old interface of 'secure' still exist
if [[ $prop != "aclinherit" || \
$exp_val != "secure" || \
$prop_val != "restricted" ]]; then
log_fail "$prop of $dataset is [$prop_val] rather "\
"than [$exp_val]"
fi
fi
}
#
# Function to read the configX.cfg files and create the specified
# dataset hierarchy
#
function scan_config { #config-file
typeset config_file=$1
DISK=${DISKS%% *}
list=""
typeset -i mount_dir=1
grep "^[^#]" $config_file | {
while read name type init ; do
create_dataset $name $type $DISK
init_props $name $init $mount_dir
((mount_dir = mount_dir + 1))
done
}
}
#
# Function to check an exit flag, calling log_fail if that exit flag
# is non-zero. Can be used from code that runs in a tight loop, which
# would otherwise result in a lot of journal output.
#
function check_failure { # int status, error message to use
typeset -i exit_flag=$1
error_message=$2
if [[ $exit_flag -ne 0 ]]; then
log_fail "$error_message"
fi
}
#
# Main function. Executes the commands specified in the stateX.cfg
# files and then verifies that all the properties have the correct
# values and 'source' fields.
#
function scan_state { #state-file
typeset state_file=$1
typeset -i i=0
typeset -i j=0
log_note "Reading state from $state_file"
while ((i < ${#prop[*]})); do
grep "^[^#]" $state_file | {
while IFS=: read target op; do
#
# The user can if they wish specify that no
# operation be performed (by specifying '-'
# rather than a command). This is not as
# useless as it sounds as it allows us to
# verify that the dataset hierarchy has been
# set up correctly as specified in the
# configX.cfg file (which includes 'set'ting
# properties at a higher level and checking
# that they propogate down to the lower levels.
#
# Note in a few places here, we use
# check_failure, rather than log_must - this
# substantially reduces journal output.
#
if [[ $op == "-" ]]; then
log_note "No operation specified"
else
export __ZFS_POOL_RESTRICT="TESTPOOL"
log_must zfs unmount -a
unset __ZFS_POOL_RESTRICT
for p in ${prop[i]} ${prop[((i+1))]}; do
zfs $op $p $target
ret=$?
check_failure $ret "zfs $op $p \
$target"
done
fi
for check_obj in $list; do
read init_src final_src
for p in ${prop[i]} ${prop[((i+1))]}; do
# check_failure to keep journal small
verify_prop_src $check_obj $p \
$final_src
ret=$?
check_failure $ret "verify" \
"_prop_src $check_obj $p" \
"$final_src"
# Again, to keep journal size down.
verify_prop_val $p $check_obj \
$final_src $j
ret=$?
check_failure $ret "verify" \
"_prop_val $check_obj $p" \
"$final_src"
done
done
done
}
((i = i + 2))
((j = j + 1))
done
}
#
# Note that we keep this list relatively short so that this test doesn't
# time out (after taking more than 10 minutes).
#
set -A prop "checksum" "" \
"compression" "" \
"atime" "" \
"sharenfs" "" \
"recordsize" "recsize" \
"mountpoint" "" \
"snapdir" "" \
"aclmode" "" \
"readonly" ""
#
# Note except for the mountpoint default value (which is handled in
# the routine itself), each property specified in the 'prop' array
# above must have a corresponding entry in the two arrays below.
#
set -A def_val "on" "off" "on" \
"off" "" \
"" "hidden" "discard" \
"off"
set -A local_val "off" "on" "off" \
"on" "" \
"$TESTDIR" "visible" "groupmask" \
"off"
#
# Global flag indicating whether the default record size had been
# read.
#
typeset def_recordsize=0
set -A config_files $(ls $STF_SUITE/tests/functional/inheritance/config*[1-9]*.cfg)
set -A state_files $(ls $STF_SUITE/tests/functional/inheritance/state*.cfg)
#
# Global list of datasets created.
#
list=""
typeset -i k=0
if [[ ${#config_files[*]} != ${#state_files[*]} ]]; then
log_fail "Must have the same number of config files " \
" (${#config_files[*]}) and state files ${#state_files[*]}"
fi
while ((k < ${#config_files[*]})); do
default_cleanup_noexit
def_recordsize=0
log_note "Testing configuration ${config_files[k]}"
scan_config ${config_files[k]}
scan_state ${state_files[k]}
((k = k + 1))
done
log_pass "Properties correctly inherited as expected"
|