Skip to contentSkip to conversation

qemu-img: return allocated size for block device with qcow2 format

PATCH Superseded Ivan Ren 3 participants 12 messages May 2, 2018 – May 4, 2018 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ivan Ren

author
#0 lore raw

PATCH 1/1 qemu-img: return allocated size for block device with qcow2 format

qemu-img info with a block device which has a qcow2 format always return 0 for disk size, and this can not reflect the qcow2 size and the used space of the block device. This patch return the allocated size of qcow2 as the disk size.

---
 block/qcow2-bitmap.c |  69 +++++++++++++++++
 block/qcow2.c        | 212 +++++++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.h        |  42 ++++++++++
 3 files changed, 323 insertions(+)
block/qcow2-bitmap.c +69 -0
index 6e93ec4..2957a7a 100644
@@ -636,6 +636,75 @@ fail:
     return NULL;
 }
 
+/*
+ * Get the highest allocated cluster index of bitmap
+ */
+int qcow2_get_bitmap_allocated_clusters(BlockDriverState *bs,
+                                        int64_t *p_highest_cluster_index,
+                                        int64_t nb_clusters)
+{
+    int ret;
+    BDRVQcow2State *s = bs->opaque;
+    Qcow2BitmapList *bm_list = NULL;
+    Qcow2Bitmap *bm;
+
+    if (s->nb_bitmaps == 0) {
+        return 0;
+    }
+
+    ret = update_highest_cluster_index(s, s->bitmap_directory_offset,
+                                       s->bitmap_directory_size,
+                                       p_highest_cluster_index, nb_clusters);
+    if (ret == 1) {
+        goto out;
+    }
+
+    bm_list = bitmap_list_load(bs, s->bitmap_directory_offset,
+                               s->bitmap_directory_size, NULL);
+    if (bm_list == NULL) {
+        ret = -EINVAL;
+        goto out;
+    }
+
+    QSIMPLEQ_FOREACH(bm, bm_list, entry) {
+        uint64_t *bitmap_table = NULL;
+        int i;
+
+        ret = update_highest_cluster_index(s, bm->table.offset,
+                                        bm->table.size * sizeof(uint64_t),
+                                        p_highest_cluster_index, nb_clusters);
+        if (ret == 1) {
+            goto out;
+        }
+
+        ret = bitmap_table_load(bs, &bm->table, &bitmap_table);
+        if (ret < 0) {
+            goto out;
+        }
+
+        for (i = 0; i < bm->table.size; ++i) {
+            uint64_t entry = bitmap_table[i];
+            uint64_t offset = entry & BME_TABLE_ENTRY_OFFSET_MASK;
+
+            /* ignore entry legality */
+            if (offset == 0) {
+                continue;
+            }
+
+            ret = update_highest_cluster_index(s, offset, s->cluster_size,
+                                         p_highest_cluster_index, nb_clusters);
+            if (ret == 1) {
+                g_free(bitmap_table);
+                goto out;
+            }
+        }
+        g_free(bitmap_table);
+    }
+out:
+    bitmap_list_free(bm_list);
+    return ret;
+}
+
 int qcow2_check_bitmaps_refcounts(BlockDriverState *bs, BdrvCheckResult *res,
                                   void **refcount_table,
                                   int64_t *refcount_table_size)
block/qcow2.c +212 -0

224 lines not on this page · raw message

block/qcow2.h +42 -0

54 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 4 lines · Ivan Ren

qemu-img info with a block device which has a qcow2 format always return 0 for disk size, and this can not reflect the qcow2 size and the used space of the block device. This patch return the allocated size of qcow2 as the disk size.

