Skip to contentSkip to conversation

Split qcow2 driver

PATCH In review Kevin Wolf 3 participants 9 messages May 28, 2009

Subsystems (2)
lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

cover Split qcow2 driver

The current qcow2 code is a monster of 3000 lines of code. This is hardly manageable and doesn't exactly improve the driver's structure.

This patch series tries to split the driver in smaller modules. It doesn't contain changes to functionality or structure, especially the latter might come later as this series makes it much clearer what the internal interfaces used by the qcow2 driver actually are.

The first three patches mainly move code around. They also build up a qcow2.h header file which contains the common structs and functions used by several modules. Some functions need to become global to keep things compilable.

The fourth patch cleans up the global namespace by adding a qcow2_ prefix to all of the new global functions introduced by the first patches.

Kevin Wolf (4):
qcow2: Split out refcount handling
qcow2: Split out guest cluster functions
qcow2: Split out snapshot functions
qcow2: Rename global functions

 Makefile               |    4 +-
 block/qcow2-cluster.c  |  755 ++++++++++++++++++
 block/qcow2-refcount.c |  836 +++++++++++++++++++
 block/qcow2-snapshot.c |  405 ++++++++++
 block/qcow2.c          | 2081 +-----------------------------------------------
 block/qcow2.h          |  203 +++++
 6 files changed, 2233 insertions(+), 2051 deletions(-)
 create mode 100644 block/qcow2-cluster.c
 create mode 100644 block/qcow2-refcount.c
 create mode 100644 block/qcow2-snapshot.c
 create mode 100644 block/qcow2.h
author
#1 lore raw

PATCH 1/4 qcow2: Split out refcount handling

qcow2-refcount.c contains all functions which are related to cluster allocation and management in the image file. A large part of this is the reference counting of these clusters.

Also a header file qcow2.h is introduced which will contain the interface of the split qcow2 modules.

---
 Makefile               |    3 +-
 block/qcow2-refcount.c |  808 ++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.c          |  871 +-----------------------------------------------
 block/qcow2.h          |  154 +++++++++
 4 files changed, 966 insertions(+), 870 deletions(-)
 create mode 100644 block/qcow2-refcount.c
 create mode 100644 block/qcow2.h
Makefile +2 -1
index d7b9985..5a0fdbe 100644
@@ -68,7 +68,8 @@ recurse-all: $(SUBDIR_RULES)
 BLOCK_OBJS=cutils.o cache-utils.o qemu-malloc.o qemu-option.o module.o
 BLOCK_OBJS+=block/cow.o block/qcow.o aes.o block/vmdk.o block/cloop.o
 BLOCK_OBJS+=block/dmg.o block/bochs.o block/vpc.o block/vvfat.o
-BLOCK_OBJS+=block/qcow2.o block/parallels.o block/nbd.o
+BLOCK_OBJS+=block/qcow2.o block/qcow2-refcount.o
+BLOCK_OBJS+=block/parallels.o block/nbd.o
 BLOCK_OBJS+=nbd.o block.o aio.o
 
 ifdef CONFIG_WIN32
block/qcow2-refcount.c new file +808 -0

808 lines not on this page · raw message

block/qcow2.c +2 -869

925 lines not on this page · raw message

block/qcow2.h new file +154 -0

154 lines not on this page · raw message

Signature · 1 line
1.6.0.6
author
#2 lore raw

PATCH 2/4 qcow2: Split out guest cluster functions

In reply to Kevin Wolf

qcow2-cluster.c contains all functions related to the management of guest clusters, i.e. what the guest sees on its virtual disk. This code is about mapping these guest clusters to host clusters in the image file using the two-level lookup tables.

---
 Makefile               |    2 +-
 block/qcow2-cluster.c  |  754 +++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2-refcount.c |   28 ++
 block/qcow2.c          |  767 +-----------------------------------------------
 block/qcow2.h          |   37 +++-
 5 files changed, 820 insertions(+), 768 deletions(-)
 create mode 100644 block/qcow2-cluster.c
Makefile +1 -1
index 5a0fdbe..7bc37ce 100644
@@ -68,7 +68,7 @@ recurse-all: $(SUBDIR_RULES)
 BLOCK_OBJS=cutils.o cache-utils.o qemu-malloc.o qemu-option.o module.o
 BLOCK_OBJS+=block/cow.o block/qcow.o aes.o block/vmdk.o block/cloop.o
 BLOCK_OBJS+=block/dmg.o block/bochs.o block/vpc.o block/vvfat.o
