Note: This bug is displayed in read-only format because the product is no longer active in Red Hat Bugzilla.
RHEL Engineering is moving the tracking of its product development work on RHEL 6 through RHEL 9 to Red Hat Jira (issues.redhat.com). If you're a Red Hat customer, please continue to file support cases via the Red Hat customer portal. If you're not, please head to the "RHEL project" in Red Hat Jira and file new tickets here. Individual Bugzilla bugs in the statuses "NEW", "ASSIGNED", and "POST" are being migrated throughout September 2023. Bugs of Red Hat partners with an assigned Engineering Partner Manager (EPM) are migrated in late September as per pre-agreed dates. Bugs against components "kernel", "kernel-rt", and "kpatch" are only migrated if still in "NEW" or "ASSIGNED". If you cannot log in to RH Jira, please consult article #7032570. That failing, please send an e-mail to the RH Jira admins at rh-issues@redhat.com to troubleshoot your issue as a user management inquiry. The email creates a ServiceNow ticket with Red Hat. Individual Bugzilla bugs that are migrated will be moved to status "CLOSED", resolution "MIGRATED", and set with "MigratedToJIRA" in "Keywords". The link to the successor Jira issue will be found under "Links", have a little "two-footprint" icon next to it, and direct you to the "RHEL project" in Red Hat Jira (issue links are of type "https://issues.redhat.com/browse/RHEL-XXXX", where "X" is a digit). This same link will be available in a blue banner at the top of the page informing you that that bug has been migrated.

Bug 1975688

Summary: qemu-nbd block status reports allocated zero area in qcow2 image as a hole
Product: Red Hat Enterprise Linux 9 Reporter: zixchen
Component: qemu-kvmAssignee: Eric Blake <eblake>
qemu-kvm sub component: NBD QA Contact: Tingting Mao <timao>
Status: CLOSED CURRENTRELEASE Docs Contact:
Severity: high    
Priority: high CC: coli, eblake, jinzhao, juzhang, kkiwi, nsoffer, virt-maint, xuwei
Version: 9.0Keywords: Triaged
Target Milestone: betaFlags: pm-rhel: mirror+
Target Release: ---   
Hardware: x86_64   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: 1968693 Environment:
Last Closed: 2021-10-15 06:39:03 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On: 1968693    
Bug Blocks:    

Description zixchen 2021-06-24 08:16:08 UTC
+++ This bug was initially created as a clone of Bug #1968693 +++

Description of problem:

In qemu 6.0.0, qemu-nbd fixed hole reporting for raw images:
https://github.com/qemu/qemu/commit/0da9856851dcca09222a1467e16ddd05dc66e460

But this change broke reporting of zeroed area in qcow2 image with a backup file.
Previously this was reported as NBD_STATE_ZERO, but not it is reported as
NBD_STATE_ZERO | NBD_STATE_HOLE.

Here is example reproduction:

# Create an image with a backing file:
$ qemu-img create base.raw $((4*64*1024))
$ qemu-img create -f qcow2 -b base.raw -F raw top.qcow2

# Write data to the first 3 cluster in the base image:
$ qemu-io -f raw -c "write -P 65 0 64k" base.raw
$ qemu-io -f raw -c "write -P 66 64k 64k" base.raw
$ qemu-io -f raw -c "write -P 67 128k 64k" base.raw

# Write data to the second cluster of the top image, hiding the second cluster in the backing file:
$ qemu-io -f qcow2 -c "write -P 69 64k 64k" top.qcow2

# Write zeroes to the third cluster of the top imge, hiding the third cluster in the backing file:
$ qemu-io -f qcow2 -c "write -z 128k 64k" top.qcow2


This creates (A-Z: data, 0: zero, -: hole):

top:  -D0-
base: ABC-


# Query the image via qemu-nbd

$ qemu-nbd -r -t -f qcow2 top.qcow2 &

$ qemu-img map --output json nbd://localhost
[{ "start": 0, "length": 131072, "depth": 0, "zero": false, "data": true, "offset": 0},
{ "start": 131072, "length": 131072, "depth": 0, "zero": true, "data": false, "offset": 131072}]

$ nbdinfo --map nbd://localhost
         0      131072    0  allocated
    131072      131072    3  hole,zero

Both qemu-img map and qemu-nbd report the third cluster as a hole. However qemu-nbd include
the depth parameter, so we can tell that the third cluster is allocated in the top image.
qemu-nbd does not report the depth of each extent, so there is no way to tell that the 
third cluster must be zeroed in the top image.

