#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2018 SUSE Linux Products GmbH. All Rights Reserved.
#
# FS QA Test No. 168
#
# Test that we are able to do send operations when one of the source snapshots
# (or subvolume) has a file that is deleted while there is still a open file
# descriptor for that file.
#
. ./common/preamble
_begin_fstest auto quick send

# Override the default cleanup function.
_cleanup()
{
	cd /
	rm -f $tmp.*
	rm -fr $send_files_dir
}

# Import common functions.
. ./common/filter

# real QA test starts here
_supported_fs btrfs
_require_test
_require_scratch
_require_btrfs_command "property"
_require_fssum

send_files_dir=$TEST_DIR/btrfs-test-$seq

rm -fr $send_files_dir
mkdir $send_files_dir

_scratch_mkfs >>$seqres.full 2>&1
_scratch_mount

# Create a subvolume used for first full send test and used to create two
# snapshots for the incremental send test.
$BTRFS_UTIL_PROG subvolume create $SCRATCH_MNT/sv1 | _filter_scratch

# Create some test files.
$XFS_IO_PROG -f -c "pwrite -S 0xf1 0 64K" $SCRATCH_MNT/sv1/foo >>$seqres.full
$XFS_IO_PROG -f -c "pwrite -S 0x7b 0 90K" $SCRATCH_MNT/sv1/bar >>$seqres.full
$XFS_IO_PROG -f -c "pwrite -S 0xea 0 256K" $SCRATCH_MNT/sv1/baz >>$seqres.full

# Flush the previous buffered writes, since setting a subvolume to RO mode
# does not do it and we want to check if the data is correctly transmitted by
# the send operations.
sync

# Keep an open file descriptor on file bar.
exec 73<$SCRATCH_MNT/sv1/bar

# While the file descriptor is open, delete the file, set the subvolume to
# read-only mode and see if a full send operation succeeds.
unlink $SCRATCH_MNT/sv1/bar
$BTRFS_UTIL_PROG property set $SCRATCH_MNT/sv1 ro true
$FSSUM_PROG -A -f -w $send_files_dir/sv1.fssum $SCRATCH_MNT/sv1
$BTRFS_UTIL_PROG send -f $send_files_dir/sv1.send $SCRATCH_MNT/sv1 2>&1 \
	| _filter_scratch

# Now that the we did the full send, close the file descriptor and set the
# subvolume back to read-write mode.
exec 73>&-
$BTRFS_UTIL_PROG property set $SCRATCH_MNT/sv1 ro false

# Now try an incremental send operation while there's an open file descriptor
# for a file that was deleted from the send snapshot (while it was in read-write
# mode).

# Create a snapshot of the subvolume, to be used later as the parent snapshot
# for an incremental send operation.
$BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT/sv1 $SCRATCH_MNT/snap1 \
	| _filter_scratch

# First do a full send of this snapshot.
$FSSUM_PROG -A -f -w $send_files_dir/snap1.fssum $SCRATCH_MNT/snap1
$BTRFS_UTIL_PROG send -f $send_files_dir/snap1.send $SCRATCH_MNT/snap1 2>&1 \
	| _filter_scratch

# Modify file baz, to check that the incremental send operation does not miss
# that this file has changed.
$XFS_IO_PROG -c "pwrite -S 0x19 4K 8K" $SCRATCH_MNT/sv1/baz >>$seqres.full

# Create a second snapshot of the subvolume, to be used later as the send
# snapshot of an incremental send operation.
$BTRFS_UTIL_PROG subvolume snapshot -r $SCRATCH_MNT/sv1 $SCRATCH_MNT/snap2 \
	| _filter_scratch

# Temporarily turn the second snapshot to read-write mode and then open a file
# descriptor on its foo file.
$BTRFS_UTIL_PROG property set $SCRATCH_MNT/snap2 ro false
exec 73<$SCRATCH_MNT/snap2/foo

# Delete the foo file from the second snapshot while holding the file descriptor
# open.
unlink $SCRATCH_MNT/snap2/foo

# Set the second snapshot back to RO mode, so that we can use it for the
# incremental send operation.
$BTRFS_UTIL_PROG property set $SCRATCH_MNT/snap2 ro true

# Do the incremental send while there's an open file descriptor on file foo from
# the second snapshot.
$FSSUM_PROG -A -f -w $send_files_dir/snap2.fssum $SCRATCH_MNT/snap2
$BTRFS_UTIL_PROG send -f $send_files_dir/snap2.send -p $SCRATCH_MNT/snap1 \
	$SCRATCH_MNT/snap2 2>&1 | _filter_scratch

# Now that the incremental send is done close the file descriptor on snap2/foo.
exec 73>&-

# Recreate the filesystem using the send streams and check all files exist,
# except for the ones we deleted while holding an open file descriptor on them,
# and with correct data and metadata (size, timestamps, ownership, permissions).
_scratch_unmount
_scratch_mkfs >>$seqres.full 2>&1
_scratch_mount

$BTRFS_UTIL_PROG receive -f $send_files_dir/sv1.send $SCRATCH_MNT
$FSSUM_PROG -r $send_files_dir/sv1.fssum $SCRATCH_MNT/sv1
$BTRFS_UTIL_PROG receive -f $send_files_dir/snap1.send $SCRATCH_MNT
$FSSUM_PROG -r $send_files_dir/snap1.fssum $SCRATCH_MNT/snap1
$BTRFS_UTIL_PROG receive -f $send_files_dir/snap2.send $SCRATCH_MNT
$FSSUM_PROG -r $send_files_dir/snap2.fssum $SCRATCH_MNT/snap2

status=0
exit