-BLOCK_OBJS+=block/qcow2.o block/qcow2-refcount.o
+BLOCK_OBJS+=block/qcow2.o block/qcow2-refcount.o block/qcow2-cluster.o
 BLOCK_OBJS+=block/parallels.o block/nbd.o
 BLOCK_OBJS+=nbd.o block.o aio.o
 
block/qcow2-cluster.c new file +754 -0

754 lines not on this page · raw message

block/qcow2-refcount.c +28 -0
index b70afe9..3f461c6 100644
@@ -389,6 +389,34 @@ void free_clusters(BlockDriverState *bs,
     update_refcount(bs, offset, size, -1);
 }
 
+/*
+ * free_any_clusters
+ *
+ * free clusters according to its type: compressed or not
+ *
+ */
+
+void free_any_clusters(BlockDriverState *bs,
+    uint64_t cluster_offset, int nb_clusters)
+{
+    BDRVQcowState *s = bs->opaque;
+
+    /* free the cluster */
+
+    if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
+        int nb_csectors;
+        nb_csectors = ((cluster_offset >> s->csize_shift) &
+                       s->csize_mask) + 1;
+        free_clusters(bs, (cluster_offset & s->cluster_offset_mask) & ~511,
+                      nb_csectors * 512);
+        return;
+    }
+
+    free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
+
+    return;
+}
+
 
 
 /*********************************************************/
block/qcow2.c +2 -765

798 lines not on this page · raw message

block/qcow2.h +35 -2
index 3a34212..367d9bd 100644
@@ -124,7 +124,16 @@ typedef struct QCowCreateState {
     int64_t refcount_block_offset;
 } QCowCreateState;
 
-static int size_to_clusters(BDRVQcowState *s, int64_t size)
+/* XXX This could be private for qcow2-cluster.c */
+typedef struct QCowL2Meta
+{
+    uint64_t offset;
+    int n_start;
+    int nb_available;
+    int nb_clusters;
+} QCowL2Meta;
+
+static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
 {
     return (size + (s->cluster_size - 1)) >> s->cluster_bits;
 }
@@ -133,6 +142,8 @@ static int size_to_clusters(BDRVQcowState *s, int64_t size)
 
 /* qcow2.c functions */
 void l2_cache_reset(BlockDriverState *bs);
+int backing_read1(BlockDriverState *bs,
+                  int64_t sector_num, uint8_t *buf, int nb_sectors);
 
 /* qcow2-refcount.c functions */
 int refcount_init(BlockDriverState *bs);
@@ -141,7 +152,9 @@ void refcount_close(BlockDriverState *bs);
 int64_t alloc_clusters(BlockDriverState *bs, int64_t size);
 int64_t alloc_bytes(BlockDriverState *bs, int size);
 void free_clusters(BlockDriverState *bs,
-                          int64_t offset, int64_t size);
+    int64_t offset, int64_t size);
+void free_any_clusters(BlockDriverState *bs,
+    uint64_t cluster_offset, int nb_clusters);
 
 void create_refcount_update(QCowCreateState *s, int64_t offset, int64_t size);
 int update_snapshot_refcount(BlockDriverState *bs,
@@ -151,4 +164,24 @@ int update_snapshot_refcount(BlockDriverState *bs,
 
 int check_refcounts(BlockDriverState *bs);
 
+/* qcow2-cluster.c functions */
+int grow_l1_table(BlockDriverState *bs, int min_size);
+int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset);
+void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
+                     uint8_t *out_buf, const uint8_t *in_buf,
+                     int nb_sectors, int enc,
+                     const AES_KEY *key);
+
+uint64_t get_cluster_offset(BlockDriverState *bs, uint64_t offset, int *num);
+uint64_t alloc_cluster_offset(BlockDriverState *bs,
+                              uint64_t offset,
+                              int n_start, int n_end,
+                              int *num, QCowL2Meta *m);
+uint64_t alloc_compressed_cluster_offset(BlockDriverState *bs,
+                                         uint64_t offset,
+                                         int compressed_size);
+
+int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
+    QCowL2Meta *m);
+
 #endif
Signature · 1 line
1.6.0.6
author
#3 lore raw

PATCH 3/4 qcow2: Split out snapshot functions

In reply to Kevin Wolf

qcow2-snapshot.c contains the code related to snapshotting.

---
 Makefile               |    1 +
 block/qcow2-snapshot.c |  405 ++++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.c          |  392 ----------------------------------------------
 block/qcow2.h          |   16 ++
 4 files changed, 422 insertions(+), 392 deletions(-)
 create mode 100644 block/qcow2-snapshot.c
