#! /bin/bash
# FS QA Test No. 012
#
# Test btrfs-convert
#
# 1) create ext4 filesystem & populate it
# 2) convert it to btrfs, mount it, verify contents
# 3) verify archived ext4 image integriy & contents
# 4) populate btrfs fs with new data
# 5) roll back conversion to original ext4
# 6) verify rolled-back fs integrity & contents
#
#-----------------------------------------------------------------------
# Copyright (c) 2013 Red Hat, Inc.  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it would be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write the Free Software Foundation,
# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#-----------------------------------------------------------------------
#

seq=`basename $0`
seqres=$RESULT_DIR/$seq
echo "== QA output created by $seq"

here=`pwd`
tmp=/tmp/$$
status=1	# failure is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_cleanup()
{
    cd /
    rm -f $tmp.*
}

# get standard environment, filters and checks
. ./common/rc
. ./common/filter.btrfs

# real QA test starts here

# Modify as appropriate.
_supported_fs btrfs
_supported_os Linux
_require_scratch_nocheck

BTRFS_CONVERT_PROG="`set_prog_path btrfs-convert`"
E2FSCK_PROG="`set_prog_path e2fsck`"

_require_command "$BTRFS_CONVERT_PROG" btrfs-convert
_require_command "$MKFS_EXT4_PROG" mkfs.ext4
_require_command "$E2FSCK_PROG" e2fsck

rm -f $seqres.full

# Create & populate an ext4 filesystem
$MKFS_EXT4_PROG -F -b 4096 $SCRATCH_DEV > $seqres.full 2>&1 || \
	_notrun "Could not create ext4 filesystem"
# Manual mount so we don't use -t btrfs or selinux context
mount -t ext4 $SCRATCH_DEV $SCRATCH_MNT

cp -aR /lib/modules/`uname -r`/ $SCRATCH_MNT
_scratch_unmount

# Convert it to btrfs, mount it, verify the data
$BTRFS_CONVERT_PROG $SCRATCH_DEV >> $seqres.full 2>&1 || \
	_fail "btrfs-convert failed"
_scratch_mount || _fail "Could not mount new btrfs fs"
# (Ignore the symlinks which may be broken/nonexistent)
diff -r /lib/modules/`uname -r`/ $SCRATCH_MNT/`uname -r`/ 2>&1 | grep -vw "source\|build"

# Old ext4 image file should exist & be consistent
$E2FSCK_PROG -fn $SCRATCH_MNT/ext2_saved/image >> $seqres.full 2>&1 || \
	_fail "archived ext4 image is corrupt"

# And the files in that image should match
mkdir -p $SCRATCH_MNT/mnt
mount -o loop $SCRATCH_MNT/ext2_saved/image $SCRATCH_MNT/mnt || \
	_fail "could not loop mount saved ext4 image"
# Ignore the symlinks which may be broken/nonexistent
diff -r /lib/modules/`uname -r`/ $SCRATCH_MNT/mnt/`uname -r`/ 2>&1 | grep -vw "source\|build"
umount $SCRATCH_MNT/mnt

# Now put some fresh data on the btrfs fs
mkdir -p $SCRATCH_MNT/new 
cp -aR /lib/modules/`uname -r`/ $SCRATCH_MNT/new

_scratch_unmount

# Now restore the ext4 device
$BTRFS_CONVERT_PROG -r $SCRATCH_DEV >> $seqres.full 2>&1 || \
	_fail "btrfs-convert rollback failed"

# Check it again
$E2FSCK_PROG -fn $SCRATCH_DEV >> $seqres.full 2>&1 || \
        _fail "restored ext4 image is corrupt"

# Mount the un-converted ext4 device & check the contents
mount -t ext4 $SCRATCH_DEV $SCRATCH_MNT
# (Ignore the symlinks which may be broken/nonexistent)
diff -r /lib/modules/`uname -r`/ $SCRATCH_MNT/`uname -r`/ 2>&1 | grep -vw "source\|build"

_scratch_unmount

# Convert it to btrfs, mount it and delete "ext2_saved"
$BTRFS_CONVERT_PROG $SCRATCH_DEV >> $seqres.full 2>&1 || \
	_fail "btrfs-convert failed"
_scratch_mount || _fail "Could not mount new btrfs fs"
$BTRFS_UTIL_PROG subvolume delete $SCRATCH_MNT/ext2_saved >> $seqres.full 2>&1 ||
	_fail "failed to delete ext2_saved subvolume"
_scratch_unmount

# Now restore the ext4 device, expecting a failure
$BTRFS_CONVERT_PROG -r $SCRATCH_DEV >> $seqres.full 2>&1
[ $? -eq 1 ] || _fail "Failure is expected, but btrfs-convert returns with rollback complete"

# success, all done
status=0
exit
