--- /dev/null
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2025 SUSE S.A. All Rights Reserved.
+#
+# FS QA Test No. 342
+#
+# Test free space tree mount options, for newer kernels with only 2 options involed:
+# - No space cache
+# - New (default) v2 space cache
+#
+. ./common/preamble
+_begin_fstest auto quick
+
+_require_scratch
+_require_btrfs_command inspect-internal dump-super
+_require_btrfs_fs_feature free_space_tree
+
+# The block-group-tree feature relies on v2 cache, thus it doesn't support
+# "nospace_cache" mount option.
+_require_btrfs_no_block_group_tree
+
+mkfs_nocache()
+{
+ _scratch_mkfs >/dev/null 2>&1
+ _scratch_mount -o clear_cache,nospace_cache
+ _scratch_unmount
+}
+
+mkfs_v2()
+{
+ _scratch_mkfs >/dev/null 2>&1
+ _scratch_mount -o space_cache=v2
+ _scratch_unmount
+}
+
+check_fst_compat()
+{
+ compat_ro="$($BTRFS_UTIL_PROG inspect-internal dump-super "$SCRATCH_DEV" | \
+ sed -rn 's/^compat_ro_flags\s+(.*)$/\1/p')"
+ if ((compat_ro & 0x1)); then
+ echo "free space tree is enabled"
+ else
+ echo "free space tree is disabled"
+ fi
+}
+
+# Mount options might interfere.
+export MOUNT_OPTIONS=""
+
+# When the free space tree is not enabled:
+# -o space_cache=v2: enable the free space tree
+# -o clear_cache,space_cache=v2: clear the old space cache and enable the free space tree
+# We don't check the no options case or plain space_cache as that will change
+# in the future to turn on space_cache=v2.
+
+mkfs_nocache
+echo "Using no space cache"
+_scratch_mount -o nospace_cache
+check_fst_compat
+_scratch_unmount
+
+mkfs_nocache
+echo "Enabling free space tree"
+_scratch_mount -o space_cache=v2
+check_fst_compat
+_scratch_unmount
+
+# When the free space tree is enabled:
+# -o nospace_cache: error
+# no options, -o space_cache=v2: keep using the free space tree
+# -o clear_cache, -o clear_cache,space_cache=v2: clear and recreate the free space tree
+# -o clear_cache,nospace_cache: clear the free space tree
+
+mkfs_v2
+echo "Trying to mount without free space tree"
+_try_scratch_mount -o nospace_cache >/dev/null 2>&1 || echo "mount failed"
+
+mkfs_v2
+echo "Mounting existing free space tree"
+_scratch_mount
+check_fst_compat
+_scratch_unmount
+_scratch_mount -o space_cache=v2
+check_fst_compat
+_scratch_unmount
+
+mkfs_v2
+echo "Recreating free space tree"
+_scratch_mount -o clear_cache,space_cache=v2
+check_fst_compat
+_scratch_unmount
+mkfs_v2
+_scratch_mount -o clear_cache
+check_fst_compat
+_scratch_unmount
+
+mkfs_v2
+echo "Disabling free space tree"
+_scratch_mount -o clear_cache,nospace_cache
+check_fst_compat
+_scratch_unmount
+
+_exit 0
--- /dev/null
+QA output created by 342
+Using no space cache
+free space tree is disabled
+Enabling free space tree
+free space tree is enabled
+Trying to mount without free space tree
+mount failed
+Mounting existing free space tree
+free space tree is enabled
+free space tree is enabled
+Recreating free space tree
+free space tree is enabled
+free space tree is enabled
+Disabling free space tree
+free space tree is disabled