Makefile +1 -0
index 7bc37ce..32e2f68 100644
@@ -69,6 +69,7 @@ BLOCK_OBJS=cutils.o cache-utils.o qemu-malloc.o qemu-option.o module.o
 BLOCK_OBJS+=block/cow.o block/qcow.o aes.o block/vmdk.o block/cloop.o
 BLOCK_OBJS+=block/dmg.o block/bochs.o block/vpc.o block/vvfat.o
 BLOCK_OBJS+=block/qcow2.o block/qcow2-refcount.o block/qcow2-cluster.o
+BLOCK_OBJS+=block/qcow2-snapshot.o
 BLOCK_OBJS+=block/parallels.o block/nbd.o
 BLOCK_OBJS+=nbd.o block.o aio.o
 
block/qcow2-snapshot.c new file +405 -0

405 lines not on this page · raw message

block/qcow2.c +0 -392

410 lines not on this page · raw message

block/qcow2.h +16 -0
index 367d9bd..397f21d 100644
@@ -138,6 +138,13 @@ static inline int size_to_clusters(BDRVQcowState *s, int64_t size)
     return (size + (s->cluster_size - 1)) >> s->cluster_bits;
 }
 
+static inline int64_t align_offset(int64_t offset, int n)
+{
+    offset = (offset + n - 1) & ~(n - 1);
+    return offset;
+}
+
+
 // FIXME Need qcow2_ prefix to global functions
 
 /* qcow2.c functions */
@@ -184,4 +191,13 @@ uint64_t alloc_compressed_cluster_offset(BlockDriverState *bs,
 int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
     QCowL2Meta *m);
 
+/* qcow2-snapshot.c functions */
+int qcow_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
+int qcow_snapshot_goto(BlockDriverState *bs, const char *snapshot_id);
+int qcow_snapshot_delete(BlockDriverState *bs, const char *snapshot_id);
+int qcow_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab);
+
+void qcow_free_snapshots(BlockDriverState *bs);
+int qcow_read_snapshots(BlockDriverState *bs);
+
 #endif
Signature · 1 line
1.6.0.6
author
#4 lore raw

PATCH 4/4 qcow2: Rename global functions

In reply to Kevin Wolf

The qcow2 source is now split into several more manageable files. During the conversion quite some functions that were static before needed to be changed to be global to make the source compile again.

We were lucky enough not to get name conflicts with these additional global names, but they are not nice. This patch adds a qcow2_ prefix to all of the global functions in qcow2.

---
 block/qcow2-cluster.c  |   63 ++++++++++++++++++++++++-----------------------
 block/qcow2-refcount.c |   40 +++++++++++++++---------------
 block/qcow2-snapshot.c |   34 +++++++++++++-------------
 block/qcow2.c          |   55 ++++++++++++++++++++++-------------------
 block/qcow2.h          |   54 ++++++++++++++++++++--------------------
 5 files changed, 125 insertions(+), 121 deletions(-)
block/qcow2-cluster.c +32 -31
index 494cc91..99215fa 100644
@@ -28,7 +28,7 @@
 #include "block_int.h"
 #include "block/qcow2.h"
 
-int grow_l1_table(BlockDriverState *bs, int min_size)
+int qcow2_grow_l1_table(BlockDriverState *bs, int min_size)
 {
     BDRVQcowState *s = bs->opaque;
     int new_l1_size, new_l1_size2, ret, i;
@@ -51,7 +51,7 @@ int grow_l1_table(BlockDriverState *bs, int min_size)
     memcpy(new_l1_table, s->l1_table, s->l1_size * sizeof(uint64_t));
 
     /* write new table (align to cluster) */
-    new_l1_table_offset = alloc_clusters(bs, new_l1_size2);
+    new_l1_table_offset = qcow2_alloc_clusters(bs, new_l1_size2);
 
     for(i = 0; i < s->l1_size; i++)
         new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
@@ -68,7 +68,7 @@ int grow_l1_table(BlockDriverState *bs, int min_size)
                 sizeof(data)) != sizeof(data))
         goto fail;
     qemu_free(s->l1_table);
-    free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
+    qcow2_free_clusters(bs, s->l1_table_offset, s->l1_size * sizeof(uint64_t));
     s->l1_table_offset = new_l1_table_offset;
     s->l1_table = new_l1_table;
     s->l1_size = new_l1_size;
@@ -78,7 +78,7 @@ int grow_l1_table(BlockDriverState *bs, int min_size)
     return -EIO;
 }
 