This does not effect guest using NBD to access the image, but if the caller wants to copy
both images to other storage via qemu-nbd, keeping the all the layers, there no way to tell
the correct contents of the top image.

RHV supports copying snapshots form one system to another via qemu-nbd, using this pipeline:

    src.img -> qemu-nbd -> imageio server -> imageio-client -> qemu-nbd -> dst.img

This can also be used by 3rd party code:

    src.img -> qemu-nbd -> imageio server > 3rd party client

When RHV is configured to allow copying single image from an image chain (disk snapshots
in RHV tersm), it uses qemu-nbd on the source side like this:

    qemu-nbd .. 'json:{"driver": "qcow2", "backing": null, "file": {"driver": "file", "filename": "top.qcow2"}}'

imageio client request image extents from imageio server, using NBD block status with
qemu-nbd to return the extents:

    [
        { "start": 0, "length": 131072, "zero": false, "hole": false},
        { "start": 131072, "length": 131072, "zero": true, "hole": true}
    ]

This is part of imageio API:
http://ovirt.github.io/ovirt-imageio/images.html#extents

Based on this, imageio client copies the first extent to the destination image,
and skip the second extents which is a hole.

After copying both images, the result will be an image with a hole in the third cluster,
exposing the third cluster form the base image. This will corrupt the image.

RHV supports this flow for 3 use cases:
1. Snapshot based backup - this will create corrupted backup silently
2. Restore backups keeping all snapshots - this will create corrupted image, which
   will fail once the guest is started.

RHV user may use imageio client tool, imageio python library or the HTTP API
directly.

RHV introduced this feature in 4.4.4, tested with qemu-nbd 4.2.

Version-Release number of selected component (if applicable):
qemu-img-6.0.0-17.module+el8.5.0+11173+c9fce0bb.x86_64

Expected results:

Report zeroed area in qcow2 image without the NBD_HOLE_BIT.

Example output that will be compatible with existing RHV code is:

$ nbdinfo --map nbd://localhost
         0      131072    0  allocated
    131072       65536    2  zero
    196608       65536    3  hole,zero

RHV will report:

    [
        { "start": 0, "length": 131072, "zero": false, "hole": false},
        { "start": 131072, "length": 65536, "zero": true, "hole": false}
        { "start": 196608, "length": 65536, "zero": true, "hole": true}
    ]

Client code can restore the qcow2 image by copying the first extents, and writing
zeroes in the second extent.


This issue affects also other programs like nbdcopy if they will try to support
copying of single images from qcow2 chain.

--- Additional comment from Klaus Heinrich Kiwi on 2021-06-08 18:46:17 UTC ---

Eric, can you have a look at this one?

--- Additional comment from Nir Soffer on 2021-06-08 20:47:12 UTC ---

I posted experimental fix upstream:
https://lists.nongnu.org/archive/html/qemu-block/2021-06/msg00347.html

This fixes RHV issue, but it is not complete and correct for all
cases, but there is discussion on the right way to solve this issue.

--- Additional comment from Eric Blake on 2021-06-09 18:07:58 UTC ---

I posted a counter-proposal patch that makes it easier to get at the already-existing information in qemu:
https://lists.gnu.org/archive/html/qemu-devel/2021-06/msg02446.html

But note that such a patch requires a change in ovirt to consume the new context; and there is still more work to be done before qemu-img map can access the new information.  nbdinfo can easily list it, but even that project would need a patch to expose human-readable names of the bits.

So the upstream solution requires quite a few downstream backports across multiple projects, vs. a simpler choice of a downstream-only revert in just qemu of the patch that changed qemu 6.0 behavior.  But note that a downstream revert, while feasible in the short term, will not be perpetuated indefinitely, so ultimately ovirt will need to start paying attention to either qemu:allocation-depth or the proposed qemu:joint-allocation, where a qemu revert is only an option for qemu versions that must be used with unpatched ovirt.

--- Additional comment from Nir Soffer on 2021-06-10 13:31:01 UTC ---

