qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
	Maria Kustova <maxa@catit.be>, Maria Kustova <maria.k@catit.be>,
	Stefan Hajnoczi <stefanha@redhat.com>
Subject: [Qemu-devel] [PULL v2 44/59] layout: Add generators for refcount table and blocks
Date: Mon, 22 Sep 2014 12:42:15 +0100	[thread overview]
Message-ID: <1411386150-24003-45-git-send-email-stefanha@redhat.com> (raw)
In-Reply-To: <1411386150-24003-1-git-send-email-stefanha@redhat.com>

From: Maria Kustova <maxa@catit.be>

Refcount structures are placed in clusters randomly selected from all
unallocated host clusters.

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Signed-off-by: Maria Kustova <maria.k@catit.be>
Reviewed-by: Fam Zheng <famz@redhat.com>
Message-id: 7e2f38608db6fba2da53997390b19400d445c45d.1408450493.git.maria.k@catit.be
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 tests/image-fuzzer/qcow2/layout.py | 138 ++++++++++++++++++++++++++++++++++++-
 1 file changed, 137 insertions(+), 1 deletion(-)

diff --git a/tests/image-fuzzer/qcow2/layout.py b/tests/image-fuzzer/qcow2/layout.py
index 730c771..63e801f 100644
--- a/tests/image-fuzzer/qcow2/layout.py
+++ b/tests/image-fuzzer/qcow2/layout.py
@@ -102,6 +102,8 @@ class Image(object):
         self.end_of_extension_area = FieldsList()
         self.l2_tables = FieldsList()
         self.l1_table = FieldsList()
+        self.refcount_table = FieldsList()
+        self.refcount_blocks = FieldsList()
         self.ext_offset = 0
         self.create_header(cluster_bits, backing_file_name)
         self.set_backing_file_name(backing_file_name)
@@ -113,7 +115,8 @@ class Image(object):
     def __iter__(self):
         return chain(self.header, self.backing_file_format,
                      self.feature_name_table, self.end_of_extension_area,
-                     self.backing_file_name, self.l1_table, self.l2_tables)
+                     self.backing_file_name, self.l1_table, self.l2_tables,
+                     self.refcount_table, self.refcount_blocks)
 
     def create_header(self, cluster_bits, backing_file_name=None):
         """Generate a random valid header."""
@@ -330,6 +333,138 @@ class Image(object):
                                                 float(self.cluster_size**2)))
         self.header['l1_table_offset'][0].value = l1_offset
 
