#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2024 Oracle.  All Rights Reserved.
#
# FS QA Test 741
#
# Attempt to mount both the DM physical device and the DM flakey device.
# Verify the returned error message.
#
. ./common/preamble
_begin_fstest auto quick volume tempfsid

# Override the default cleanup function.
_cleanup()
{
	# If by somehow the fs mounted the underlying device (twice), we have
	# to  make sure $extra_mnt is not mounted, or TEST_DEV can not be
	# unmounted for fsck.
	_unmount $extra_mnt &> /dev/null
	_unmount $extra_mnt &> /dev/null
	rm -rf $extra_mnt
	_scratch_unmount
	_cleanup_flakey
	cd /
	rm -r -f $tmp.*
}

# Import common functions.
. ./common/filter
. ./common/dmflakey

_require_test
_require_scratch
_require_dm_target flakey

_fixed_by_fs_commit btrfs 2f1aeab9fca1 \
			"btrfs: return accurate error code on open failure"

_scratch_mkfs >> $seqres.full
_init_flakey
_scratch_mount

extra_mnt=$TEST_DIR/extra_mnt
rm -rf $extra_mnt
mkdir -p $extra_mnt

# Mount must fail because the physical device has a dm created on it.
# Filters alter the return code of the mount.
_mount $NON_FLAKEY_DEV $extra_mnt 2>/dev/null && \
	_fail "mount of busy device succeeded"

# Try again with flakey unmounted, must also fail.
_scratch_unmount
_mount $NON_FLAKEY_DEV $extra_mnt 2>/dev/null && \
	_fail "mount of busy device succeeded"

# Removing dm should make mount successful.
_cleanup_flakey
_scratch_mount

status=0
exit
