9ae3a8
From fdd55f6fb66d89ff7d241395419de8615e2063a4 Mon Sep 17 00:00:00 2001
9ae3a8
From: Max Reitz <mreitz@redhat.com>
9ae3a8
Date: Thu, 7 Nov 2013 12:16:30 +0100
9ae3a8
Subject: [PATCH 13/87] qemu-iotests: Overlapping cluster allocations
9ae3a8
9ae3a8
RH-Author: Max Reitz <mreitz@redhat.com>
9ae3a8
Message-id: <1383604354-12743-16-git-send-email-mreitz@redhat.com>
9ae3a8
Patchwork-id: 55315
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH 15/43] qemu-iotests: Overlapping cluster allocations
9ae3a8
Bugzilla: 1004347
9ae3a8
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
RH-Acked-by: Fam Zheng <famz@redhat.com>
9ae3a8
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
9ae3a8
9ae3a8
BZ: 1004347
9ae3a8
9ae3a8
A new test on corrupted images with overlapping cluster allocations.
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
9ae3a8
(cherry picked from commit ca0eca91b65c34d6e5f5c77d5c18ed3de5b26139)
9ae3a8
9ae3a8
Signed-off-by: Max Reitz <mreitz@redhat.com>
9ae3a8
9ae3a8
Conflicts:
9ae3a8
	tests/qemu-iotests/group
9ae3a8
9ae3a8
Conflicts because upstream contains more tests in the group file.
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 tests/qemu-iotests/060     |  111 ++++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 tests/qemu-iotests/060.out |   44 +++++++++++++++++
9ae3a8
 tests/qemu-iotests/group   |    1 +
9ae3a8
 3 files changed, 156 insertions(+), 0 deletions(-)
9ae3a8
 create mode 100644 tests/qemu-iotests/060
9ae3a8
 create mode 100644 tests/qemu-iotests/060.out
