> 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.
>> +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:
> ...
Yea, thank you for your suggestion. I think a hack to qcow2_get_allocated_file_size
will work well.
> (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.