+    def create_refcount_structures(self):
+        """Generate random refcount blocks and refcount table."""
+        def allocate_rfc_blocks(data, size):
+            """Return indices of clusters allocated for refcount blocks."""
+            cluster_ids = set()
+            diff = block_ids = set([x / size for x in data])
+            while len(diff) != 0:
+                # Allocate all yet not allocated clusters
+                new = self._get_available_clusters(data | cluster_ids,
+                                                   len(diff))
+                # Indices of new refcount blocks necessary to cover clusters
+                # in 'new'
+                diff = set([x / size for x in new]) - block_ids
+                cluster_ids |= new
+                block_ids |= diff
+            return cluster_ids, block_ids
+
+        def allocate_rfc_table(data, init_blocks, block_size):
+            """Return indices of clusters allocated for the refcount table
+            and updated indices of clusters allocated for blocks and indices
+            of blocks.
+            """
+            blocks = set(init_blocks)
+            clusters = set()
+            # Number of entries in one cluster of the refcount table
+            size = self.cluster_size / UINT64_S
+            # Number of clusters necessary for the refcount table based on
+            # the current number of refcount blocks
+            table_size = int(ceil((max(blocks) + 1) / float(size)))
+            # Index of the first cluster of the refcount table
+            table_start = self._get_adjacent_clusters(data, table_size + 1)
+            # Clusters allocated for the current length of the refcount table
+            table_clusters = set(range(table_start, table_start + table_size))
+            # Clusters allocated for the refcount table including
+            # last optional one for potential l1 growth
+            table_clusters_allocated = set(range(table_start, table_start +
+                                                 table_size + 1))
+            # New refcount blocks necessary for clusters occupied by the
+            # refcount table
+            diff = set([c / block_size for c in table_clusters]) - blocks
+            blocks |= diff
+            while len(diff) != 0:
+                # Allocate clusters for new refcount blocks
+                new = self._get_available_clusters((data | clusters) |
+                                                   table_clusters_allocated,
+                                                   len(diff))
+                # Indices of new refcount blocks necessary to cover
+                # clusters in 'new'
+                diff = set([x / block_size for x in new]) - blocks
+                clusters |= new
+                blocks |= diff
+                # Check if the refcount table needs one more cluster
+                if int(ceil((max(blocks) + 1) / float(size))) > table_size:
+                    new_block_id = (table_start + table_size) / block_size
+                    # Check if the additional table cluster needs
+                    # one more refcount block
+                    if new_block_id not in blocks:
+                        diff.add(new_block_id)
+                    table_clusters.add(table_start + table_size)
+                    table_size += 1
+            return table_clusters, blocks, clusters
+
+        def create_table_entry(table_offset, block_cluster, block_size,
+                               cluster):
+            """Generate a refcount table entry."""
+            offset = table_offset + UINT64_S * (cluster / block_size)
+            return ['>Q', offset, block_cluster * self.cluster_size,
+                    'refcount_table_entry']
+
+        def create_block_entry(block_cluster, block_size, cluster):
+            """Generate a list of entries for the current block."""
+            entry_size = self.cluster_size / block_size
+            offset = block_cluster * self.cluster_size
+            entry_offset = offset + entry_size * (cluster % block_size)
+            # While snapshots are not supported all refcounts are set to 1
+            return ['>H', entry_offset, 1, 'refcount_block_entry']
+        # Size of a block entry in bits
+        refcount_bits = 1 << self.header['refcount_order'][0].value
+        # Number of refcount entries per refcount block
+        # Convert self.cluster_size from bytes to bits to have the same
+        # base for the numerator and denominator
+        block_size = self.cluster_size * 8 / refcount_bits
+        meta_data = self._get_metadata()
+        if len(self.data_clusters) == 0:
+            # All metadata for an empty guest image needs 4 clusters:
+            # header, rfc table, rfc block, L1 table.
+            # Header takes cluster #0, other clusters ##1-3 can be used
+            block_clusters = set([random.choice(list(set(range(1, 4)) -
+                                                     meta_data))])
+            block_ids = set([0])
+            table_clusters = set([random.choice(list(set(range(1, 4)) -
+                                                     meta_data -
+                                                     block_clusters))])
+        else:
+            block_clusters, block_ids = \
+                                allocate_rfc_blocks(self.data_clusters |
+                                                    meta_data, block_size)
+            table_clusters, block_ids, new_clusters = \
+                                    allocate_rfc_table(self.data_clusters |
+                                                       meta_data |
+                                                       block_clusters,
+                                                       block_ids,
+                                                       block_size)
+            block_clusters |= new_clusters
+
+        meta_data |= block_clusters | table_clusters
+        table_offset = min(table_clusters) * self.cluster_size
+        block_id = None
+        # Clusters allocated for refcount blocks
+        block_clusters = list(block_clusters)
+        # Indices of refcount blocks
+        block_ids = list(block_ids)
+        # Refcount table entries
+        rfc_table = []
+        # Refcount entries
+        rfc_blocks = []
+
+        for cluster in sorted(self.data_clusters | meta_data):
+            if cluster / block_size != block_id:
+                block_id = cluster / block_size
+                block_cluster = block_clusters[block_ids.index(block_id)]
+                rfc_table.append(create_table_entry(table_offset,
+                                                    block_cluster,
+                                                    block_size, cluster))
+            rfc_blocks.append(create_block_entry(block_cluster, block_size,
+                                                 cluster))
+        self.refcount_table = FieldsList(rfc_table)
+        self.refcount_blocks = FieldsList(rfc_blocks)
+
+        self.header['refcount_table_offset'][0].value = table_offset
+        self.header['refcount_table_clusters'][0].value = len(table_clusters)
+
     def fuzz(self, fields_to_fuzz=None):
         """Fuzz an image by corrupting values of a random subset of its fields.
 
@@ -471,6 +606,7 @@ def create_image(test_img_path, backing_file_name=None, backing_file_fmt=None,
     image.create_feature_name_table()
     image.set_end_of_extension_area()
     image.create_l_structures()
+    image.create_refcount_structures()
     image.fuzz(fields_to_fuzz)
     image.write(test_img_path)
     return image.image_size
-- 
1.9.3

  parent reply	other threads:[~2014-09-22 11:44 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-22 11:41 [Qemu-devel] [PULL v2 00/59] Block patches Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 01/59] block/vhdx.c: Mark parent_vhdx_guid variable as unused Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 02/59] ide/atapi: Mark non-data commands as complete Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 03/59] aio-win32: fix uninitialized use of have_select_revents Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 04/59] ide/ahci: Check for -ECANCELED in aio callbacks Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 05/59] block: Add refcnt in BlockDriverAIOCB Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 06/59] block: Add bdrv_aio_cancel_async Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 07/59] block: Drop bdrv_em_co_aiocb_info.cancel Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 08/59] block: Drop bdrv_em_aiocb_info.cancel Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 09/59] thread-pool: Convert thread_pool_aiocb_info.cancel to cancel_async Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 10/59] linux-aio: Convert laio_aiocb_info.cancel to .cancel_async Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 11/59] dma: Convert dma_aiocb_info.cancel " Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 12/59] iscsi: Convert iscsi_aiocb_info.cancel " Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 13/59] archipelago: Drop archipelago_aiocb_info.cancel Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 14/59] blkdebug: Drop blkdebug_aiocb_info.cancel Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 15/59] blkverify: Drop blkverify_aiocb_info.cancel Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 16/59] curl: Drop curl_aiocb_info.cancel Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 17/59] qed: Drop qed_aiocb_info.cancel Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 18/59] quorum: fix quorum_aio_cancel() Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 19/59] quorum: Convert quorum_aiocb_info.cancel to .cancel_async Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 20/59] rbd: Drop rbd_aiocb_info.cancel Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 21/59] sheepdog: Convert sd_aiocb_info.cancel to .cancel_async Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 22/59] win32-aio: Drop win32_aiocb_info.cancel Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 23/59] ide: Convert trim_aiocb_info.cancel to .cancel_async Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 24/59] block: Drop AIOCBInfo.cancel Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 25/59] block: Rename qemu_aio_release -> qemu_aio_unref Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 26/59] qdev-monitor: fix segmentation fault on qdev_device_help() Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 27/59] aio-win32: avoid out-of-bounds access to the events array Stefan Hajnoczi
2014-09-22 11:41 ` [Qemu-devel] [PULL v2 28/59] block: Introduce "null" drivers Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 29/59] qapi: Sort BlockdevDriver enum data list Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 30/59] qapi: Sort items in BlockdevOptions definition Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 31/59] qapi/block: Add "fatal" to BLOCK_IMAGE_CORRUPTED Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 32/59] qcow2: Add qcow2_signal_corruption() Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 33/59] qcow2: Use qcow2_signal_corruption() for overlaps Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 34/59] qcow2: Check L1/L2/reftable entries for alignment Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 35/59] iotests: Add more tests for qcow2 corruption Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 36/59] image-fuzzer: Trivial readability and formatting improvements Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 37/59] hmp: fix memory leak at hmp_info_block_jobs() Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 38/59] qcow2: Fix leak of QemuOpts in qcow2_open() Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 39/59] qapi: Allow enums in anonymous unions Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 40/59] qcow2: Add overlap-check.template option Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 41/59] qapi/block-core: Add "new" qcow2 options Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 42/59] docs: List all image elements currently supported by the fuzzer Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 43/59] fuzz: Add fuzzing functions for entries of refcount table and blocks Stefan Hajnoczi
2014-09-22 11:42 ` Stefan Hajnoczi [this message]
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 45/59] ahci: Adding basic functionality qtest Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 46/59] ahci: MSI capability should be at 0x80, not 0x50 Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 47/59] ahci: Add test_pci_spec to ahci-test Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 48/59] ahci: add test_pci_enable " Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 49/59] ahci: properly shadow the TFD register Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 50/59] ahci: Add test_hba_spec to ahci-test Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 51/59] ahci: Add test_hba_enable " Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 52/59] ahci: Add test_identify case " Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 53/59] block/archipelago: Fix typo in qemu_archipelago_truncate() Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 54/59] block: delete cow block driver Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 55/59] block: vhdx - fix reading beyond pointer during image creation Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 56/59] async: aio_context_new(): Handle event_notifier_init failure Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 57/59] virtio: Import virtio_vring.h Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 58/59] vring: Better error handling if num is too large Stefan Hajnoczi
2014-09-22 11:42 ` [Qemu-devel] [PULL v2 59/59] block: Always compile virtio-blk dataplane Stefan Hajnoczi
2014-09-23 12:23 ` [Qemu-devel] [PULL v2 00/59] Block patches Peter Maydell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1411386150-24003-45-git-send-email-stefanha@redhat.com \
    --to=stefanha@redhat.com \
    --cc=maria.k@catit.be \
    --cc=maxa@catit.be \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).