How does this differ from what qemu can already give you at runtime via the wr_highest_offset property in BlockDeviceStats, and related to the write_threshold ('block-set-write-threshold command, BLOCK_WRITE_THRESHOLD event)? Is there any code we can reuse, rather than writing something from scratch?

Quoted 8 lines
---
  block/qcow2-bitmap.c |  69 +++++++++++++++++
  block/qcow2.c        | 212 +++++++++++++++++++++++++++++++++++++++++++++++++++
  block/qcow2.h        |  42 ++++++++++
  3 files changed, 323 insertions(+)
Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#2 lore raw

Thanks for your review, Eric.
Yes, the wr_highest_offset can tell the end offset at runtime, and write_threshold similar to it. But in my situation, I need to know the allocated end without a vm running.

Quoted 27 lines · Eric Blake <[email protected]> 于2018年5月2日周三 下午10:02写道:
Quoted 4 lines · Ivan Ren

qemu-img info with a block device which has a qcow2 format always return 0 for disk size, and this can not reflect the qcow2 size and the used space of the block device. This patch return the allocated size of qcow2 as the disk size.

How does this differ from what qemu can already give you at runtime via the wr_highest_offset property in BlockDeviceStats, and related to the write_threshold ('block-set-write-threshold command, BLOCK_WRITE_THRESHOLD event)? Is there any code we can reuse, rather than writing something from scratch?

Quoted 5 lines
---
  block/qcow2-bitmap.c |  69 +++++++++++++++++
  block/qcow2.c        | 212

+++++++++++++++++++++++++++++++++++++++++++++++++++

Quoted 3 lines
  block/qcow2.h        |  42 ++++++++++
  3 files changed, 323 insertions(+)

--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org

#3 lore raw
Quoted 5 lines · 叶残风

Thanks for your review, Eric.
Yes, the wr_highest_offset can tell the end offset at runtime, and write_threshold similar to it. But in my situation, I need to know the allocated end without a vm running.

Oh, I've just seen that your patch doesn't even get the allocated size, it actually determines the highest allocated offset. Well, those are two different things. You should not return this value via qcow2_get_allocated_file_size(). I'm not sure where it should go (probably wr_highest_offset?) and how it should be presented in qemu-img, though.

Also note that bdrv_get_allocated_file_size() is called by QMP's query-named-block-nodes and query-block, both of which are online synchronous functions. It should not take a long time, specifically it should not scan the whole qcow2 file.

Max

author
#4 lore raw
Quoted 4 lines

Yeah, well, then you use qemu-img check to repair it. The refcount structure is there to know which clusters are allocated, so we should use it when we want to know that and not assume it's broken. If the user thinks it may be broken, they are free to run qemu-img check to

check.

Yea, it is right. It makes things easy.

Quoted 11 lines

Oh, I've just seen that your patch doesn't even get the allocated size, it actually determines the highest allocated offset. Well, those are two different things. You should not return this value via qcow2_get_allocated_file_size(). I'm not sure where it should go (probably wr_highest_offset?) and how it should be presented in qemu-img, though.

Also note that bdrv_get_allocated_file_size() is called by QMP's query-named-block-nodes and query-block, both of which are online synchronous functions. It should not take a long time, specifically it should not scan the whole qcow2 file.

Yes, My original idea was to get the highest allocated offset on the block device and regard it as currently max allocated size(ignore the hole lower than it).
Through the discuss, I think it is better to count how many clusters with non-0 refcount as the real allocated size.
I'll try to get a new version patch to implement this.

Quoted 22 lines · Max Reitz
Quoted 5 lines · 叶残风

Thanks for your review, Eric.
Yes, the wr_highest_offset can tell the end offset at runtime, and write_threshold similar to it. But in my situation, I need to know the allocated end without a vm running.

Oh, I've just seen that your patch doesn't even get the allocated size, it actually determines the highest allocated offset. Well, those are two different things. You should not return this value via qcow2_get_allocated_file_size(). I'm not sure where it should go (probably wr_highest_offset?) and how it should be presented in qemu-img, though.

Also note that bdrv_get_allocated_file_size() is called by QMP's query-named-block-nodes and query-block, both of which are online synchronous functions. It should not take a long time, specifically it should not scan the whole qcow2 file.

Max

#5 lore raw
In reply to Ivan Ren

Hi,

[replying to this version because the previous mail doesn't seem to have made it to the mailing lists for whatever reason]

Quoted 4 lines · Ivan Ren

qemu-img info with a block device which has a qcow2 format always return 0 for disk size, and this can not reflect the qcow2 size and the used space of the block device. This patch return the allocated size of qcow2 as the disk size.

I'm not quite sure whether you really need this information for block devices (I tend to agree with Eric that wr_highest_cluster is the more important information there), but I can imagine it just being nice to have.

So the basic idea makes sense to me, but I think the implementation can be simplified and the reporting in qemu-img should be done differently.

Quoted 7 lines
---
 block/qcow2-bitmap.c |  69 +++++++++++++++++
 block/qcow2.c        | 212 +++++++++++++++++++++++++++++++++++++++++++++++++++
 block/qcow2.h        |  42 ++++++++++
 3 files changed, 323 insertions(+)

The whole implementation reminds me a lot of qcow2's check function, which basically just recalculates the refcounts. So I'm wondering whether you could just count how many clusters with non-0 refcount there are and thus simplify the implementation dramatically.

[...]

block/qcow2.c static QemuOptsList qcow2_create_opts = { · line 4696

Quoted 6 lines
block/qcow2.c +6 -0
+static int64_t qcow2_get_allocated_file_size(BlockDriverState *bs)
+{
+    struct stat st;
+    if (stat(bs->filename, &st) < 0 || !S_ISBLK(st.st_mode)) {
+        goto get_file_size;
+    }

This definitely doesn't work because nobody guarantees that bs->filename is something that stat() can work with. I'm aware that you need to do the S_ISBLK() check somewhere, but the qcow2 driver is not the right place.

I don't really have a good way around this, though. These things come to mind:

(1) We could let file-posix report an error for S_ISBLK because the information is known to be usually useless -- but I think that is not quite the right thing to do because maybe some block devices do report useful information there, I don't know.

(2) Or we introduce a new field in qemu-img info (and thus in ImageInfo, too, I suppose?). qemu-img info (or rather bdrv_query_image_info()) could detect whether the format layer supports bdrv_get_allocated_file_size, and if so, it emits that information separately from the allocated size of bs->file->bs. But that would break vmdk...

(3) As a hack, qcow2_get_allocated_file_size() could first always call bdrv_get_allocated_file_size(bs->file->bs), and if that returns 0 (which is absolutely impossible for qcow2 files because they have an image header that takes up some space), we fall back to qcow2_get_block_allocated_size(). While I consider it a hack, I can't come up with a scenario where it wouldn't work.

Max

Quoted 10 lines
block/qcow2.c +10 -0
+
+    return qcow2_get_block_allocated_size(bs);
+
+get_file_size:
+    if (bs->file) {
+        return bdrv_get_allocated_file_size(bs->file->bs);
+    }
+    return -ENOTSUP;
+}
+
#6 lore raw
Quoted 9 lines · Max Reitz
Quoted 4 lines · Ivan Ren

qemu-img info with a block device which has a qcow2 format always return 0 for disk size, and this can not reflect the qcow2 size and the used space of the block device. This patch return the allocated size of qcow2 as the disk size.

I'm not quite sure whether you really need this information for block devices (I tend to agree with Eric that wr_highest_cluster is the more important information there), but I can imagine it just being nice to have.

Hmm, so in an extreme case, if you create an internal snapshot, then the guest makes edits, then you remove the internal snapshot, we have a wr_highest_offset that has advanced (because the guest changes had to allocate new clusters due to COW of refcount=2 clusters); but the deleted snapshot now means we have a lot of unused clusters earlier in the image (deleting the snapshot took refcount=2 clusters back to 1, and any COW'd clusters edited after the internal snapshot means the snapshot version is now back to refcount=0, whether or not we also try to punch a hole in the protocol layer for those freed clusters). Thus, reporting the highest written cluster is a larger number than the number of clusters that are actually in use, and both numbers might be useful to know (how big do I have to size my block device, and how utilized is my block device), especially if we add code for online compaction or defragmentation of a qcow2 image so that we can move higher offsets into holes left earlier in the image.

If you don't use internal snapshots, the only way to get holes of unallocated clusters earlier in the image is if the guest uses TRIM operations, and I'm not sure if that's easier or harder to trigger, nor which approach (internal snapshots vs. guest TRIM operations) is likely to leave more holes of unallocated clusters.

Quoted 5 lines

The whole implementation reminds me a lot of qcow2's check function, which basically just recalculates the refcounts. So I'm wondering whether you could just count how many clusters with non-0 refcount there are and thus simplify the implementation dramatically.

We also recently added 'qemu-img measure', which DOES report how many clusters are in use. Is any of that reusable here?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#7 lore raw
Quoted 12 lines · Eric Blake
Quoted 10 lines · Max Reitz
Quoted 4 lines · Ivan Ren

qemu-img info with a block device which has a qcow2 format always return 0 for disk size, and this can not reflect the qcow2 size and the used space of the block device. This patch return the allocated size of qcow2 as the disk size.

I'm not quite sure whether you really need this information for block devices (I tend to agree with Eric that wr_highest_cluster is the more important information there), but I can imagine it just being nice to have.

Quoted 15 lines

Hmm, so in an extreme case, if you create an internal snapshot, then the guest makes edits, then you remove the internal snapshot, we have a wr_highest_offset that has advanced (because the guest changes had to allocate new clusters due to COW of refcount=2 clusters); but the deleted snapshot now means we have a lot of unused clusters earlier in the image (deleting the snapshot took refcount=2 clusters back to 1, and any COW'd clusters edited after the internal snapshot means the snapshot version is now back to refcount=0, whether or not we also try to punch a hole in the protocol layer for those freed clusters).  Thus, reporting the highest written cluster is a larger number than the number of clusters that are actually in use, and both numbers might be useful to know (how big do I have to size my block device, and how utilized is my block device), especially if we add code for online compaction or defragmentation of a qcow2 image so that we can move higher offsets into holes left earlier in the image.

In any case, since a block device is linear you really need to know the highest offset that is in use. Sure, if you know how much space there is wasted in holes in the middle of the file, that is nice to know, but it doesn't tell you when you need to grow your block device.

(It does tell you when to consider qemu-img convert or a mirror job to defragment the image, though...)

Quoted 5 lines

If you don't use internal snapshots, the only way to get holes of unallocated clusters earlier in the image is if the guest uses TRIM operations, and I'm not sure if that's easier or harder to trigger, nor which approach (internal snapshots vs. guest TRIM operations) is likely to leave more holes of unallocated clusters.

And I think we (at least used to) have quirks in qcow2's allocation algorithm that meant it could leave some clusters unallocated in the middle of the image (I think that was in case you allocate more than a single cluster (e.g. for an L1 table), but then you also need to allocate a new refblock, so you allocate that first and then you only start to allocate clusters after that refblock, even though there might still be space in front of it).

Quoted 7 lines
Quoted 4 lines

The whole implementation reminds me a lot of qcow2's check function, which basically just recalculates the refcounts.  So I'm wondering whether you could just count how many clusters with non-0 refcount there are and thus simplify the implementation dramatically.

We also recently added 'qemu-img measure', which DOES report how many clusters are in use.  Is any of that reusable here?

It only tells you that information for a hypothetical new image, though, doesn't it?

Max

#8 lore raw
Quoted 5 lines · Max Reitz
Quoted 2 lines

We also recently added 'qemu-img measure', which DOES report how many clusters are in use.  Is any of that reusable here?

It only tells you that information for a hypothetical new image, though, doesn't it?

It has two uses: with just a size, estimate the overhead needed to create a file with the given format and exposing the given size to the guest (both sparse and fully allocated sizes are estimated); and with a pre-existing image, compute the exact overhead needed for 'qemu-img convert' to create a new image with the given format and the same guest-visible contents. The latter use, where it uses an existing guest image as the starting point to measure, indeed only tells you what a hypothetical new image will occupy (the used cluster count, not the wr_highest_offset count) - but isn't that what you want?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#9 lore raw
Quoted 8 lines · Eric Blake
Quoted 5 lines · Max Reitz
Quoted 2 lines

We also recently added 'qemu-img measure', which DOES report how many clusters are in use.  Is any of that reusable here?

It only tells you that information for a hypothetical new image, though, doesn't it?

Quoted 9 lines

It has two uses: with just a size, estimate the overhead needed to create a file with the given format and exposing the given size to the guest (both sparse and fully allocated sizes are estimated); and with a pre-existing image, compute the exact overhead needed for 'qemu-img convert' to create a new image with the given format and the same guest-visible contents.  The latter use, where it uses an existing guest image as the starting point to measure, indeed only tells you what a hypothetical new image will occupy (the used cluster count, not the wr_highest_offset count) - but isn't that what you want?

As far as I can tell, it only uses allocation status of the active layer to determine how much of the hypothetical new image would be allocated (the man page says "as if converting an existing image file using qemu-img convert"). But this patch tries to determine exactly how much is allocated in the current image for everything, not just how much you need to have allocated to represent the active layer.

That may include snapshots, but it also includes bitmaps, or allocated zero clusters (which qemu-img measure discounts), or things like empty L2 tables. So, everything.

Max

author
#10 lore raw
In reply to Max Reitz
Quoted 4 lines

The whole implementation reminds me a lot of qcow2's check function, which basically just recalculates the refcounts. So I'm wondering whether you could just count how many clusters with non-0 refcount there are and thus simplify the implementation dramatically.

Thanks for your review, Max.

Yes, just get the highest non-0 refcount cluster index can simply get the end offset. But in some situations(such as some errors happen), a cluster is indexed in index table, but the refcount may be 0 error, just like the qcow2 check inconsistency. So I traverse the whole index part, include L1, L2, snapshot and so on.
I try to reuse the qcow2 check code, but the check routine limit the avaliable
end to SIZE_MAX which work well in file situation, however the block device has
a fix end. And the check routine print a lot check info which I don't need.

Quoted 10 lines

block/qcow2.c static QemuOptsList qcow2_create_opts = { · line 4696

Quoted 6 lines
block/qcow2.c +6 -0
+static int64_t qcow2_get_allocated_file_size(BlockDriverState *bs)
+{
+    struct stat st;
+    if (stat(bs->filename, &st) < 0 || !S_ISBLK(st.st_mode)) {
+        goto get_file_size;
+    }

This definitely doesn't work because nobody guarantees that bs->filename is something that stat() can work with. I'm aware that you need to do the S_ISBLK() check somewhere, but the qcow2 driver is not the right

place.

Quoted 4 lines

I don't really have a good way around this, though. These things come to mind:
...

Yea, thank you for your suggestion. I think a hack to qcow2_get_allocated_file_size
will work well.

Quoted 6 lines

(3) As a hack, qcow2_get_allocated_file_size() could first always call bdrv_get_allocated_file_size(bs->file->bs), and if that returns 0 (which is absolutely impossible for qcow2 files because they have an image header that takes up some space), we fall back to qcow2_get_block_allocated_size(). While I consider it a hack, I can't come up with a scenario where it wouldn't work.

Quoted 81 lines · Max Reitz <[email protected]> 于2018年5月2日周三 下午10:37写道:

Hi,

[replying to this version because the previous mail doesn't seem to have made it to the mailing lists for whatever reason]

Quoted 4 lines · Ivan Ren

qemu-img info with a block device which has a qcow2 format always return 0 for disk size, and this can not reflect the qcow2 size and the used space of the block device. This patch return the allocated size of qcow2 as the disk size.

I'm not quite sure whether you really need this information for block devices (I tend to agree with Eric that wr_highest_cluster is the more important information there), but I can imagine it just being nice to have.

So the basic idea makes sense to me, but I think the implementation can be simplified and the reporting in qemu-img should be done differently.

Quoted 5 lines
---
 block/qcow2-bitmap.c |  69 +++++++++++++++++
 block/qcow2.c        | 212

+++++++++++++++++++++++++++++++++++++++++++++++++++

Quoted 2 lines
 block/qcow2.h        |  42 ++++++++++
 3 files changed, 323 insertions(+)

The whole implementation reminds me a lot of qcow2's check function, which basically just recalculates the refcounts. So I'm wondering whether you could just count how many clusters with non-0 refcount there are and thus simplify the implementation dramatically.

[...]

block/qcow2.c static QemuOptsList qcow2_create_opts = { · line 4696

Quoted 6 lines
block/qcow2.c +6 -0
+static int64_t qcow2_get_allocated_file_size(BlockDriverState *bs)
+{
+    struct stat st;
+    if (stat(bs->filename, &st) < 0 || !S_ISBLK(st.st_mode)) {
+        goto get_file_size;
+    }

This definitely doesn't work because nobody guarantees that bs->filename is something that stat() can work with. I'm aware that you need to do the S_ISBLK() check somewhere, but the qcow2 driver is not the right place.

I don't really have a good way around this, though. These things come to mind:

(1) We could let file-posix report an error for S_ISBLK because the information is known to be usually useless -- but I think that is not quite the right thing to do because maybe some block devices do report useful information there, I don't know.

(2) Or we introduce a new field in qemu-img info (and thus in ImageInfo, too, I suppose?). qemu-img info (or rather bdrv_query_image_info()) could detect whether the format layer supports bdrv_get_allocated_file_size, and if so, it emits that information separately from the allocated size of bs->file->bs. But that would break vmdk...

(3) As a hack, qcow2_get_allocated_file_size() could first always call bdrv_get_allocated_file_size(bs->file->bs), and if that returns 0 (which is absolutely impossible for qcow2 files because they have an image header that takes up some space), we fall back to qcow2_get_block_allocated_size(). While I consider it a hack, I can't come up with a scenario where it wouldn't work.

Max

Quoted 10 lines
block/qcow2.c +10 -0
+
+    return qcow2_get_block_allocated_size(bs);
+
+get_file_size:
+    if (bs->file) {
+        return bdrv_get_allocated_file_size(bs->file->bs);
+    }
+    return -ENOTSUP;
+}
+
#11 lore raw
Quoted 5 lines · 叶残风
Quoted 4 lines

The whole implementation reminds me a lot of qcow2's check function, which basically just recalculates the refcounts.  So I'm wondering whether you could just count how many clusters with non-0 refcount there are and thus simplify the implementation dramatically.

Quoted 7 lines

Thanks for your review, Max.

Yes, just get the highest non-0 refcount cluster index can simply get the end offset. But in some situations(such as some errors happen), a cluster is indexed in index table, but the refcount may be 0 error, just like the qcow2 check inconsistency. So I traverse the whole index part, include L1, L2, snapshot and so on.

Yeah, well, then you use qemu-img check to repair it. The refcount structure is there to know which clusters are allocated, so we should use it when we want to know that and not assume it's broken. If the user thinks it may be broken, they are free to run qemu-img check to check.

Note that qemu makes sure that no refcount ever is lower than it must be. Refcounts are always updated in an order such that if qemu crashes during some operations the refcount is equal to or higher than what it needs to be. Anything else is a bug in qemu (or the result of lazy-refcounts, but then the image is marked dirty and automatically repaired when opened R/W the next time).

Max

Quoted 121 lines

I try to reuse the qcow2 check code, but the check routine limit the avaliable
end to SIZE_MAX which work well in file situation, however the block device has
a fix end. And the check routine print a lot check info which I don't need.

Quoted 10 lines

block/qcow2.c static QemuOptsList qcow2_create_opts = { · line 4696

Quoted 6 lines
block/qcow2.c +6 -0
+static int64_t qcow2_get_allocated_file_size(BlockDriverState *bs)
+{
+    struct stat st;
+    if (stat(bs->filename, &st) < 0 || !S_ISBLK(st.st_mode)) {
+        goto get_file_size;
+    }

This definitely doesn't work because nobody guarantees that bs->filename is something that stat() can work with.  I'm aware that you need to do the S_ISBLK() check somewhere, but the qcow2 driver is not the right

place.

Quoted 4 lines

I don't really have a good way around this, though.  These things come to mind:
...

Yea, thank you for your suggestion. I think a hack to qcow2_get_allocated_file_size
will work well.

Quoted 6 lines

(3) As a hack, qcow2_get_allocated_file_size() could first always call bdrv_get_allocated_file_size(bs->file->bs), and if that returns 0 (which is absolutely impossible for qcow2 files because they have an image header that takes up some space), we fall back to qcow2_get_block_allocated_size().  While I consider it a hack, I can't come up with a scenario where it wouldn't work.

Max Reitz <[email protected] <mailto:[email protected]>> 于2018年5月2日 周三 下午10:37写道:

Hi,

[replying to this version because the previous mail doesn't seem to have made it to the mailing lists for whatever reason]

On 2018-05-02 15:34, Ivan Ren wrote:
> qemu-img info with a block device which has a qcow2 format always > return 0 for disk size, and this can not reflect the qcow2 size > and the used space of the block device. This patch return the > allocated size of qcow2 as the disk size.

I'm not quite sure whether you really need this information for block devices (I tend to agree with Eric that wr_highest_cluster is the more important information there), but I can imagine it just being nice to have.

So the basic idea makes sense to me, but I think the implementation can be simplified and the reporting in qemu-img should be done differently.

>
> Signed-off-by: Ivan Ren <[email protected] <mailto:[email protected]>>
> ---
>  block/qcow2-bitmap.c |  69 +++++++++++++++++ >  block/qcow2.c        | 212
+++++++++++++++++++++++++++++++++++++++++++++++++++
>  block/qcow2.h        |  42 ++++++++++ >  3 files changed, 323 insertions(+)

The whole implementation reminds me a lot of qcow2's check function, which basically just recalculates the refcounts.  So I'm wondering whether you could just count how many clusters with non-0 refcount there are and thus simplify the implementation dramatically.

[...]

> +static int64_t qcow2_get_allocated_file_size(BlockDriverState *bs) > +{
> +    struct stat st;
> +    if (stat(bs->filename, &st) < 0 || !S_ISBLK(st.st_mode)) { > +        goto get_file_size;
> +    }

This definitely doesn't work because nobody guarantees that bs->filename is something that stat() can work with.  I'm aware that you need to do the S_ISBLK() check somewhere, but the qcow2 driver is not the right place.

I don't really have a good way around this, though.  These things come to mind:

(1) We could let file-posix report an error for S_ISBLK because the information is known to be usually useless -- but I think that is not quite the right thing to do because maybe some block devices do report useful information there, I don't know.

(2) Or we introduce a new field in qemu-img info (and thus in ImageInfo, too, I suppose?).  qemu-img info (or rather bdrv_query_image_info()) could detect whether the format layer supports bdrv_get_allocated_file_size, and if so, it emits that information separately from the allocated size of bs->file->bs.  But that would break vmdk...

(3) As a hack, qcow2_get_allocated_file_size() could first always call bdrv_get_allocated_file_size(bs->file->bs), and if that returns 0 (which is absolutely impossible for qcow2 files because they have an image header that takes up some space), we fall back to qcow2_get_block_allocated_size().  While I consider it a hack, I can't come up with a scenario where it wouldn't work.

Max

> +
> +    return qcow2_get_block_allocated_size(bs);
> +
> +get_file_size:
> +    if (bs->file) {
> +        return bdrv_get_allocated_file_size(bs->file->bs); > +    }
> +    return -ENOTSUP;
> +}
> +