Max Reitz | 0e8a371 | 2015-02-10 15:02:32 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Test cases for qcow2 refcount table growth |
| 4 | # |
| 5 | # Copyright (C) 2015 Red Hat, Inc. |
| 6 | # |
| 7 | # This program is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; either version 2 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | # |
| 20 | |
| 21 | # creator |
| 22 | owner=mreitz@redhat.com |
| 23 | |
| 24 | seq="$(basename $0)" |
| 25 | echo "QA output created by $seq" |
| 26 | |
| 27 | here="$PWD" |
Max Reitz | 0e8a371 | 2015-02-10 15:02:32 -0500 | [diff] [blame] | 28 | status=1 # failure is the default! |
| 29 | |
| 30 | _cleanup() |
| 31 | { |
| 32 | _cleanup_test_img |
| 33 | } |
| 34 | trap "_cleanup; exit \$status" 0 1 2 3 15 |
| 35 | |
| 36 | # get standard environment, filters and checks |
| 37 | . ./common.rc |
| 38 | . ./common.filter |
| 39 | |
| 40 | _supported_fmt qcow2 |
| 41 | _supported_proto file |
| 42 | _supported_os Linux |
| 43 | |
| 44 | echo |
| 45 | echo '=== New refcount structures may not conflict with existing structures ===' |
| 46 | |
| 47 | echo |
| 48 | echo '--- Test 1 ---' |
| 49 | echo |
| 50 | |
| 51 | # Preallocation speeds up the write operation, but preallocating everything will |
| 52 | # destroy the purpose of the write; so preallocate one KB less than what would |
| 53 | # cause a reftable growth... |
| 54 | IMGOPTS='preallocation=metadata,cluster_size=1k' _make_test_img 64512K |
| 55 | # ...and make the image the desired size afterwards. |
| 56 | $QEMU_IMG resize "$TEST_IMG" 65M |
| 57 | |
| 58 | # The first write results in a growth of the refcount table during an allocation |
| 59 | # which has precisely the required size so that the new refcount block allocated |
| 60 | # in alloc_refcount_block() is right after cluster_index; this did lead to a |
| 61 | # different refcount block being written to disk (a zeroed cluster) than what is |
| 62 | # cached (a refblock with one entry having a refcount of 1), and the second |
| 63 | # write would then result in that cached cluster being marked dirty and then |
| 64 | # in it being written to disk. |
| 65 | # This should not happen, the new refcount structures may not conflict with |
| 66 | # new_block. |
| 67 | # (Note that for some reason, 'write 63M 1K' does not trigger the problem) |
| 68 | $QEMU_IO -c 'write 62M 1025K' -c 'write 64M 1M' "$TEST_IMG" | _filter_qemu_io |
| 69 | |
| 70 | _check_test_img |
| 71 | |
| 72 | |
| 73 | echo |
| 74 | echo '--- Test 2 ---' |
| 75 | echo |
| 76 | |
| 77 | IMGOPTS='preallocation=metadata,cluster_size=1k' _make_test_img 64513K |
| 78 | # This results in an L1 table growth which in turn results in some clusters at |
| 79 | # the start of the image becoming free |
| 80 | $QEMU_IMG resize "$TEST_IMG" 65M |
| 81 | |
| 82 | # This write results in a refcount table growth; but the refblock allocated |
| 83 | # immediately before that (new_block) takes cluster index 4 (which is now free) |
| 84 | # and is thus not self-describing (in contrast to test 1, where new_block was |
| 85 | # self-describing). The refcount table growth algorithm then used to place the |
| 86 | # new refcount structures at cluster index 65536 (which is the same as the |
| 87 | # cluster_index parameter in this case), allocating a new refcount block for |
| 88 | # that cluster while new_block already existed, leaking new_block. |
| 89 | # Therefore, the new refcount structures may not be put at cluster_index |
| 90 | # (because new_block already describes that cluster, and the new structures try |
| 91 | # to be self-describing). |
| 92 | $QEMU_IO -c 'write 63M 130K' "$TEST_IMG" | _filter_qemu_io |
| 93 | |
| 94 | _check_test_img |
| 95 | |
Alberto Garcia | abf754f | 2018-03-21 15:38:52 +0200 | [diff] [blame] | 96 | echo |
| 97 | echo '=== Allocating a new refcount block must not leave holes in the image ===' |
| 98 | echo |
| 99 | |
| 100 | IMGOPTS='cluster_size=512,refcount_bits=16' _make_test_img 1M |
| 101 | |
| 102 | # This results in an image with 256 used clusters: the qcow2 header, |
| 103 | # the refcount table, one refcount block, the L1 table, four L2 tables |
| 104 | # and 248 data clusters |
| 105 | $QEMU_IO -c 'write 0 124k' "$TEST_IMG" | _filter_qemu_io |
| 106 | |
| 107 | # 256 clusters of 512 bytes each give us a 128K image |
| 108 | stat -c "size=%s (expected 131072)" $TEST_IMG |
| 109 | |
| 110 | # All 256 entries of the refcount block are used, so writing a new |
| 111 | # data cluster also allocates a new refcount block |
| 112 | $QEMU_IO -c 'write 124k 512' "$TEST_IMG" | _filter_qemu_io |
| 113 | |
| 114 | # Two more clusters, the image size should be 129K now |
| 115 | stat -c "size=%s (expected 132096)" $TEST_IMG |
Max Reitz | 0e8a371 | 2015-02-10 15:02:32 -0500 | [diff] [blame] | 116 | |
| 117 | # success, all done |
| 118 | echo |
| 119 | echo '*** done' |
| 120 | rm -f $seq.full |
| 121 | status=0 |