(In reply to Eric Blake from comment #3)
> But note that such a patch requires a change in ovirt to consume the new
> context; 

This is fine, I started to work on this here:
https://gerrit.ovirt.org/c/ovirt-imageio/+/115197

We can consume the new context in next ovirt release - 4.4.7.

> and there is still more work to be done before qemu-img map can
> access the new information.  nbdinfo can easily list it, but even that
> project would need a patch to expose human-readable names of the bits.

We don't depend on qemu-img map or nbdinfo, so this is fine. 

> So the upstream solution requires quite a few downstream backports across
> multiple projects, vs. a simpler choice of a downstream-only revert in just
> qemu of the patch that changed qemu 6.0 behavior.  But note that a
> downstream revert, while feasible in the short term, will not be perpetuated
> indefinitely, so ultimately ovirt will need to start paying attention to
> either qemu:allocation-depth or the proposed qemu:joint-allocation, where a
> qemu revert is only an option for qemu versions that must be used with
> unpatched ovirt.

I think we must revert the original patch downstream regardless of the new
context, since it breaks existing code, and the breakage is the worst kind,
silent data corruption during backup.

Reverting the patch will also give us more time work on a better long term
solution.

We will be happy to consume the new context in qemu 6.0, but we can also
wait to 6.1 if this work needs more time.

--- Additional comment from Nir Soffer on 2021-06-12 18:52:25 UTC ---

RHV-4.4.7 will resolve this issue by using qemu:allocation-depth, see bug 1971182.

--- Additional comment from  on 2021-06-15 05:34:52 UTC ---

Can reproduce this issue,
Version:
qemu-kvm-6.0.0-19.module+el8.5.0+11385+6e7d542e.x86_64
kernel-4.18.0-310.el8.x86_64

Steps:
# Create an image with a backing file:
$ qemu-img create base.raw $((4*64*1024))
$ qemu-img create -f qcow2 -b base.raw -F raw top.qcow2

# Write data to the first 3 cluster in the base image:
$ qemu-io -f raw -c "write -P 65 0 64k" base.raw
$ qemu-io -f raw -c "write -P 66 64k 64k" base.raw
$ qemu-io -f raw -c "write -P 67 128k 64k" base.raw

# Write data to the second cluster of the top image, hiding the second cluster in the backing file:
$ qemu-io -f qcow2 -c "write -P 69 64k 64k" top.qcow2

# Write zeroes to the third cluster of the top imge, hiding the third cluster in the backing file:
$ qemu-io -f qcow2 -c "write -z 128k 64k" top.qcow2



# Query the image via qemu-nbd

$ qemu-nbd -r -t -f qcow2 top.qcow2 &

$ qemu-img map --output json nbd://localhost
[{ "start": 0, "length": 131072, "depth": 0, "zero": false, "data": true, "offset": 0},
{ "start": 131072, "length": 131072, "depth": 0, "zero": true, "data": false, "offset": 131072}]

$ nbdinfo --map nbd://localhost
         0      131072    0  allocated
    131072      131072    3  hole,zero

Expected result:
should not report a hole.

Comment 1 Eric Blake 2021-09-30 19:43:12 UTC
Further upstream discussion decided that:
- adding a 'qemu:joint-allocation' metacontext NBD extension was not necessary.
https://lists.gnu.org/archive/html/qemu-devel/2021-06/msg02787.html
- RHV is planning to consume the already-existing qemu-nbd 'qemu:allocation-depth' metacontext correctly.
- 'qemu-img map --output=json' was enhanced to show better output:
  a275b452c65     qemu-img: Reword 'qemu-img map --output=json' docs
  8417e1378ca     qemu-img: Make unallocated part of backing chain obvious in map
Upstream qemu 6.1 contains both those patches, therefore RHEL 9 picks them up by rebase.

Comment 2 Tingting Mao 2021-10-08 07:04:03 UTC
Hi Eric,

After tried again in latest qemu packages, still hit the issue. Could you please help to check?

Thanks.


Tested with:
qemu-kvm-6.1.0-4.el9
kernel-5.14.0-0.rc7.54.el9.x86_64


Steps:
1. Create an image with a backing file:
# qemu-img create base.raw $((4*64*1024))
# qemu-img create -f qcow2 -b base.raw -F raw top.qcow2

2. Write data to the first 3 cluster in the base image:
# qemu-io -f raw -c "write -P 65 0 64k" base.raw
# qemu-io -f raw -c "write -P 66 64k 64k" base.raw
# qemu-io -f raw -c "write -P 67 128k 64k" base.raw

3. Write data to the second cluster of the top image, hiding the second cluster in the backing file:
# qemu-io -f qcow2 -c "write -P 69 64k 64k" top.qcow2

4. Write zeroes to the third cluster of the top imge, hiding the third cluster in the backing file:
# qemu-io -f qcow2 -c "write -z 128k 64k" top.qcow2

5. Query the image via qemu-nbd
# qemu-nbd -r -t -f qcow2 top.qcow2 &
# qemu-img map --output json nbd://localhost
[{ "start": 0, "length": 131072, "depth": 0, "present": true, "zero": false, "data": true, "offset": 0},
{ "start": 131072, "length": 131072, "depth": 0, "present": true, "zero": true, "data": false, "offset": 131072}]

# nbdinfo --map nbd://localhost
         0      131072    0  data
    131072      131072    3  hole,zero



Note: from what I learnt from the bug, the expected result should be like:
# nbdinfo --map nbd://localhost
         0      131072    0  allocated
    131072       65536    2  zero
    196608       65536    3  hole,zero

# qemu-img map --output=json nbd://localhost
    [
        { "start": 0, "length": 131072, "zero": false, "hole": false},
        { "start": 131072, "length": 65536, "zero": true, "hole": false}
        { "start": 196608, "length": 65536, "zero": true, "hole": true}
    ]

Comment 3 Eric Blake 2021-10-11 13:10:27 UTC
(In reply to Tingting Mao from comment #2)
> Hi Eric,
> 
> After tried again in latest qemu packages, still hit the issue. Could you
> please help to check?
> 
> Thanks.
> 
> 
> Tested with:
> qemu-kvm-6.1.0-4.el9
> kernel-5.14.0-0.rc7.54.el9.x86_64
> 
> 
> Steps:
> 1. Create an image with a backing file:
> # qemu-img create base.raw $((4*64*1024))
> # qemu-img create -f qcow2 -b base.raw -F raw top.qcow2
> 
> 2. Write data to the first 3 cluster in the base image:
> # qemu-io -f raw -c "write -P 65 0 64k" base.raw
> # qemu-io -f raw -c "write -P 66 64k 64k" base.raw
> # qemu-io -f raw -c "write -P 67 128k 64k" base.raw
> 
> 3. Write data to the second cluster of the top image, hiding the second
> cluster in the backing file:
> # qemu-io -f qcow2 -c "write -P 69 64k 64k" top.qcow2
> 
> 4. Write zeroes to the third cluster of the top imge, hiding the third
> cluster in the backing file:
> # qemu-io -f qcow2 -c "write -z 128k 64k" top.qcow2

Good so far.

> 
> 5. Query the image via qemu-nbd
> # qemu-nbd -r -t -f qcow2 top.qcow2 &

This step fails to export the allocation depth information.  In order to faithfully recreate the backing chain, you want to also use -A, as in:

qemu-nbd -r -t -A -f qcow2 top.qcow2 &

> # qemu-img map --output json nbd://localhost
> [{ "start": 0, "length": 131072, "depth": 0, "present": true, "zero": false,
> "data": true, "offset": 0},
> { "start": 131072, "length": 131072, "depth": 0, "present": true, "zero":
> true, "data": false, "offset": 131072}]
> 
> # nbdinfo --map nbd://localhost
>          0      131072    0  data
>     131072      131072    3  hole,zero

This output is correct, but not the only possible answer.  Remember, '--map' is shorthand for '--map=base:allocation', which only shows the basic NBD block status.  When viewed over NBD, qemu cannot directly see any backing image.  From the point of view of the NBD protocol, the first two clusters have data, all remaining clusters read as zero, and behave as if currently unallocated.

You could also inspect the qemu allocation depth, to learn which portions of the image at the server come from which depth.  With the -A added above, that would be:

$ nbdinfo --map=qemu:allocation-depth nbd://localhost
         0       65536    2  backing depth 2
     65536      131072    1  local
    196608       65536    2  backing depth 2

which correctly shows clusters 0 and 3 from the backing file, and 1 and 2 from the top.qcow2 image.

> 
> 
> 
> Note: from what I learnt from the bug, the expected result should be like:
> # nbdinfo --map nbd://localhost
>          0      131072    0  allocated
>     131072       65536    2  zero
>     196608       65536    3  hole,zero

This would also be correct, but less precise.  The NBD protocol does not require the server to advertise whether a cluster is a hole (which would be how you get this information).  In your particular case, it depends on whether qemu's code for inspecting top.qcow2 reports that cluster as merely reading as zero, or as also being currently unallocated.  In general, the reporting of 'zero' is more important that the reporting of 'hole', and it is merely a quality-of-implementation, rather than a bug, if 'hole' is omitted where it could have been reported.  But knowing where the holes are is insufficient for reconstructing a backing chain, particularly since this was one of the actual bug fixes in upstream qemu (older qemu reported an unallocated zero cluster as 'zero' only; newer qemu more precisely reports it as 'hole,zero').

And to double-check that top.qcow2 has an unallocated zero cluster (rather than an allocated cluster with actual zero contents), you can perform:

$ qemu-img map --output=json -f qcow2 top.qcow2
[{ "start": 0, "length": 65536, "depth": 1, "present": true, "zero": false, "data": true, "offset": 0},
{ "start": 65536, "length": 65536, "depth": 0, "present": true, "zero": false, "data": true, "offset": 327680},
{ "start": 131072, "length": 65536, "depth": 0, "present": true, "zero": true, "data": false},
{ "start": 196608, "length": 65536, "depth": 1, "present": true, "zero": true, "data": false, "offset": 196608}]

Note how cluster 2 does not have an "offset" output, showing that the earlier 'qemu-io -c "write -z ...' command did the write zero without actually allocating the cluster in the qcow2 file.

If you use "write -P 0" instead, that would force the cluster to be allocated, but then qemu loses the ability to quickly determine that the cluster reads as zero, so you get this different output (but also correct):

$ qemu-io -f qcow2 -c 'w -P 0 128k 64k' top.qcow2
wrote 65536/65536 bytes at offset 131072
64 KiB, 1 ops; 00.02 sec (4.148 MiB/sec and 66.3733 ops/sec)
$ qemu-img map --output=json -f qcow2 top.qcow2
[{ "start": 0, "length": 65536, "depth": 1, "present": true, "zero": false, "data": true, "offset": 0},
{ "start": 65536, "length": 131072, "depth": 0, "present": true, "zero": false, "data": true, "offset": 327680},
{ "start": 196608, "length": 65536, "depth": 1, "present": true, "zero": true, "data": false, "offset": 196608}]
$ qemu-nbd -r -t -f qcow2 -A top.qcow2
$ nbdinfo --map nbd://localhost
         0      196608    0  data
    196608       65536    3  hole,zero


> 
> # qemu-img map --output=json nbd://localhost
>     [
>         { "start": 0, "length": 131072, "zero": false, "hole": false},
>         { "start": 131072, "length": 65536, "zero": true, "hole": false}
>         { "start": 196608, "length": 65536, "zero": true, "hole": true}
>     ]

The upstream change made in qemu 6.1 in response to this bug was the addition of the "present":true/false boolean when directly querying qcow2 files with 'qemu-img map'; but when querying over NBD, you will never see "present":false (because from qemu's perspective, there is no backing file on qemu's end).  Reconstructing the backing chain over NBD requires the use of the qemu:allocation-depth metadata context (using -A on the qemu-nbd side, and using nbdinfo --map=qemu:allocation-depth or similar on the client side).  But that behavior was already possible in upstream qemu 6.0, so there remains nothing to change downstream as a result of this bug.

Comment 4 John Ferlan 2021-10-12 20:14:41 UTC
I added ITR=9.0.0 - since the fix went into 6.1, we could conceivably use bug 1997408 as the depends on bug and qemu-kvm-6.1.0-1.el9 as the fixed in version... We'd just need QE to set ITM to get the release+, then "if" verified with the above information just moved to VERIFIED (unless we need Mirek to add to errata in some way - that's a step I'm never quite clear if we need to do).

Comment 5 Tingting Mao 2021-10-13 08:25:13 UTC
It's clear now. Thanks a lot for Eric's info.


Exposed the image as below:
# qemu-nbd -r -t -A -f qcow2 top.qcow2 &

Dump the image info:
# nbdinfo --map=qemu:allocation-depth nbd://localhost
         0       65536    2  backing depth 2
     65536      131072    1  local
    196608       65536    2  backing depth 2

# qemu-img map --output=json nbd://localhost
[{ "start": 0, "length": 131072, "depth": 0, "present": true, "zero": false, "data": true, "offset": 0},
{ "start": 131072, "length": 131072, "depth": 0, "present": true, "zero": true, "data": false, "offset": 131072}]

# qemu-img map --output=json -f qcow2 top.qcow2 
[{ "start": 0, "length": 65536, "depth": 1, "present": true, "zero": false, "data": true, "offset": 0},
{ "start": 65536, "length": 65536, "depth": 0, "present": true, "zero": false, "data": true, "offset": 327680},
{ "start": 131072, "length": 65536, "depth": 0, "present": true, "zero": true, "data": false},
{ "start": 196608, "length": 65536, "depth": 1, "present": true, "zero": true, "data": false, "offset": 196608}]

Comment 7 Tingting Mao 2021-10-15 06:39:03 UTC
According to Comment 3, Comment 4 and Comment 5, close this bug as currentrelease. Thanks.