-void l2_cache_reset(BlockDriverState *bs)
+void qcow2_l2_cache_reset(BlockDriverState *bs)
 {
     BDRVQcowState *s = bs->opaque;
 
@@ -191,7 +191,7 @@ static uint64_t *l2_allocate(BlockDriverState *bs, int l1_index)
 
     /* allocate a new l2 entry */
 
-    l2_offset = alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
+    l2_offset = qcow2_alloc_clusters(bs, s->l2_size * sizeof(uint64_t));
 
     /* update the L1 entry */
 
@@ -260,10 +260,10 @@ static int count_contiguous_free_clusters(uint64_t nb_clusters, uint64_t *l2_tab
 /* The crypt function is compatible with the linux cryptoloop
    algorithm for < 4 GB images. NOTE: out_buf == in_buf is
    supported */
-void encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
-                     uint8_t *out_buf, const uint8_t *in_buf,
-                     int nb_sectors, int enc,
-                     const AES_KEY *key)
+void qcow2_encrypt_sectors(BDRVQcowState *s, int64_t sector_num,
+                           uint8_t *out_buf, const uint8_t *in_buf,
+                           int nb_sectors, int enc,
+                           const AES_KEY *key)
 {
     union {
         uint64_t ll[2];
@@ -292,12 +292,12 @@ static int qcow_read(BlockDriverState *bs, int64_t sector_num,
 
     while (nb_sectors > 0) {
         n = nb_sectors;
-        cluster_offset = get_cluster_offset(bs, sector_num << 9, &n);
+        cluster_offset = qcow2_get_cluster_offset(bs, sector_num << 9, &n);
         index_in_cluster = sector_num & (s->cluster_sectors - 1);
         if (!cluster_offset) {
             if (bs->backing_hd) {
                 /* read from the base image */
-                n1 = backing_read1(bs->backing_hd, sector_num, buf, n);
+                n1 = qcow2_backing_read1(bs->backing_hd, sector_num, buf, n);
                 if (n1 > 0) {
                     ret = bdrv_read(bs->backing_hd, sector_num, buf, n1);
                     if (ret < 0)
@@ -307,7 +307,7 @@ static int qcow_read(BlockDriverState *bs, int64_t sector_num,
                 memset(buf, 0, 512 * n);
             }
         } else if (cluster_offset & QCOW_OFLAG_COMPRESSED) {
-            if (decompress_cluster(s, cluster_offset) < 0)
+            if (qcow2_decompress_cluster(s, cluster_offset) < 0)
                 return -1;
             memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
         } else {
@@ -315,7 +315,7 @@ static int qcow_read(BlockDriverState *bs, int64_t sector_num,
             if (ret != n * 512)
                 return -1;
             if (s->crypt_method) {
-                encrypt_sectors(s, sector_num, buf, buf, n, 0,
+                qcow2_encrypt_sectors(s, sector_num, buf, buf, n, 0,
                                 &s->aes_decrypt_key);
             }
         }
@@ -339,7 +339,7 @@ static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
     if (ret < 0)
         return ret;
     if (s->crypt_method) {
-        encrypt_sectors(s, start_sect + n_start,
+        qcow2_encrypt_sectors(s, start_sect + n_start,
                         s->cluster_data,
                         s->cluster_data, n, 1,
                         &s->aes_encrypt_key);
@@ -368,7 +368,8 @@ static int copy_sectors(BlockDriverState *bs, uint64_t start_sect,
  *
  */
 
-uint64_t get_cluster_offset(BlockDriverState *bs, uint64_t offset, int *num)
+uint64_t qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
+    int *num)
 {
     BDRVQcowState *s = bs->opaque;
     int l1_index, l2_index;
@@ -466,7 +467,7 @@ static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
 
     l1_index = offset >> (s->l2_bits + s->cluster_bits);
     if (l1_index >= s->l1_size) {
-        ret = grow_l1_table(bs, l1_index + 1);
+        ret = qcow2_grow_l1_table(bs, l1_index + 1);
         if (ret < 0)
             return 0;
     }
@@ -482,7 +483,7 @@ static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
             return 0;
     } else {
         if (l2_offset)
-            free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
+            qcow2_free_clusters(bs, l2_offset, s->l2_size * sizeof(uint64_t));
         l2_table = l2_allocate(bs, l1_index);
         if (l2_table == NULL)
             return 0;
@@ -513,9 +514,9 @@ static int get_cluster_table(BlockDriverState *bs, uint64_t offset,
  *
  */
 
-uint64_t alloc_compressed_cluster_offset(BlockDriverState *bs,
-                                         uint64_t offset,
-                                         int compressed_size)
+uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
+                                               uint64_t offset,
+                                               int compressed_size)
 {
     BDRVQcowState *s = bs->opaque;
     int l2_index, ret;
@@ -531,9 +532,9 @@ uint64_t alloc_compressed_cluster_offset(BlockDriverState *bs,
         return cluster_offset & ~QCOW_OFLAG_COPIED;
 
     if (cluster_offset)
-        free_any_clusters(bs, cluster_offset, 1);
+        qcow2_free_any_clusters(bs, cluster_offset, 1);
 
-    cluster_offset = alloc_bytes(bs, compressed_size);
+    cluster_offset = qcow2_alloc_bytes(bs, compressed_size);
     nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
                   (cluster_offset >> 9);
 
@@ -554,7 +555,7 @@ uint64_t alloc_compressed_cluster_offset(BlockDriverState *bs,
     return cluster_offset;
 }
 
-int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
+int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
     QCowL2Meta *m)
 {
     BDRVQcowState *s = bs->opaque;
@@ -607,8 +608,8 @@ int alloc_cluster_link_l2(BlockDriverState *bs, uint64_t cluster_offset,
         goto err;
 
     for (i = 0; i < j; i++)
-        free_any_clusters(bs, be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED,
-                          1);
+        qcow2_free_any_clusters(bs,
+            be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1);
 
     ret = 0;
 err:
@@ -629,10 +630,10 @@ err:
  *
  */
 
-uint64_t alloc_cluster_offset(BlockDriverState *bs,
-                              uint64_t offset,
-                              int n_start, int n_end,
-                              int *num, QCowL2Meta *m)
+uint64_t qcow2_alloc_cluster_offset(BlockDriverState *bs,
+                                    uint64_t offset,
+                                    int n_start, int n_end,
+                                    int *num, QCowL2Meta *m)
 {
     BDRVQcowState *s = bs->opaque;
     int l2_index, ret;
@@ -688,7 +689,7 @@ uint64_t alloc_cluster_offset(BlockDriverState *bs,
 
     /* allocate a new cluster */
 
-    cluster_offset = alloc_clusters(bs, nb_clusters * s->cluster_size);
+    cluster_offset = qcow2_alloc_clusters(bs, nb_clusters * s->cluster_size);
 
     /* save info needed for meta data update */
     m->offset = offset;
@@ -730,7 +731,7 @@ static int decompress_buffer(uint8_t *out_buf, int out_buf_size,
     return 0;
 }
 
-int decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
+int qcow2_decompress_cluster(BDRVQcowState *s, uint64_t cluster_offset)
 {
     int ret, csize, nb_csectors, sector_offset;
     uint64_t coffset;
block/qcow2-refcount.c +20 -20
index 3f461c6..b38390c 100644
@@ -34,7 +34,7 @@ static int update_refcount(BlockDriverState *bs,
 /*********************************************************/
 /* refcount handling */
 
-int refcount_init(BlockDriverState *bs)
+int qcow2_refcount_init(BlockDriverState *bs)
 {
     BDRVQcowState *s = bs->opaque;
     int ret, refcount_table_size2, i;
@@ -55,7 +55,7 @@ int refcount_init(BlockDriverState *bs)
     return -ENOMEM;
 }
 
-void refcount_close(BlockDriverState *bs)
+void qcow2_refcount_close(BlockDriverState *bs)
 {
     BDRVQcowState *s = bs->opaque;
     qemu_free(s->refcount_block_cache);
@@ -154,10 +154,10 @@ static int grow_refcount_table(BlockDriverState *bs, int min_size)
     s->refcount_table_offset = table_offset;
 
     update_refcount(bs, table_offset, new_table_size2, 1);
-    free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
+    qcow2_free_clusters(bs, old_table_offset, old_table_size * sizeof(uint64_t));
     return 0;
  fail:
-    free_clusters(bs, table_offset, new_table_size2);
+    qcow2_free_clusters(bs, table_offset, new_table_size2);
     qemu_free(new_table);
     return -EIO;
 }
@@ -334,7 +334,7 @@ retry:
     return (s->free_cluster_index - nb_clusters) << s->cluster_bits;
 }
 
-int64_t alloc_clusters(BlockDriverState *bs, int64_t size)
+int64_t qcow2_alloc_clusters(BlockDriverState *bs, int64_t size)
 {
     int64_t offset;
 
@@ -345,7 +345,7 @@ int64_t alloc_clusters(BlockDriverState *bs, int64_t size)
 
 /* only used to allocate compressed sectors. We try to allocate
    contiguous sectors. size must be <= cluster_size */
-int64_t alloc_bytes(BlockDriverState *bs, int size)
+int64_t qcow2_alloc_bytes(BlockDriverState *bs, int size)
 {
     BDRVQcowState *s = bs->opaque;
     int64_t offset, cluster_offset;
@@ -353,7 +353,7 @@ int64_t alloc_bytes(BlockDriverState *bs, int size)
 
     assert(size > 0 && size <= s->cluster_size);
     if (s->free_byte_offset == 0) {
-        s->free_byte_offset = alloc_clusters(bs, s->cluster_size);
+        s->free_byte_offset = qcow2_alloc_clusters(bs, s->cluster_size);
     }
  redo:
     free_in_cluster = s->cluster_size -
@@ -368,7 +368,7 @@ int64_t alloc_bytes(BlockDriverState *bs, int size)
         if ((offset & (s->cluster_size - 1)) != 0)
             update_cluster_refcount(bs, offset >> s->cluster_bits, 1);
     } else {
-        offset = alloc_clusters(bs, s->cluster_size);
+        offset = qcow2_alloc_clusters(bs, s->cluster_size);
         cluster_offset = s->free_byte_offset & ~(s->cluster_size - 1);
         if ((cluster_offset + s->cluster_size) == offset) {
             /* we are lucky: contiguous data */
@@ -383,7 +383,7 @@ int64_t alloc_bytes(BlockDriverState *bs, int size)
     return offset;
 }
 
-void free_clusters(BlockDriverState *bs,
+void qcow2_free_clusters(BlockDriverState *bs,
                           int64_t offset, int64_t size)
 {
     update_refcount(bs, offset, size, -1);
@@ -396,7 +396,7 @@ void free_clusters(BlockDriverState *bs,
  *
  */
 
-void free_any_clusters(BlockDriverState *bs,
+void qcow2_free_any_clusters(BlockDriverState *bs,
     uint64_t cluster_offset, int nb_clusters)
 {
     BDRVQcowState *s = bs->opaque;
@@ -407,12 +407,13 @@ void free_any_clusters(BlockDriverState *bs,
         int nb_csectors;
         nb_csectors = ((cluster_offset >> s->csize_shift) &
                        s->csize_mask) + 1;
-        free_clusters(bs, (cluster_offset & s->cluster_offset_mask) & ~511,
-                      nb_csectors * 512);
+        qcow2_free_clusters(bs,
+            (cluster_offset & s->cluster_offset_mask) & ~511,
+            nb_csectors * 512);
         return;
     }
 
-    free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
+    qcow2_free_clusters(bs, cluster_offset, nb_clusters << s->cluster_bits);
 
     return;
 }
@@ -424,7 +425,8 @@ void free_any_clusters(BlockDriverState *bs,
 
 
 
-void create_refcount_update(QCowCreateState *s, int64_t offset, int64_t size)
+void qcow2_create_refcount_update(QCowCreateState *s, int64_t offset,
+    int64_t size)
 {
     int refcount;
     int64_t start, last, cluster_offset;
@@ -442,17 +444,15 @@ void create_refcount_update(QCowCreateState *s, int64_t offset, int64_t size)
 }
 
 /* update the refcounts of snapshots and the copied flag */
-int update_snapshot_refcount(BlockDriverState *bs,
-                             int64_t l1_table_offset,
-                             int l1_size,
-                             int addend)
+int qcow2_update_snapshot_refcount(BlockDriverState *bs,
+    int64_t l1_table_offset, int l1_size, int addend)
 {
     BDRVQcowState *s = bs->opaque;
     uint64_t *l1_table, *l2_table, l2_offset, offset, l1_size2, l1_allocated;
     int64_t old_offset, old_l2_offset;
     int l2_size, i, j, l1_modified, l2_modified, nb_csectors, refcount;
 
-    l2_cache_reset(bs);
+    qcow2_l2_cache_reset(bs);
 
     l2_table = NULL;
     l1_table = NULL;
@@ -771,7 +771,7 @@ fail:
  * Returns 0 if no errors are found, the number of errors in case the image is
  * detected as corrupted, and -errno when an internal error occured.
  */
-int check_refcounts(BlockDriverState *bs)
+int qcow2_check_refcounts(BlockDriverState *bs)
 {
     BDRVQcowState *s = bs->opaque;
     int64_t size;
block/qcow2-snapshot.c +17 -17
index 2883e50..e1e4d89 100644
@@ -46,7 +46,7 @@ typedef struct __attribute__((packed)) QCowSnapshotHeader {
     /* name follows  */
 } QCowSnapshotHeader;
 
-void qcow_free_snapshots(BlockDriverState *bs)
+void qcow2_free_snapshots(BlockDriverState *bs)
 {
     BDRVQcowState *s = bs->opaque;
     int i;
@@ -60,7 +60,7 @@ void qcow_free_snapshots(BlockDriverState *bs)
     s->nb_snapshots = 0;
 }
 
-int qcow_read_snapshots(BlockDriverState *bs)
+int qcow2_read_snapshots(BlockDriverState *bs)
 {
     BDRVQcowState *s = bs->opaque;
     QCowSnapshotHeader h;
@@ -111,7 +111,7 @@ int qcow_read_snapshots(BlockDriverState *bs)
     s->snapshots_size = offset - s->snapshots_offset;
     return 0;
  fail:
-    qcow_free_snapshots(bs);
+    qcow2_free_snapshots(bs);
     return -1;
 }
 
@@ -137,7 +137,7 @@ static int qcow_write_snapshots(BlockDriverState *bs)
     }
     snapshots_size = offset;
 
-    snapshots_offset = alloc_clusters(bs, snapshots_size);
+    snapshots_offset = qcow2_alloc_clusters(bs, snapshots_size);
     offset = snapshots_offset;
 
     for(i = 0; i < s->nb_snapshots; i++) {
@@ -177,7 +177,7 @@ static int qcow_write_snapshots(BlockDriverState *bs)
         goto fail;
 
     /* free the old snapshot table */
-    free_clusters(bs, s->snapshots_offset, s->snapshots_size);
+    qcow2_free_clusters(bs, s->snapshots_offset, s->snapshots_size);
     s->snapshots_offset = snapshots_offset;
     s->snapshots_size = snapshots_size;
     return 0;
@@ -229,7 +229,7 @@ static int find_snapshot_by_id_or_name(BlockDriverState *bs, const char *name)
 }
 
 /* if no id is provided, a new one is constructed */
-int qcow_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
+int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
 {
     BDRVQcowState *s = bs->opaque;
     QCowSnapshot *snapshots1, sn1, *sn = &sn1;
@@ -258,12 +258,12 @@ int qcow_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
     sn->date_nsec = sn_info->date_nsec;
     sn->vm_clock_nsec = sn_info->vm_clock_nsec;
 
-    ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
+    ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1);
     if (ret < 0)
         goto fail;
 
     /* create the L1 table of the snapshot */
-    sn->l1_table_offset = alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
+    sn->l1_table_offset = qcow2_alloc_clusters(bs, s->l1_size * sizeof(uint64_t));
     sn->l1_size = s->l1_size;
 
     l1_table = qemu_malloc(s->l1_size * sizeof(uint64_t));
@@ -298,7 +298,7 @@ int qcow_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
 }
 
 /* copy the snapshot 'snapshot_name' into the current disk image */
-int qcow_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
+int qcow2_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
 {
     BDRVQcowState *s = bs->opaque;
     QCowSnapshot *sn;
@@ -309,10 +309,10 @@ int qcow_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
         return -ENOENT;
     sn = &s->snapshots[snapshot_index];
 
-    if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, -1) < 0)
+    if (qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, -1) < 0)
         goto fail;
 
-    if (grow_l1_table(bs, sn->l1_size) < 0)
+    if (qcow2_grow_l1_table(bs, sn->l1_size) < 0)
         goto fail;
 
     s->l1_size = sn->l1_size;
@@ -328,7 +328,7 @@ int qcow_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
         be64_to_cpus(&s->l1_table[i]);
     }
 
-    if (update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0)
+    if (qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 1) < 0)
         goto fail;
 
 #ifdef DEBUG_ALLOC
@@ -339,7 +339,7 @@ int qcow_snapshot_goto(BlockDriverState *bs, const char *snapshot_id)
     return -EIO;
 }
 
-int qcow_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
+int qcow2_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
 {
     BDRVQcowState *s = bs->opaque;
     QCowSnapshot *sn;
@@ -350,14 +350,14 @@ int qcow_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
         return -ENOENT;
     sn = &s->snapshots[snapshot_index];
 
-    ret = update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1);
+    ret = qcow2_update_snapshot_refcount(bs, sn->l1_table_offset, sn->l1_size, -1);
     if (ret < 0)
         return ret;
     /* must update the copied flag on the current cluster offsets */
-    ret = update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
+    ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset, s->l1_size, 0);
     if (ret < 0)
         return ret;
-    free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t));
+    qcow2_free_clusters(bs, sn->l1_table_offset, sn->l1_size * sizeof(uint64_t));
 
     qemu_free(sn->id_str);
     qemu_free(sn->name);
@@ -374,7 +374,7 @@ int qcow_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
     return 0;
 }
 
-int qcow_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
+int qcow2_snapshot_list(BlockDriverState *bs, QEMUSnapshotInfo **psn_tab)
 {
     BDRVQcowState *s = bs->opaque;
     QEMUSnapshotInfo *sn_tab, *sn_info;
block/qcow2.c +29 -26

155 lines not on this page · raw message

block/qcow2.h +27 -27

83 lines not on this page · raw message

Signature · 1 line
1.6.0.6
#5 lore raw
In reply to Kevin Wolf
Quoted 12 lines · Kevin Wolf

The current qcow2 code is a monster of 3000 lines of code. This is hardly manageable and doesn't exactly improve the driver's structure.

This patch series tries to split the driver in smaller modules. It doesn't contain changes to functionality or structure, especially the latter might come later as this series makes it much clearer what the internal interfaces used by the qcow2 driver actually are.

The first three patches mainly move code around. They also build up a qcow2.h header file which contains the common structs and functions used by several modules. Some functions need to become global to keep things compilable.

Quoted 9 lines

The fourth patch cleans up the global namespace by adding a qcow2_ prefix to all of the new global functions introduced by the first patches.

Kevin Wolf (4):
qcow2: Split out refcount handling
qcow2: Split out guest cluster functions
qcow2: Split out snapshot functions
qcow2: Rename global functions

Could you introduce a new qcow2 directory? Perhaps add a README too that we can use to start trying to store some information about how the qcow2 driver works to help other people dive into it.

I think markmc had started a document at one point, right?

Quoted 15 lines
 Makefile               |    4 +-
 block/qcow2-cluster.c  |  755 ++++++++++++++++++
 block/qcow2-refcount.c |  836 +++++++++++++++++++
 block/qcow2-snapshot.c |  405 ++++++++++
 block/qcow2.c          | 2081 +-----------------------------------------------
 block/qcow2.h          |  203 +++++
 6 files changed, 2233 insertions(+), 2051 deletions(-)
 create mode 100644 block/qcow2-cluster.c
 create mode 100644 block/qcow2-refcount.c
 create mode 100644 block/qcow2-snapshot.c
 create mode 100644 block/qcow2.h

Regards,

Anthony Liguori

author
#6 lore raw
Quoted 16 lines · Anthony Liguori
Quoted 15 lines · Kevin Wolf

The current qcow2 code is a monster of 3000 lines of code. This is hardly manageable and doesn't exactly improve the driver's structure.

This patch series tries to split the driver in smaller modules. It doesn't contain changes to functionality or structure, especially the latter might come later as this series makes it much clearer what the internal interfaces used by the qcow2 driver actually are.

The first three patches mainly move code around. They also build up a qcow2.h header file which contains the common structs and functions used by several modules. Some functions need to become global to keep things compilable.

The fourth patch cleans up the global namespace by adding a qcow2_ prefix to all of the new global functions introduced by the first patches.

Quoted 8 lines
Quoted 6 lines

Kevin Wolf (4):
qcow2: Split out refcount handling
qcow2: Split out guest cluster functions
qcow2: Split out snapshot functions
qcow2: Rename global functions

Could you introduce a new qcow2 directory?

I certainly could, but don't you think this is a bit too much? I mean, block/ isn't really crowded and if we end up having a separate directory for each file I wouldn't call this a good directory structure either.

Maybe we should wait for a few other opinions and then decide if a new directory is the way to go or not.

Quoted 3 lines

Perhaps add a README too
that we can use to start trying to store some information about how the qcow2 driver works to help other people dive into it.

That would obviously be a different patch, but in general documentation certainly wouldn't hurt. The very first thing to document would be the image format itself, before even starting with the driver.

Kevin

#7 lore raw
Quoted 5 lines · Kevin Wolf
Quoted 2 lines · Anthony Liguori

Could you introduce a new qcow2 directory?

Quoted 7 lines

I certainly could, but don't you think this is a bit too much? I mean, block/ isn't really crowded and if we end up having a separate directory for each file I wouldn't call this a good directory structure either.

Maybe we should wait for a few other opinions and then decide if a new directory is the way to go or not.

Not a blocker since renaming is reasonable enough in git. Just thought it was worth bringing up since we're talking about reorganizing code for better maintainability...

Quoted 9 lines
Quoted 4 lines

Perhaps add a README too
that we can use to start trying to store some information about how the qcow2 driver works to help other people dive into it.

That would obviously be a different patch, but in general documentation certainly wouldn't hurt. The very first thing to document would be the image format itself, before even starting with the driver.

Regards,

Anthony Liguori

Quoted 2 lines

Kevin