Skip to contentSkip to conversation

qcow2: add update refcount table realization for update_refcount

PATCH Superseded Jun Li 1 participant 2 messages Aug 31, 2014 – Sep 1, 2014 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jun Li

author
#0 lore raw

PATCH 1/1 qcow2: add update refcount table realization for update_refcount

When every item of refcount block is NULL, free refcount block and reset the corresponding item of refcount table with NULL.

---
 block/qcow2-refcount.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
block/qcow2-refcount.c +31 -0
index 43665b8..b53a000 100644
@@ -586,6 +586,37 @@ static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
         if (refcount == 0 && s->discard_passthrough[type]) {
             update_refcount_discard(bs, cluster_offset, s->cluster_size);
         }
+
+        /* When refcount block is NULL, update refcount table */
+        if (block_index == 0) {
+            int k = block_index;
+            int refcount_block_entries = s->cluster_size / sizeof(uint16_t);
+            for (k = 1; k < refcount_block_entries; k++) {
+                if (refcount_block[++k] != cpu_to_be16(refcount)) {
+                    break;
+                }
+            }
+
+            if (k == refcount_block_entries) {
+                qemu_vfree(refcount_block);
+                /* update refcount table */
+                unsigned int refcount_table_index;
+                uint64_t data64 = cpu_to_be64(0);
+                refcount_table_index = cluster_index >> (s->cluster_bits -
+                                       REFCOUNT_SHIFT);
+                ret = bdrv_pwrite_sync(bs->file,
+                                       s->refcount_table_offset +
+                                       refcount_table_index *
+                                       sizeof(uint64_t),
+                                       &data64, sizeof(data64));
+                if (ret < 0) {
+                    goto fail;
+                }
+
+                s->refcount_table[refcount_table_index] = data64;
+
+            }
+        }
     }
 
     ret = 0;
Signature · 1 line
1.9.3
author
#1 lore raw

Please ignore it. You could review v2 of this patch directly. Thx.

Quoted 53 lines · Jun Li

When every item of refcount block is NULL, free refcount block and reset the corresponding item of refcount table with NULL.

---
 block/qcow2-refcount.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
block/qcow2-refcount.c +31 -0
index 43665b8..b53a000 100644
@@ -586,6 +586,37 @@ static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,
         if (refcount == 0 && s->discard_passthrough[type]) {
             update_refcount_discard(bs, cluster_offset, s->cluster_size);
         }
+
+        /* When refcount block is NULL, update refcount table */
+        if (block_index == 0) {
+            int k = block_index;
+            int refcount_block_entries = s->cluster_size / sizeof(uint16_t);
+            for (k = 1; k < refcount_block_entries; k++) {
+                if (refcount_block[++k] != cpu_to_be16(refcount)) {
+                    break;
+                }
+            }
+
+            if (k == refcount_block_entries) {
+                qemu_vfree(refcount_block);
+                /* update refcount table */
+                unsigned int refcount_table_index;
+                uint64_t data64 = cpu_to_be64(0);
+                refcount_table_index = cluster_index >> (s->cluster_bits -
+                                       REFCOUNT_SHIFT);
+                ret = bdrv_pwrite_sync(bs->file,
+                                       s->refcount_table_offset +
+                                       refcount_table_index *
+                                       sizeof(uint64_t),
+                                       &data64, sizeof(data64));
+                if (ret < 0) {
+                    goto fail;
+                }
+
+                s->refcount_table[refcount_table_index] = data64;
+
+            }
+        }
     }
 
     ret = 0;
Signature · 1 line
1.9.3