2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (C) 2020 SUSE Linux Products GmbH. All Rights Reserved.
7 # Test all existent mount options of btrfs
8 # * device= argument is already being test by btrfs/125
9 # * space cache test already covered by test btrfs/131
11 _begin_fstest auto quick
13 _register_cleanup "cleanup"
15 # Import common functions.
27 # Compare the mounted flags with $opt_check. When the comparison fails, $opt is
28 # echoed to help to track which option was used to trigger the unexpected
38 active_opt=$(cat /proc/self/mounts | grep $SCRATCH_MNT | \
39 $AWK_PROG '{ print $4 }')
41 if [ "$opt_check" != "$DEFAULT_OPTS" ]; then
42 # We only care about the common things between defaults and the
43 # active set, so strip out the uniq lines between the two, and
44 # then we'll add this to our $opt_check which should equal
45 # $active_opt. We also strip 'rw' as we may be checking 'ro',
46 # so we need to adjust that accordingly
47 stripped=$(echo "$DEFAULT_OPTS,$active_opt" | tr ',' '\n' | \
48 sort | grep -v 'rw' | uniq -d | tr '\n' ',' | \
50 opt_check="$opt_check,$stripped"
53 # We diff by putting our wanted opts together with the current opts,
54 # turning it into one option per line, sort'ing, and then printing out
55 # any uniq lines left. This will catch anything that is set that we're
56 # not expecting, or anything that wasn't set that we wanted.
58 # We strip 'rw' because some tests flip ro, so just ignore rw.
59 diff=$(echo "$opt_check,$active_opt" | tr ',' '\n' | \
60 sort | grep -v 'rw' | uniq -u)
61 if [ -n "$diff" ]; then
62 echo "Unexepcted mount options, checking for '$opt_check' in '$active_opt' using '$opt'"
66 # Mounts using opt ($1), remounts using remount_opt ($2), and remounts again
67 # using opt again (1), checking if the mount opts are being enabled/disabled by
68 # using _check arguments ($3 and $4)
69 test_enable_disable_mount_opt()
74 local remount_opt_check
78 remount_opt_check="$4"
80 _scratch_mount "-o $opt"
82 test_mount_flags $opt $opt_check
84 _scratch_remount $remount_opt
86 test_mount_flags $remount_opt $remount_opt_check
90 test_mount_flags $opt $opt_check
95 # Checks if mount options are applied and reverted correctly.
96 # By using options to mount ($1) and remount ($2), this function will mount,
97 # remount, and the mount with the original args, checking if the mount options
98 # match the _check args ($3 and $4).
100 # Later, opt and remount_opt are swapped, testing the counterpart option if used
101 # to first mount the fs.
102 test_roundtrip_mount()
107 local remount_opt_check
111 remount_opt_check="$4"
113 # invert the args to make sure that both options work at mount and
115 test_enable_disable_mount_opt $opt $opt_check $remount_opt $remount_opt_check
116 test_enable_disable_mount_opt $remount_opt $remount_opt_check $opt $opt_check
119 # Just mount and check if the options were mounted correctly by comparing the
120 # results with $opt_check
129 _scratch_mount "-o $opt"
131 test_mount_flags $opt $opt_check
136 # Test mount options that should fail, usually by wrong arguments to options
142 # wrong $opt on purpose, should fail
143 _try_scratch_mount "-o $opt" >/dev/null 2>&1
144 if [ $? -ne 0 ]; then
147 echo "Option $opt should fail to mount"
151 # Try to mount using $opt, and bail our if the mount fails without errors. If
152 # the mount succeeds, then compare the mount options with $opt_check
153 test_optional_mount_opts()
160 # $opt not enabled, return without running any tests
161 _try_scratch_mount "-o $opt" >/dev/null 2>&1 || return
164 # option enabled, run the test
165 test_mount_opt $opt $opt_check
168 # Testes related to subvolumes, from subvol and subvolid options.
171 test_should_fail "subvol=vol2"
173 _scratch_mount "-o subvol=vol1"
174 if [ ! -f "$SCRATCH_MNT/file.txt" ]; then
175 echo "file.txt not found inside vol1 using subvol=vol1 mount option"
179 test_should_fail "subvolid=222"
181 _scratch_mount "-o subvolid=256"
182 if [ ! -f "$SCRATCH_MNT/file.txt" ]; then
183 echo "file.txt not found inside vol1 using subvolid=256 mount option"
187 # subvol and subvolid should point to the same subvolume
188 test_should_fail "-o subvol=vol1,subvolid=1234132"
190 test_mount_opt "subvol=vol1,subvolid=256" "subvolid=256,subvol=/vol1"
191 test_roundtrip_mount "subvol=vol1" "subvolid=256,subvol=/vol1" "subvolid=256" "subvolid=256,subvol=/vol1"
194 # These options are enable at kernel compile time, so no bother if they fail
195 test_optional_kernel_features()
197 # Test options that are enabled by kernel config, and so can fail safely
198 test_optional_mount_opts "check_int" "check_int"
199 test_optional_mount_opts "check_int_data" "check_int_data"
200 test_optional_mount_opts "check_int_print_mask=123" "check_int_print_mask=123"
202 test_should_fail "fragment=invalid"
203 test_optional_mount_opts "fragment=all" "fragment=data,fragment=metadata"
204 test_optional_mount_opts "fragment=data" "fragment=data"
205 test_optional_mount_opts "fragment=metadata" "fragment=metadata"
208 test_non_revertible_options()
210 test_mount_opt "clear_cache" "clear_cache"
211 test_mount_opt "degraded" "degraded"
213 test_mount_opt "inode_cache" "inode_cache"
215 # nologreplay should be used only with
216 test_should_fail "nologreplay"
217 test_mount_opt "nologreplay,ro" "ro,rescue=nologreplay"
219 # norecovery should be used only with. This options is an alias to nologreplay
220 test_should_fail "norecovery"
221 test_mount_opt "norecovery,ro" "ro,rescue=nologreplay"
222 test_mount_opt "rescan_uuid_tree" "rescan_uuid_tree"
223 test_mount_opt "skip_balance" "skip_balance"
224 test_mount_opt "user_subvol_rm_allowed" "user_subvol_rm_allowed"
226 test_should_fail "rescue=invalid"
228 # nologreplay requires readonly
229 test_should_fail "rescue=nologreplay"
230 test_mount_opt "rescue=nologreplay,ro" "ro,rescue=nologreplay"
233 # All these options can be reverted (with their "no" counterpart), or can have
234 # their values set to default on remount
235 test_revertible_options()
237 test_roundtrip_mount "acl" "$DEFAULT_OPTS" "noacl" "noacl"
238 test_roundtrip_mount "autodefrag" "autodefrag" "noautodefrag" "$DEFAULT_OPTS"
239 test_roundtrip_mount "barrier" "$DEFAULT_OPTS" "nobarrier" "nobarrier"
241 test_should_fail "commit=-10"
242 # commit=0 sets the default, so btrfs hides this mount opt
243 test_roundtrip_mount "commit=35" "commit=35" "commit=0" "$DEFAULT_OPTS"
245 test_should_fail "compress=invalid"
246 test_should_fail "compress-force=invalid"
247 test_roundtrip_mount "compress" "compress=zlib:3" "compress=lzo" "compress=lzo"
248 test_roundtrip_mount "compress=zstd" "compress=zstd:3" "compress=no" "$DEFAULT_OPTS"
249 test_roundtrip_mount "compress-force=no" "$DEFAULT_OPTS" "compress-force=zstd" "compress-force=zstd:3"
250 # zlib's max level is 9 and zstd's max level is 15
251 test_roundtrip_mount "compress=zlib:20" "compress=zlib:9" "compress=zstd:16" "compress=zstd:15"
252 test_roundtrip_mount "compress-force=lzo" "compress-force=lzo" "compress-force=zlib:4" "compress-force=zlib:4"
254 # on remount, if we only pass datacow after nodatacow was used it will remain with nodatasum
255 test_roundtrip_mount "nodatacow" "nodatasum,nodatacow" "datacow,datasum" "$DEFAULT_OPTS"
256 # nodatacow disabled compression
257 test_roundtrip_mount "compress-force" "compress-force=zlib:3" "nodatacow" "nodatasum,nodatacow"
259 # nodatacow disabled both datacow and datasum, and datasum enabled datacow and datasum
260 test_roundtrip_mount "nodatacow" "nodatasum,nodatacow" "datasum" "$DEFAULT_OPTS"
261 test_roundtrip_mount "nodatasum" "nodatasum" "datasum" "$DEFAULT_OPTS"
263 test_should_fail "discard=invalid"
264 test_roundtrip_mount "discard" "discard" "discard=sync" "discard"
265 test_roundtrip_mount "discard=async" "discard=async" "discard=sync" "discard"
266 test_roundtrip_mount "discard=sync" "discard" "nodiscard" "$DEFAULT_OPTS"
268 test_roundtrip_mount "enospc_debug" "enospc_debug" "noenospc_debug" "$DEFAULT_OPTS"
270 test_should_fail "fatal_errors=pani"
271 # fatal_errors=bug is the default
272 test_roundtrip_mount "fatal_errors=panic" "fatal_errors=panic" "fatal_errors=bug" "$DEFAULT_OPTS"
274 test_roundtrip_mount "flushoncommit" "flushoncommit" "noflushoncommit" "$DEFAULT_OPTS"
276 # 2048 is the max_inline default value
277 test_roundtrip_mount "max_inline=1024" "max_inline=1024" "max_inline=2048" "$DEFAULT_OPTS"
279 test_roundtrip_mount "metadata_ratio=0" "$DEFAULT_OPTS" "metadata_ratio=10" "metadata_ratio=10"
281 # ssd_spread implies ssd, while nossd_spread only disables ssd_spread
282 test_roundtrip_mount "ssd_spread" "ssd_spread" "nossd" "nossd"
283 test_roundtrip_mount "ssd" "ssd" "nossd" "nossd"
284 test_mount_opt "ssd" "ssd"
286 test_should_fail "thread_pool=-10"
287 test_should_fail "thread_pool=0"
288 test_roundtrip_mount "thread_pool=10" "thread_pool=10" "thread_pool=50" "thread_pool=50"
290 test_roundtrip_mount "notreelog" "notreelog" "treelog" "$DEFAULT_OPTS"
293 # real QA test starts here
294 _scratch_mkfs >/dev/null
296 # This test checks mount options, so having random MOUNT_OPTIONS set could
297 # affect the results of a few of these tests.
300 # create a subvolume that will be used later
303 # We need to save the current default options so we can validate our changes
304 # from one mount option to the next one.
305 DEFAULT_OPTS=$(cat /proc/self/mounts | grep $SCRATCH_MNT | \
306 $AWK_PROG '{ print $4 }')
308 $BTRFS_UTIL_PROG subvolume create "$SCRATCH_MNT/vol1" > /dev/null
309 touch "$SCRATCH_MNT/vol1/file.txt"
312 test_optional_kernel_features
314 test_non_revertible_options
316 test_revertible_options
320 echo "Silence is golden"