9ae3a8
9ae3a8
diff --git a/tests/qemu-iotests/060 b/tests/qemu-iotests/060
9ae3a8
new file mode 100644
9ae3a8
index 0000000..65bb09f
9ae3a8
--- /dev/null
9ae3a8
+++ b/tests/qemu-iotests/060
9ae3a8
@@ -0,0 +1,111 @@
9ae3a8
+#!/bin/bash
9ae3a8
+#
9ae3a8
+# Test case for image corruption (overlapping data structures) in qcow2
9ae3a8
+#
9ae3a8
+# Copyright (C) 2013 Red Hat, Inc.
9ae3a8
+#
9ae3a8
+# This program is free software; you can redistribute it and/or modify
9ae3a8
+# it under the terms of the GNU General Public License as published by
9ae3a8
+# the Free Software Foundation; either version 2 of the License, or
9ae3a8
+# (at your option) any later version.
9ae3a8
+#
9ae3a8
+# This program is distributed in the hope that it will be useful,
9ae3a8
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
9ae3a8
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9ae3a8
+# GNU General Public License for more details.
9ae3a8
+#
9ae3a8
+# You should have received a copy of the GNU General Public License
9ae3a8
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
9ae3a8
+#
9ae3a8
+
9ae3a8
+# creator
9ae3a8
+owner=mreitz@redhat.com
9ae3a8
+
9ae3a8
+seq=`basename $0`
9ae3a8
+echo "QA output created by $seq"
9ae3a8
+
9ae3a8
+here=`pwd`
9ae3a8
+tmp=/tmp/$$
9ae3a8
+status=1	# failure is the default!
9ae3a8
+
9ae3a8
+_cleanup()
9ae3a8
+{
9ae3a8
+	_cleanup_test_img
9ae3a8
+}
9ae3a8
+trap "_cleanup; exit \$status" 0 1 2 3 15
9ae3a8
+
9ae3a8
+# get standard environment, filters and checks
9ae3a8
+. ./common.rc
9ae3a8
+. ./common.filter
9ae3a8
+
9ae3a8
+# This tests qocw2-specific low-level functionality
9ae3a8
+_supported_fmt qcow2
9ae3a8
+_supported_proto generic
9ae3a8
+_supported_os Linux
9ae3a8
+
9ae3a8
+rt_offset=65536  # 0x10000 (XXX: just an assumption)
9ae3a8
+rb_offset=131072 # 0x20000 (XXX: just an assumption)
9ae3a8
+l1_offset=196608 # 0x30000 (XXX: just an assumption)
9ae3a8
+l2_offset=262144 # 0x40000 (XXX: just an assumption)
9ae3a8
+
9ae3a8
+IMGOPTS="compat=1.1"
9ae3a8
+
9ae3a8
+echo
9ae3a8
+echo "=== Testing L2 reference into L1 ==="
9ae3a8
+echo
9ae3a8
+_make_test_img 64M
9ae3a8
+# Link first L1 entry (first L2 table) onto itself
9ae3a8
+# (Note the MSb in the L1 entry is set, ensuring the refcount is one - else any
9ae3a8
+# later write will result in a COW operation, effectively ruining this attempt
9ae3a8
+# on image corruption)
9ae3a8
+poke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x03\x00\x00"
9ae3a8
+_check_test_img
9ae3a8
+
9ae3a8
+# The corrupt bit should not be set anyway
9ae3a8
+./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
9ae3a8
+
9ae3a8
+# Try to write something, thereby forcing the corrupt bit to be set
9ae3a8
+$QEMU_IO -c "write -P 0x2a 0 512" "$TEST_IMG" | _filter_qemu_io
9ae3a8
+
9ae3a8
+# The corrupt bit must now be set
9ae3a8
+./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
9ae3a8
+
9ae3a8
+# Try to open the image R/W (which should fail)
9ae3a8
+$QEMU_IO -c "read 0 512" "$TEST_IMG" 2>&1 | _filter_qemu_io | sed -e "s/can't open device .*$/can't open device/"
9ae3a8
+
9ae3a8
+# Try to open it RO (which should succeed)
9ae3a8
+$QEMU_IO -c "read 0 512" -r "$TEST_IMG" | _filter_qemu_io
9ae3a8
+
9ae3a8
+# We could now try to fix the image, but this would probably fail (how should an
9ae3a8
+# L2 table linked onto the L1 table be fixed?)
9ae3a8
+
9ae3a8
+echo
9ae3a8
+echo "=== Testing cluster data reference into refcount block ==="
9ae3a8
+echo
9ae3a8
+_make_test_img 64M
9ae3a8
+# Allocate L2 table
9ae3a8
+truncate -s "$(($l2_offset+65536))" "$TEST_IMG"
9ae3a8
+poke_file "$TEST_IMG" "$l1_offset" "\x80\x00\x00\x00\x00\x04\x00\x00"
9ae3a8
+# Mark cluster as used
9ae3a8
+poke_file "$TEST_IMG" "$(($rb_offset+8))" "\x00\x01"
9ae3a8
+# Redirect new data cluster onto refcount block
9ae3a8
+poke_file "$TEST_IMG" "$l2_offset" "\x80\x00\x00\x00\x00\x02\x00\x00"
9ae3a8
+_check_test_img
9ae3a8
+./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
9ae3a8
+$QEMU_IO -c "write -P 0x2a 0 512" "$TEST_IMG" | _filter_qemu_io
9ae3a8
+./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
9ae3a8
+
9ae3a8
+# Try to fix it
9ae3a8
+_check_test_img -r all
9ae3a8
+
9ae3a8
+# The corrupt bit should be cleared
9ae3a8
+./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
9ae3a8
+
9ae3a8
+# Look if it's really really fixed
9ae3a8
+$QEMU_IO -c "write -P 0x2a 0 512" "$TEST_IMG" | _filter_qemu_io
9ae3a8
+./qcow2.py "$TEST_IMG" dump-header | grep incompatible_features
9ae3a8
+
9ae3a8
+# success, all done
9ae3a8
+echo "*** done"
9ae3a8
+rm -f $seq.full
9ae3a8
+status=0
9ae3a8
diff --git a/tests/qemu-iotests/060.out b/tests/qemu-iotests/060.out
9ae3a8
new file mode 100644
9ae3a8
index 0000000..ca4583a
9ae3a8
--- /dev/null
9ae3a8
+++ b/tests/qemu-iotests/060.out
9ae3a8
@@ -0,0 +1,44 @@
9ae3a8
+QA output created by 060
9ae3a8
+
9ae3a8
+=== Testing L2 reference into L1 ===
9ae3a8
+
9ae3a8
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 
9ae3a8
+ERROR cluster 3 refcount=1 reference=3
9ae3a8
+
9ae3a8
+1 errors were found on the image.
9ae3a8
+Data may be corrupted, or further writes to the image may corrupt it.
9ae3a8
+incompatible_features     0x0
9ae3a8
+qcow2: Preventing invalid write on metadata (overlaps with active L1 table); image marked as corrupt.
9ae3a8
+write failed: Input/output error
9ae3a8
+incompatible_features     0x2
9ae3a8
+qcow2: Image is corrupt; cannot be opened read/write.
9ae3a8
+qemu-io: can't open device
9ae3a8
+no file open, try 'help open'
9ae3a8
+read 512/512 bytes at offset 0
9ae3a8
+512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
9ae3a8
+
9ae3a8
+=== Testing cluster data reference into refcount block ===
9ae3a8
+
9ae3a8
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=67108864 
9ae3a8
+ERROR refcount block 0 refcount=2
9ae3a8
+ERROR cluster 2 refcount=1 reference=2
9ae3a8
+
9ae3a8
+2 errors were found on the image.
9ae3a8
+Data may be corrupted, or further writes to the image may corrupt it.
9ae3a8
+incompatible_features     0x0
9ae3a8
+qcow2: Preventing invalid write on metadata (overlaps with refcount block); image marked as corrupt.
9ae3a8
+write failed: Input/output error
9ae3a8
+incompatible_features     0x2
9ae3a8
+Repairing refcount block 0 refcount=2
9ae3a8
+The following inconsistencies were found and repaired:
9ae3a8
+
9ae3a8
+    0 leaked clusters
9ae3a8
+    1 corruptions
9ae3a8
+
9ae3a8
+Double checking the fixed image now...
9ae3a8
+No errors were found on the image.
9ae3a8
+incompatible_features     0x0
9ae3a8
+wrote 512/512 bytes at offset 0
9ae3a8
+512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
9ae3a8
+incompatible_features     0x0
9ae3a8
+*** done
9ae3a8
diff --git a/tests/qemu-iotests/group b/tests/qemu-iotests/group
9ae3a8
index 1017a19..9c8826d 100644
9ae3a8
--- a/tests/qemu-iotests/group
9ae3a8
+++ b/tests/qemu-iotests/group
9ae3a8
@@ -61,4 +61,5 @@
9ae3a8
 052 rw auto backing
9ae3a8
 053 rw auto
9ae3a8
 059 rw auto
9ae3a8
+060 rw auto
9ae3a8
 068 rw auto
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8