Skip to contentSkip to conversation

add-cow file format

PATCH Superseded Dong Xu Wang 4 participants 12 messages Oct 18, 2012 – Oct 22, 2012 Block layer

v13 of 20 Superseded Latest is v20 →

lore
Share & tools
lore
#0 lore raw

v13 cover add-cow file format

It will introduce a new file format: add-cow.

The add-cow file format makes it possible to perform copy-on-write on top of a raw disk image. When we know that no backing file clusters remain visible (e.g. we have streamed the entire image and copied all data from the backing file), then it is possible to discard the add-cow file and use the raw image file directly.

This feature adds the copy-on-write feature to raw files (which cannot support it natively) while allowing us to get full performance again later when we no longer need copy-on-write.

add-cow can benefit from other available functions, such as path_has_protocol and qed_read_string, so we will make them public.

snapshot_blkdev are not supported now for add-cow. Will add it in futher patches.

These patches are using QemuOpts parser, former patches could be found here:
http://patchwork.ozlabs.org/patch/191347/

v12->v13:
1) Use QemuOpts, not QEMUOptionParameter
2) cluster_size configuable
3) Refactor block-cache.c
4) Correct qemu-iotests script.
5) Other bug fix.

v11->v12:
1) Removed un-used feature bit.
2) Share cache code with qcow2.c.
3) Remove snapshot_blkdev support, will add it in another patch.
5) COW Bitmap field in add-cow file will be multiple of 65536.
6) fix grammer and typo.

Dong Xu Wang (6):
docs: document for add-cow file format
make path_has_protocol non static
qed_read_string to bdrv_read_string
rename qcow2-cache.c to block-cache.c
add-cow file format core code.
qemu-iotests: add add-cow iotests support.

 block.c                      |   29 ++-
 block.h                      |    3 +
 block/Makefile.objs          |    4 +-
 block/add-cow.c              |  693 ++++++++++++++++++++++++++++++++++++++++++
 block/add-cow.h              |   85 +++++
 block/block-cache.c          |  321 +++++++++++++++++++
 block/block-cache.h          |   77 +++++
 block/qcow2-cache.c          |  323 --------------------
 block/qcow2-cluster.c        |   54 ++--
 block/qcow2-refcount.c       |   67 +++--
 block/qcow2.c                |   21 +-
 block/qcow2.h                |   24 +--
 block/qed.c                  |   34 +--
 block_int.h                  |    2 +
 docs/specs/add-cow.txt       |  139 +++++++++
 tests/qemu-iotests/017       |    2 +-
 tests/qemu-iotests/020       |    2 +-
 tests/qemu-iotests/common    |    6 +
 tests/qemu-iotests/common.rc |   15 +-
 trace-events                 |   13 +-
 20 files changed, 1465 insertions(+), 449 deletions(-)
 create mode 100644 block/add-cow.c
 create mode 100644 block/add-cow.h
 create mode 100644 block/block-cache.c
 create mode 100644 block/block-cache.h
 delete mode 100644 block/qcow2-cache.c
 create mode 100644 docs/specs/add-cow.txt
#1 lore raw

PATCH v13 1/6 docs: document for add-cow file format

Document for add-cow format, the usage and spec of add-cow are introduced.

---
 docs/specs/add-cow.txt |  139 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 139 insertions(+), 0 deletions(-)
 create mode 100644 docs/specs/add-cow.txt
docs/specs/add-cow.txt new file +139 -0
new file mode 100644
index 0000000..dc1e107
@@ -0,0 +1,139 @@
+== General ==
+
+The raw file format does not support backing files or copy on write feature.
+The add-cow image format makes it possible to use backing files with raw
+image by keeping a separate .add-cow metadata file. Once all sectors
+have been written into the raw image it is safe to discard the .add-cow
+and backing files, then we can use the raw image directly.
+
+An example usage of add-cow would look like::
+(ubuntu.img is a disk image which has been installed OS.)
+    1)  Create a raw image with the same size of ubuntu.img
+            qemu-img create -f raw test.raw 8G
+    2)  Create an add-cow image which will store dirty bitmap
+            qemu-img create -f add-cow test.add-cow \
+                -o backing_file=ubuntu.img,image_file=test.raw
+    3)  Run qemu with add-cow image
+            qemu -drive if=virtio,file=test.add-cow
+
+test.raw may be larger than ubuntu.img, in that case, the size of test.add-cow
+will be calculated from the size of test.raw.
+
+=Specification=
+
+The file format looks like this:
+
+ +---------------+-------------+-----------------+
+ |     Header    |   Reserved  |    COW bitmap   |
+ +---------------+-------------+-----------------+
+
+All numbers in add-cow are stored in Little Endian byte order.
+
+== Header ==
+
+The Header is included in the first bytes:
+(#define HEADER_SIZE (4096 * header_size))
+    Byte    0 -  7:     magic
+                        add-cow magic string ("ADD_COW\xff").
+
+            8 -  11:    version
+                        Version number (only valid value is 1 now).
+
+            12 - 15:    backing file name offset
+                        Offset in the add-cow file at which the backing file
+                        name is stored (NB: The string is not nul-terminated).
+                        If backing file name does NOT exist, this field will be
+                        0. Must be between 80 and [HEADER_SIZE - 2](a file name
+                        must be at least 1 byte).
+
+            16 - 19:    backing file name size
+                        Length of the backing file name in bytes. It will be 0
+                        if the backing file name offset is 0. If backing file
+                        name offset is non-zero, then it must be non-zero. Must
+                        be less than [HEADER_SIZE - 80] to fit in the reserved
+                        part of the header.
+
+            20 - 23:    image file name offset
+                        Offset in the add-cow file at which the image file name
+                        is stored (NB: The string is not null terminated). It
+                        must be between 80 and [HEADER_SIZE - 2].
+
+            24 - 27:    image file name size
+                        Length of the image file name in bytes.
+                        Must be less than [HEADER_SIZE - 80] to fit in the reserved
+                        part of the header.
+
+            28 - 31:    cluster bits
+                        Number of bits that are used for addressing an offset
+                        within a cluster (1 << cluster_bits is the cluster size).
+                        Must not be less than 9 (i.e. 512 byte clusters).
+
+                        Note: qemu as of today has an implementation limit of 2 MB
+                        as the maximum cluster size and won't be able to open images
+                        with larger cluster sizes.
+
+            32 - 39:    features
+                        Bitmask of features. An implementation can safely ignore
+                        any unknown bits that are set.
+
+                        Bit 0:      All allocated bit.  If this bit is set then
+                                    backing file and COW bitmap will not be used,
+                                    and can read from or write to image file directly.
+
+                        Bits 1-63:  Reserved (set to 0)
+
+            40 - 47:    optional features
+                        Not used now. Reserved for future use. It must be set to 0.
+                        And must be ignored while reading.
+
+            48 - 51:    header size
+                        The header field is variable-sized. This field indicates
+                        how many 4096 bytes will be used to store add-cow header.
+                        In add-cow v1, it is fixed to 1, so the header size will
+                        be 4096 * 1 = 4096 bytes.
+
+            52 - 67:    backing file format
+                        Format of backing file. It will be filled with 0 if
+                        backing file name offset is 0. If backing file name
+                        offset is non-empty, it must be non-empty. It is coded
+                        in free-form ASCII, and is not NUL-terminated. Zero
+                        padded on the right.
+
+            68 - 83:    image file format
+                        Format of image file. It must be non-empty. It is coded
+                        in free-form ASCII, and is not NUL-terminated. Zero
+                        padded on the right.
+
+            84 - [HEADER_SIZE - 1]:
+                        It is used to make sure COW bitmap field starts at the
+                        HEADER_SIZE byte, backing file name and image file name
+                        will be stored here. The bytes that is not pointing to
+                        backing file and image file names must be set to 0.
+
+== COW bitmap ==
+
+The "COW bitmap" field starts at offset HEADER_SIZE, stores a bitmap related to
+backing file and image file. The bitmap will track whether the sector in
+backing file is dirty or not.
+
+Each bit in the bitmap tracks one cluster's status. For example, if cluster
+bit is 16, then each bit tracks one cluster, (1 >> 16) = 65536 bytes. The size
+of bitmap is calculated according to virtual size of image file, and it must
+be multiple of 65536, the bits not used will be set to 0. Within each byte,
+the least significant bit covers the first cluster. Bit orders in one
+byte look like:
+ +----+----+----+----+----+----+----+----+
+ | b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 |
+ +----+----+----+----+----+----+----+----+
+
+If the bit is 0, indicates the sector has not been allocated in image file, data
+should be loaded from backing file while reading; if the bit is 1, indicates the
+related sector has been dirty, should be loaded from image file while reading.
+Writing to a sector causes the corresponding bit to be set to 1.
+
+If raw image is not an even multiple of cluster bytes, bits that correspond to
+bytes beyond the raw file size in add-cow must be written as 0 and must be
+ignored when reading.
+
+Image file name and backing file name must NOT be the same, we prevent this
+while creating add-cow files.
Signature · 1 line
1.7.1
#2 lore raw

Re: [Qemu-devel] [PATCH V13 1/6] docs: document for add-cow file format

Quoted 14 lines · Dong Xu Wang

Document for add-cow format, the usage and spec of add-cow are introduced.

---
 docs/specs/add-cow.txt |  139 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 139 insertions(+), 0 deletions(-)
 create mode 100644 docs/specs/add-cow.txt
docs/specs/add-cow.txt new file +0 -0
new file mode 100644
index 0000000..dc1e107

docs/specs/add-cow.txt line 4

Quoted 4 lines
docs/specs/add-cow.txt new file +4 -0
@@ -0,0 +1,139 @@
+== General ==
+
+The raw file format does not support backing files or copy on write feature.
+The add-cow image format makes it possible to use backing files with raw

s/with raw/with a raw/

docs/specs/add-cow.txt line 10

Quoted 6 lines
docs/specs/add-cow.txt new file +6 -0
+image by keeping a separate .add-cow metadata file. Once all sectors
+have been written into the raw image it is safe to discard the .add-cow
+and backing files, then we can use the raw image directly.
+
+An example usage of add-cow would look like::
+(ubuntu.img is a disk image which has been installed OS.)

s/has been installed/has an installed/

Quoted 8 lines
docs/specs/add-cow.txt new file +8 -0
+    1)  Create a raw image with the same size of ubuntu.img
+            qemu-img create -f raw test.raw 8G
+    2)  Create an add-cow image which will store dirty bitmap
+            qemu-img create -f add-cow test.add-cow \
+                -o backing_file=ubuntu.img,image_file=test.raw
+    3)  Run qemu with add-cow image
+            qemu -drive if=virtio,file=test.add-cow
+

docs/specs/add-cow.txt line 28

Quoted 10 lines
docs/specs/add-cow.txt new file +10 -0
+test.raw may be larger than ubuntu.img, in that case, the size of test.add-cow
+will be calculated from the size of test.raw.
+
+=Specification=
+
+The file format looks like this:
+
+ +---------------+-------------+-----------------+
+ |     Header    |   Reserved  |    COW bitmap   |
+ +---------------+-------------+-----------------+

Since you call out what all 4096 bytes must be in the Header section (all bytes not occupied by a backing file name or format must be NUL), do your really need Reserved in this section, or can you just claim that the 4096-byte header is directly followed by the COW bitmap?

Quoted 16 lines
docs/specs/add-cow.txt new file +16 -0
+
+All numbers in add-cow are stored in Little Endian byte order.
+
+== Header ==
+
+The Header is included in the first bytes:
+(#define HEADER_SIZE (4096 * header_size))
+    Byte    0 -  7:     magic
+                        add-cow magic string ("ADD_COW\xff").
+
+            8 -  11:    version
+                        Version number (only valid value is 1 now).
+
+            12 - 15:    backing file name offset
+                        Offset in the add-cow file at which the backing file
+                        name is stored (NB: The string is not nul-terminated).

docs/specs/add-cow.txt line 54

Quoted 10 lines
docs/specs/add-cow.txt new file +10 -0
+                        If backing file name does NOT exist, this field will be
+                        0. Must be between 80 and [HEADER_SIZE - 2](a file name
+                        must be at least 1 byte).
+
+            16 - 19:    backing file name size
+                        Length of the backing file name in bytes. It will be 0
+                        if the backing file name offset is 0. If backing file
+                        name offset is non-zero, then it must be non-zero. Must
+                        be less than [HEADER_SIZE - 80] to fit in the reserved
+                        part of the header.

More specifically, it must be small enough so that offset+size <= HEADER_SIZE.

docs/specs/add-cow.txt line 64

Quoted 10 lines
docs/specs/add-cow.txt new file +10 -0
+
+            20 - 23:    image file name offset
+                        Offset in the add-cow file at which the image file name
+                        is stored (NB: The string is not null terminated). It
+                        must be between 80 and [HEADER_SIZE - 2].
+
+            24 - 27:    image file name size
+                        Length of the image file name in bytes.
+                        Must be less than [HEADER_SIZE - 80] to fit in the reserved
+                        part of the header.

More specifically, it must be small enough so that offset+size <= HEADER_SIZE.

docs/specs/add-cow.txt line 77

Quoted 13 lines
docs/specs/add-cow.txt new file +13 -0
+
+            28 - 31:    cluster bits
+                        Number of bits that are used for addressing an offset
+                        within a cluster (1 << cluster_bits is the cluster size).
+                        Must not be less than 9 (i.e. 512 byte clusters).
+
+                        Note: qemu as of today has an implementation limit of 2 MB
+                        as the maximum cluster size and won't be able to open images
+                        with larger cluster sizes.
+
+            32 - 39:    features
+                        Bitmask of features. An implementation can safely ignore
+                        any unknown bits that are set.

Really? That sounds more like optional features, if an implementation can ignore a set bit. You really want to require that implementations reject operations on a file with a feature bit set that they don't recognize.

docs/specs/add-cow.txt line 81

Quoted 4 lines
docs/specs/add-cow.txt new file +4 -0
+
+                        Bit 0:      All allocated bit.  If this bit is set then
+                                    backing file and COW bitmap will not be used,
+                                    and can read from or write to image file directly.

And this particular bit sounds like an optional feature - setting the bit is an optimization in speed, but leaving the bit clear or ignoring the bit when it is set does not change correctness.

docs/specs/add-cow.txt line 87

Quoted 6 lines
docs/specs/add-cow.txt new file +6 -0
+
+                        Bits 1-63:  Reserved (set to 0)
+
+            40 - 47:    optional features
+                        Not used now. Reserved for future use. It must be set to 0.
+                        And must be ignored while reading.

s/. And must be ignored/, and ignored/

docs/specs/add-cow.txt line 93

Quoted 6 lines
docs/specs/add-cow.txt new file +6 -0
+
+            48 - 51:    header size
+                        The header field is variable-sized. This field indicates
+                        how many 4096 bytes will be used to store add-cow header.
+                        In add-cow v1, it is fixed to 1, so the header size will
+                        be 4096 * 1 = 4096 bytes.

So is the value '1' or '4096' in this field? The wording isn't quite clear. But reading elsewhere, it looks like this should always be '1' in version one add-cow.

docs/specs/add-cow.txt

Quoted 5 lines
docs/specs/add-cow.txt new file +5 -0
+
+            52 - 67:    backing file format
+                        Format of backing file. It will be filled with 0 if
+                        backing file name offset is 0. If backing file name
+                        offset is non-empty, it must be non-empty.

Are you going to enforce this? Normally, if backing file format is omitted, then qemu knows how to probe backing file format (with the caveat that it is a security risk if the probe returns non-raw but the backing file really was raw).

docs/specs/add-cow.txt line 105

Quoted 8 lines

It is coded

docs/specs/add-cow.txt new file +7 -0
+                        in free-form ASCII, and is not NUL-terminated. Zero
+                        padded on the right.
+
+            68 - 83:    image file format
+                        Format of image file. It must be non-empty. It is coded
+                        in free-form ASCII, and is not NUL-terminated. Zero
+                        padded on the right.

Why do we need this field? Isn't the image file ALWAYS raw?

docs/specs/add-cow.txt

Quoted 2 lines
docs/specs/add-cow.txt new file +2 -0
+
+            84 - [HEADER_SIZE - 1]:

Elsewhere in your spec, you use 80 rather than 84 for the starting point of valid offsets. Which is it?

docs/specs/add-cow.txt line 111

Quoted 4 lines
docs/specs/add-cow.txt new file +4 -0
+                        It is used to make sure COW bitmap field starts at the
+                        HEADER_SIZE byte, backing file name and image file name
+                        will be stored here. The bytes that is not pointing to
+                        backing file and image file names must be set to 0.

Not just file names, but also backing file format.

docs/specs/add-cow.txt line 115

Quoted 4 lines
docs/specs/add-cow.txt new file +4 -0
+
+== COW bitmap ==
+
+The "COW bitmap" field starts at offset HEADER_SIZE, stores a bitmap related to

Shouldn't that be HEADER_SIZE * number of header pages, since you dedicated a field in the header for that purpose?

docs/specs/add-cow.txt line 120

Quoted 5 lines
docs/specs/add-cow.txt new file +5 -0
+backing file and image file. The bitmap will track whether the sector in
+backing file is dirty or not.
+
+Each bit in the bitmap tracks one cluster's status. For example, if cluster
+bit is 16, then each bit tracks one cluster, (1 >> 16) = 65536 bytes. The size

s/>>/<</

docs/specs/add-cow.txt line 122

Quoted 2 lines
docs/specs/add-cow.txt new file +2 -0
+of bitmap is calculated according to virtual size of image file, and it must
+be multiple of 65536, the bits not used will be set to 0. Within each byte,

What must be a multiple of 65536, the image file, or the size of the bitmap in the add-cow file? I think what you want is:

The image size is rounded up to cluster size (where any bytes in the last cluster that do not fit in the image are ignored), then if the number of clusters is not a multiple of 8, then remaining bits in the bitmap will be set to 0.

Or do you really want to require that the bitmap is a multiple of 64k bytes (at 8 bits per byte, that means the bitmap covers a multiple of 512k clusters, and at 512 bytes as the minimum cluster size, that the add-cow file format manages a minimum of 256M)? That is, are you requiring that the bitmap end on an aligned boundary, to make the bitmap easier to use without having to special case a short-read on the last page of the bitmap?

docs/specs/add-cow.txt line 130

Quoted 8 lines
docs/specs/add-cow.txt new file +8 -0
+the least significant bit covers the first cluster. Bit orders in one
+byte look like:
+ +----+----+----+----+----+----+----+----+
+ | b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 |
+ +----+----+----+----+----+----+----+----+
+
+If the bit is 0, indicates the sector has not been allocated in image file, data
+should be loaded from backing file while reading; if the bit is 1, indicates the

s/indicates/it indicates/ (twice)

If there is no backing file, or if the image file is larger than the backing file and the offset is beyond the end of the backing file, then the data should be read as all zero bytes instead.

docs/specs/add-cow.txt line 139

Quoted 9 lines
docs/specs/add-cow.txt new file +9 -0
+related sector has been dirty, should be loaded from image file while reading.
+Writing to a sector causes the corresponding bit to be set to 1.
+
+If raw image is not an even multiple of cluster bytes, bits that correspond to
+bytes beyond the raw file size in add-cow must be written as 0 and must be
+ignored when reading.
+
+Image file name and backing file name must NOT be the same, we prevent this
+while creating add-cow files.

You prevent it when creating add-cow files via qemu-img, but that doesn't stop malicious users from creating a file with those properties and then trying to get you to parse it as add-cow. I think this needs to instead be a requirement on the consumer of a potentially bad file, and not a requirement on the producer to avoid bad files, since you can't control all producers, as in:
If image file name and backing file resolve to the same file, the add-cow image must be treated as invalid.

Signature · 2 lines
Eric Blake   [email protected]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#3 lore raw

Re: [Qemu-devel] [PATCH V13 1/6] docs: document for add-cow file format

Quoted 9 lines · Eric Blake
Quoted 8 lines · Dong Xu Wang

Document for add-cow format, the usage and spec of add-cow are introduced.

---
  docs/specs/add-cow.txt |  139 ++++++++++++++++++++++++++++++++++++++++++++++++
  1 files changed, 139 insertions(+), 0 deletions(-)

create mode 100644 docs/specs/add-cow.txt

Quoted 13 lines

docs/specs/add-cow.txt line 4

Quoted 10 lines
docs/specs/add-cow.txt new file +4 -0
new file mode 100644
index 0000000..dc1e107
@@ -0,0 +1,139 @@
+== General ==
+
+The raw file format does not support backing files or copy on write feature.
+The add-cow image format makes it possible to use backing files with raw

s/with raw/with a raw/

Okay.

Quoted 8 lines

docs/specs/add-cow.txt line 10

Quoted 6 lines
docs/specs/add-cow.txt new file +6 -0
+image by keeping a separate .add-cow metadata file. Once all sectors
+have been written into the raw image it is safe to discard the .add-cow
+and backing files, then we can use the raw image directly.
+
+An example usage of add-cow would look like::
+(ubuntu.img is a disk image which has been installed OS.)

s/has been installed/has an installed/

Okay.

Quoted 16 lines
Quoted 16 lines
docs/specs/add-cow.txt new file +15 -0
+    1)  Create a raw image with the same size of ubuntu.img
+            qemu-img create -f raw test.raw 8G
+    2)  Create an add-cow image which will store dirty bitmap
+            qemu-img create -f add-cow test.add-cow \
+                -o backing_file=ubuntu.img,image_file=test.raw
+    3)  Run qemu with add-cow image
+            qemu -drive if=virtio,file=test.add-cow
+
+test.raw may be larger than ubuntu.img, in that case, the size of test.add-cow
+will be calculated from the size of test.raw.
+
+=Specification=
+
+The file format looks like this:
+
Quoted 9 lines

docs/specs/add-cow.txt line 28

Quoted 3 lines
docs/specs/add-cow.txt new file +3 -0
+ +---------------+-------------+-----------------+
+ |     Header    |   Reserved  |    COW bitmap   |
+ +---------------+-------------+-----------------+

Since you call out what all 4096 bytes must be in the Header section (all bytes not occupied by a backing file name or format must be NUL), do your really need Reserved in this section, or can you just claim that the 4096-byte header is directly followed by the COW bitmap?

Okay, I think Header + COW would be enough.

Quoted 21 lines
Quoted 21 lines
docs/specs/add-cow.txt new file +21 -0
+
+All numbers in add-cow are stored in Little Endian byte order.
+
+== Header ==
+
+The Header is included in the first bytes:
+(#define HEADER_SIZE (4096 * header_size))
+    Byte    0 -  7:     magic
+                        add-cow magic string ("ADD_COW\xff").
+
+            8 -  11:    version
+                        Version number (only valid value is 1 now).
+
+            12 - 15:    backing file name offset
+                        Offset in the add-cow file at which the backing file
+                        name is stored (NB: The string is not nul-terminated).
+                        If backing file name does NOT exist, this field will be
+                        0. Must be between 80 and [HEADER_SIZE - 2](a file name
+                        must be at least 1 byte).
+
+            16 - 19:    backing file name size
Quoted 8 lines

docs/specs/add-cow.txt line 54

Quoted 5 lines
docs/specs/add-cow.txt new file +5 -0
+                        Length of the backing file name in bytes. It will be 0
+                        if the backing file name offset is 0. If backing file
+                        name offset is non-zero, then it must be non-zero. Must
+                        be less than [HEADER_SIZE - 80] to fit in the reserved
+                        part of the header.

More specifically, it must be small enough so that offset+size <= HEADER_SIZE.

Okay.

Quoted 15 lines

docs/specs/add-cow.txt line 64

Quoted 11 lines
docs/specs/add-cow.txt new file +10 -0
+
+            20 - 23:    image file name offset
+                        Offset in the add-cow file at which the image file name
+                        is stored (NB: The string is not null terminated). It
+                        must be between 80 and [HEADER_SIZE - 2].
+
+            24 - 27:    image file name size
+                        Length of the image file name in bytes.
+                        Must be less than [HEADER_SIZE - 80] to fit in the reserved
+                        part of the header.

More specifically, it must be small enough so that offset+size <= HEADER_SIZE.

Okay.

Quoted 10 lines
Quoted 10 lines
docs/specs/add-cow.txt new file +10 -0
+
+            28 - 31:    cluster bits
+                        Number of bits that are used for addressing an offset
+                        within a cluster (1 << cluster_bits is the cluster size).
+                        Must not be less than 9 (i.e. 512 byte clusters).
+
+                        Note: qemu as of today has an implementation limit of 2 MB
+                        as the maximum cluster size and won't be able to open images
+                        with larger cluster sizes.
+
Quoted 9 lines

docs/specs/add-cow.txt line 77

Quoted 3 lines
docs/specs/add-cow.txt new file +3 -0
+            32 - 39:    features
+                        Bitmask of features. An implementation can safely ignore
+                        any unknown bits that are set.

Really? That sounds more like optional features, if an implementation can ignore a set bit. You really want to require that implementations reject operations on a file with a feature bit set that they don't recognize.

Yep, I should reject the file if un-recognized bits are set.

Quoted 9 lines

docs/specs/add-cow.txt line 81

Quoted 4 lines
docs/specs/add-cow.txt new file +4 -0
+
+                        Bit 0:      All allocated bit.  If this bit is set then
+                                    backing file and COW bitmap will not be used,
+                                    and can read from or write to image file directly.

And this particular bit sounds like an optional feature - setting the bit is an optimization in speed, but leaving the bit clear or ignoring the bit when it is set does not change correctness.

Okay, but if this bit is categorized to optional feature, I will be no features bit then...

Quoted 9 lines

docs/specs/add-cow.txt line 87

Quoted 6 lines
docs/specs/add-cow.txt new file +6 -0
+
+                        Bits 1-63:  Reserved (set to 0)
+
+            40 - 47:    optional features
+                        Not used now. Reserved for future use. It must be set to 0.
+                        And must be ignored while reading.

s/. And must be ignored/, and ignored/

Okay.

Quoted 11 lines

docs/specs/add-cow.txt line 93

Quoted 6 lines
docs/specs/add-cow.txt new file +6 -0
+
+            48 - 51:    header size
+                        The header field is variable-sized. This field indicates
+                        how many 4096 bytes will be used to store add-cow header.
+                        In add-cow v1, it is fixed to 1, so the header size will
+                        be 4096 * 1 = 4096 bytes.

So is the value '1' or '4096' in this field? The wording isn't quite clear. But reading elsewhere, it looks like this should always be '1' in version one add-cow.

I mean '1' in this field, I will describe more clearly in future patch.

Quoted 11 lines

docs/specs/add-cow.txt

Quoted 5 lines
docs/specs/add-cow.txt new file +5 -0
+
+            52 - 67:    backing file format
+                        Format of backing file. It will be filled with 0 if
+                        backing file name offset is 0. If backing file name
+                        offset is non-empty, it must be non-empty.

Are you going to enforce this? Normally, if backing file format is omitted, then qemu knows how to probe backing file format (with the caveat that it is a security risk if the probe returns non-raw but the backing file really was raw).

To avoid the security risk I add this field in header, can I do anything to enforce this more?

Quoted 11 lines

docs/specs/add-cow.txt line 105

Quoted 8 lines

It is coded

docs/specs/add-cow.txt new file +7 -0
+                        in free-form ASCII, and is not NUL-terminated. Zero
+                        padded on the right.
+
+            68 - 83:    image file format
+                        Format of image file. It must be non-empty. It is coded
+                        in free-form ASCII, and is not NUL-terminated. Zero
+                        padded on the right.

Why do we need this field? Isn't the image file ALWAYS raw?

In v1, it only supports raw as a image_file, but in future it might support other formats which do not support COW, so I add image file format here.

Quoted 5 lines

docs/specs/add-cow.txt

Quoted 2 lines
docs/specs/add-cow.txt new file +2 -0
+
+            84 - [HEADER_SIZE - 1]:

Elsewhere in your spec, you use 80 rather than 84 for the starting point of valid offsets. Which is it?

Sorry, I add "cluster bits", which is 4 bytes, and I forgot changing 80 to 84. Will correct.

Quoted 8 lines

docs/specs/add-cow.txt line 111

Quoted 5 lines
docs/specs/add-cow.txt new file +4 -0
+                        It is used to make sure COW bitmap field starts at the
+                        HEADER_SIZE byte, backing file name and image file name
+                        will be stored here. The bytes that is not pointing to
+                        backing file and image file names must be set to 0.

Not just file names, but also backing file format.

Okay.

Quoted 8 lines

docs/specs/add-cow.txt line 115

Quoted 4 lines
docs/specs/add-cow.txt new file +4 -0
+
+== COW bitmap ==
+
+The "COW bitmap" field starts at offset HEADER_SIZE, stores a bitmap related to

Shouldn't that be HEADER_SIZE * number of header pages, since you dedicated a field in the header for that purpose?

There is one line in this doc:
(#define HEADER_SIZE (4096 * header_size)) HEADER_SIZE and header_size are not good names, will correct.

Quoted 8 lines

docs/specs/add-cow.txt line 120

Quoted 5 lines
docs/specs/add-cow.txt new file +5 -0
+backing file and image file. The bitmap will track whether the sector in
+backing file is dirty or not.
+
+Each bit in the bitmap tracks one cluster's status. For example, if cluster
+bit is 16, then each bit tracks one cluster, (1 >> 16) = 65536 bytes. The size

s/>>/<</

Okay.

Quoted 6 lines

docs/specs/add-cow.txt line 122

Quoted 2 lines
docs/specs/add-cow.txt new file +2 -0
+of bitmap is calculated according to virtual size of image file, and it must
+be multiple of 65536, the bits not used will be set to 0. Within each byte,

What must be a multiple of 65536, the image file, or the size of the bitmap in the add-cow file? I think what you want is:

I mean bitmap must be multiple for 65536. Will describe more clearly.

Quoted 5 lines

The image size is rounded up to cluster size (where any bytes in the last cluster that do not fit in the image are ignored), then if the number of clusters is not a multiple of 8, then remaining bits in the bitmap will be set to 0.

Quoted 8 lines

Or do you really want to require that the bitmap is a multiple of 64k bytes (at 8 bits per byte, that means the bitmap covers a multiple of 512k clusters, and at 512 bytes as the minimum cluster size, that the add-cow file format manages a minimum of 256M)? That is, are you requiring that the bitmap end on an aligned boundary, to make the bitmap easier to use without having to special case a short-read on the last page of the bitmap?

I think my description was wrong. add-cow is using block-cache.c, which is from qcow2-cache.c. I was not to change qcow2-cache.c to a great extent, so I use it directly, and it uses cluster_size as writing unit:
ret = bdrv_pwrite(bs->file, c->entries[i].offset, c->entries[i].table, s->cluster_size);

So the size of bitmap would be multiple of cluster_size of add-cow, do you think if it is acceptable?

Quoted 11 lines

docs/specs/add-cow.txt line 130

Quoted 8 lines
docs/specs/add-cow.txt new file +8 -0
+the least significant bit covers the first cluster. Bit orders in one
+byte look like:
+ +----+----+----+----+----+----+----+----+
+ | b7 | b6 | b5 | b4 | b3 | b2 | b1 | b0 |
+ +----+----+----+----+----+----+----+----+
+
+If the bit is 0, indicates the sector has not been allocated in image file, data
+should be loaded from backing file while reading; if the bit is 1, indicates the

s/indicates/it indicates/ (twice)

Okay.

Quoted 4 lines

If there is no backing file, or if the image file is larger than the backing file and the offset is beyond the end of the backing file, then the data should be read as all zero bytes instead.

Okay.

Quoted 10 lines

docs/specs/add-cow.txt line 139

Quoted 9 lines
docs/specs/add-cow.txt new file +9 -0
+related sector has been dirty, should be loaded from image file while reading.
+Writing to a sector causes the corresponding bit to be set to 1.
+
+If raw image is not an even multiple of cluster bytes, bits that correspond to
+bytes beyond the raw file size in add-cow must be written as 0 and must be
+ignored when reading.
+
+Image file name and backing file name must NOT be the same, we prevent this
+while creating add-cow files.
Quoted 8 lines

You prevent it when creating add-cow files via qemu-img, but that doesn't stop malicious users from creating a file with those properties and then trying to get you to parse it as add-cow. I think this needs to instead be a requirement on the consumer of a potentially bad file, and not a requirement on the producer to avoid bad files, since you can't control all producers, as in:
If image file name and backing file resolve to the same file, the add-cow image must be treated as invalid.

Okay, I will check it while opening add-cow file.

Quoted 1 line

Thank you Eric.

#4 lore raw

PATCH v13 2/6 make path_has_protocol non static

In reply to Dong Xu Wang

We will use path_has_protocol outside block.c, so just make it public.

---
 block.c |    2 +-
 block.h |    1 +
 2 files changed, 2 insertions(+), 1 deletions(-)
block.c +1 -1
index f639655..03ba485 100644
@@ -198,7 +198,7 @@ static void bdrv_io_limits_intercept(BlockDriverState *bs,
 }
 
 /* check if the path starts with "<protocol>:" */
-static int path_has_protocol(const char *path)
+int path_has_protocol(const char *path)
 {
     const char *p;
 
block.h +1 -0
index 7842d85..364ba04 100644
@@ -329,6 +329,7 @@ char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn);
 
 char *get_human_readable_size(char *buf, int buf_size, int64_t size);
 int path_is_absolute(const char *path);
+int path_has_protocol(const char *path);
 void path_combine(char *dest, int dest_size,
                   const char *base_path,
                   const char *filename);
Signature · 1 line
1.7.1
#5 lore raw

PATCH v13 3/6 qed_read_string to bdrv_read_string

In reply to Dong Xu Wang

Make qed_read_string function to a common interface, so move it to block.c.

---
 block.c     |   27 +++++++++++++++++++++++++++
 block.h     |    2 ++
 block/qed.c |   34 ++++------------------------------
 3 files changed, 33 insertions(+), 30 deletions(-)
block.c +27 -0
index 03ba485..9afb7b5 100644
@@ -215,6 +215,33 @@ int path_has_protocol(const char *path)
     return *p == ':';
 }
 
+/**
+ * Read a string of known length from the image file
+ *
+ * @bs:         Image file
+ * @offset:     File offset to start of string, in bytes
+ * @n:          String length in bytes
+ * @buf:        Destination buffer
+ * @buflen:     Destination buffer length in bytes
+ * @ret:        0 on success, -errno on failure
+ *
+ * The string is NUL-terminated.
+ */
+int bdrv_read_string(BlockDriverState *bs, uint64_t offset, size_t n,
+                     char *buf, size_t buflen)
+{
+    int ret;
+    if (n >= buflen) {
+        return -EINVAL;
+    }
+    ret = bdrv_pread(bs, offset, buf, n);
+    if (ret < 0) {
+        return ret;
+    }
+    buf[n] = '\0';
+    return 0;
+}
+
 int path_is_absolute(const char *path)
 {
 #ifdef _WIN32
block.h +2 -0
index 364ba04..166e00c 100644
@@ -168,6 +168,8 @@ int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
     const void *buf, int count);
 int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
     int nb_sectors, QEMUIOVector *qiov);
+int bdrv_read_string(BlockDriverState *bs, uint64_t offset, size_t n,
+                     char *buf, size_t buflen);
 int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
     int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
 int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
block/qed.c +4 -30
index 0a9dbe8..096de21 100644
@@ -217,33 +217,6 @@ static bool qed_is_image_size_valid(uint64_t image_size, uint32_t cluster_size,
 }
 
 /**
- * Read a string of known length from the image file
- *
- * @file:       Image file
- * @offset:     File offset to start of string, in bytes
- * @n:          String length in bytes
- * @buf:        Destination buffer
- * @buflen:     Destination buffer length in bytes
- * @ret:        0 on success, -errno on failure
- *
- * The string is NUL-terminated.
- */
-static int qed_read_string(BlockDriverState *file, uint64_t offset, size_t n,
-                           char *buf, size_t buflen)
-{
-    int ret;
-    if (n >= buflen) {
-        return -EINVAL;
-    }
-    ret = bdrv_pread(file, offset, buf, n);
-    if (ret < 0) {
-        return ret;
-    }
-    buf[n] = '\0';
-    return 0;
-}
-
-/**
  * Allocate new clusters
  *
  * @s:          QED state
@@ -437,9 +410,10 @@ static int bdrv_qed_open(BlockDriverState *bs, int flags)
             return -EINVAL;
         }
 
-        ret = qed_read_string(bs->file, s->header.backing_filename_offset,
-                              s->header.backing_filename_size, bs->backing_file,
-                              sizeof(bs->backing_file));
+        ret = bdrv_read_string(bs->file, s->header.backing_filename_offset,
+                               s->header.backing_filename_size,
+                               bs->backing_file,
+                               sizeof(bs->backing_file));
         if (ret < 0) {
             return ret;
         }
Signature · 1 line
1.7.1
#6 lore raw

PATCH v13 4/6 rename qcow2-cache.c to block-cache.c

In reply to Dong Xu Wang

We will re-use qcow2-cache as block layer common cache code, so change its name and made some changes, define a struct named BlockTableType, pass BlockTableType and table size parameters to block cache initialization function.

---
 block/Makefile.objs    |    3 +-
 block/block-cache.c    |  317 +++++++++++++++++++++++++++++++++++++++++++++++
 block/block-cache.h    |   76 +++++++++++
 block/qcow2-cache.c    |  323 ------------------------------------------------
 block/qcow2-cluster.c  |   54 +++++----
 block/qcow2-refcount.c |   67 ++++++-----
 block/qcow2.c          |   21 ++--
 block/qcow2.h          |   24 +---
 trace-events           |   13 +-
 9 files changed, 483 insertions(+), 415 deletions(-)
 create mode 100644 block/block-cache.c
 create mode 100644 block/block-cache.h
 delete mode 100644 block/qcow2-cache.c
block/Makefile.objs +2 -1
index 554f429..f128b78 100644
@@ -1,5 +1,6 @@
 block-obj-y += raw.o cow.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat.o
-block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o qcow2-cache.o
+block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o
+block-obj-y += block-cache.o
 block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
 block-obj-y += qed-check.o
 block-obj-y += parallels.o nbd.o blkdebug.o sheepdog.o blkverify.o
block/block-cache.c new file +317 -0

317 lines not on this page · raw message

block/block-cache.h new file +76 -0

76 lines not on this page · raw message

block/qcow2-cache.c deleted +0 -323

323 lines not on this page · raw message

block/qcow2-cluster.c +29 -25

183 lines not on this page · raw message

block/qcow2-refcount.c +37 -30

182 lines not on this page · raw message

block/qcow2.c +12 -9
index 025dce2..35950fb 100644
@@ -430,8 +430,11 @@ static int qcow2_open(BlockDriverState *bs, int flags)
     }
 
     /* alloc L2 table/refcount block cache */
-    s->l2_table_cache = qcow2_cache_create(bs, L2_CACHE_SIZE);
-    s->refcount_block_cache = qcow2_cache_create(bs, REFCOUNT_CACHE_SIZE);
+    s->l2_table_cache = block_cache_create(bs, L2_CACHE_SIZE,
+                                           s->cluster_size, BLOCK_TABLE_L2);
+    s->refcount_block_cache = block_cache_create(bs, REFCOUNT_CACHE_SIZE,
+                                                 s->cluster_size,
+                                                 BLOCK_TABLE_REF);
 
     s->cluster_cache = g_malloc(s->cluster_size);
     /* one more sector for decompressed data alignment */
@@ -510,7 +513,7 @@ static int qcow2_open(BlockDriverState *bs, int flags)
     qcow2_refcount_close(bs);
     g_free(s->l1_table);
     if (s->l2_table_cache) {
-        qcow2_cache_destroy(bs, s->l2_table_cache);
+        block_cache_destroy(bs, s->l2_table_cache);
     }
     g_free(s->cluster_cache);
     qemu_vfree(s->cluster_data);
@@ -878,13 +881,13 @@ static void qcow2_close(BlockDriverState *bs)
     BDRVQcowState *s = bs->opaque;
     g_free(s->l1_table);
 
-    qcow2_cache_flush(bs, s->l2_table_cache);
-    qcow2_cache_flush(bs, s->refcount_block_cache);
+    block_cache_flush(bs, s->l2_table_cache);
+    block_cache_flush(bs, s->refcount_block_cache);
 
     qcow2_mark_clean(bs);
 
-    qcow2_cache_destroy(bs, s->l2_table_cache);
-    qcow2_cache_destroy(bs, s->refcount_block_cache);
+    block_cache_destroy(bs, s->l2_table_cache);
+    block_cache_destroy(bs, s->refcount_block_cache);
 
     g_free(s->unknown_header_fields);
     cleanup_unknown_header_ext(bs);
@@ -1553,14 +1556,14 @@ static coroutine_fn int qcow2_co_flush_to_os(BlockDriverState *bs)
     int ret;
 
     qemu_co_mutex_lock(&s->lock);
-    ret = qcow2_cache_flush(bs, s->l2_table_cache);
+    ret = block_cache_flush(bs, s->l2_table_cache);
     if (ret < 0) {
         qemu_co_mutex_unlock(&s->lock);
         return ret;
     }
 
     if (qcow2_need_accurate_refcounts(s)) {
-        ret = qcow2_cache_flush(bs, s->refcount_block_cache);
+        ret = block_cache_flush(bs, s->refcount_block_cache);
         if (ret < 0) {
             qemu_co_mutex_unlock(&s->lock);
             return ret;
block/qcow2.h +3 -21
index b4eb654..cb6fd7a 100644
@@ -27,6 +27,7 @@
 
 #include "aes.h"
 #include "qemu-coroutine.h"
+#include "block-cache.h"
 
 //#define DEBUG_ALLOC
 //#define DEBUG_ALLOC2
@@ -94,8 +95,6 @@ typedef struct QCowSnapshot {
     uint64_t vm_clock_nsec;
 } QCowSnapshot;
 
-struct Qcow2Cache;
-typedef struct Qcow2Cache Qcow2Cache;
 
 typedef struct Qcow2UnknownHeaderExtension {
     uint32_t magic;
@@ -146,8 +145,8 @@ typedef struct BDRVQcowState {
     uint64_t l1_table_offset;
     uint64_t *l1_table;
 
-    Qcow2Cache* l2_table_cache;
-    Qcow2Cache* refcount_block_cache;
+    BlockCache *l2_table_cache;
+    BlockCache *refcount_block_cache;
 
     uint8_t *cluster_cache;
     uint8_t *cluster_data;
@@ -316,21 +315,4 @@ int qcow2_snapshot_load_tmp(BlockDriverState *bs, const char *snapshot_name);
 
 void qcow2_free_snapshots(BlockDriverState *bs);
 int qcow2_read_snapshots(BlockDriverState *bs);
-
-/* qcow2-cache.c functions */
-Qcow2Cache *qcow2_cache_create(BlockDriverState *bs, int num_tables);
-int qcow2_cache_destroy(BlockDriverState* bs, Qcow2Cache *c);
-
-void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table);
-int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c);
-int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c,
-    Qcow2Cache *dependency);
-void qcow2_cache_depends_on_flush(Qcow2Cache *c);
-
-int qcow2_cache_get(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
-    void **table);
-int qcow2_cache_get_empty(BlockDriverState *bs, Qcow2Cache *c, uint64_t offset,
-    void **table);
-int qcow2_cache_put(BlockDriverState *bs, Qcow2Cache *c, void **table);
-
 #endif
trace-events +7 -6
index 42b66f1..df1f12f 100644
@@ -454,12 +454,13 @@ qcow2_l2_allocate_write_l2(void *bs, int l1_index) "bs %p l1_index %d"
 qcow2_l2_allocate_write_l1(void *bs, int l1_index) "bs %p l1_index %d"
 qcow2_l2_allocate_done(void *bs, int l1_index, int ret) "bs %p l1_index %d ret %d"
 
-qcow2_cache_get(void *co, int c, uint64_t offset, bool read_from_disk) "co %p is_l2_cache %d offset %" PRIx64 " read_from_disk %d"
-qcow2_cache_get_replace_entry(void *co, int c, int i) "co %p is_l2_cache %d index %d"
-qcow2_cache_get_read(void *co, int c, int i) "co %p is_l2_cache %d index %d"
-qcow2_cache_get_done(void *co, int c, int i) "co %p is_l2_cache %d index %d"
-qcow2_cache_flush(void *co, int c) "co %p is_l2_cache %d"
-qcow2_cache_entry_flush(void *co, int c, int i) "co %p is_l2_cache %d index %d"
+# block/block-cache.c
+block_cache_get(void *co, int c, uint64_t offset, bool read_from_disk) "co %p is_l2_cache %d offset %" PRIx64 " read_from_disk %d"
+block_cache_get_replace_entry(void *co, int c, int i) "co %p is_l2_cache %d index %d"
+block_cache_get_read(void *co, int c, int i) "co %p is_l2_cache %d index %d"
+block_cache_get_done(void *co, int c, int i) "co %p is_l2_cache %d index %d"
+block_cache_flush(void *co, int c) "co %p is_l2_cache %d"
+block_cache_entry_flush(void *co, int c, int i) "co %p is_l2_cache %d index %d"
 
 # block/qed-l2-cache.c
 qed_alloc_l2_cache_entry(void *l2_cache, void *entry) "l2_cache %p entry %p"
Signature · 1 line
1.7.1
#7 lore raw

Re: [Qemu-devel] [PATCH V13 4/6] rename qcow2-cache.c to block-cache.c

block/qcow2.h line 31

Quoted 10 lines · Dong Xu Wang
block/qcow2.h +1 -0
index b4eb654..cb6fd7a 100644
@@ -27,6 +27,7 @@
 
 #include "aes.h"
 #include "qemu-coroutine.h"
+#include "block-cache.h"
 

Since block-cache.h is being included from qcow2.h you can drop the block-cache.h includes you added to qcow2-cluster.c and qcow2-refcount.c.

Stefan

#8 lore raw

Re: [Qemu-devel] [PATCH V13 4/6] rename qcow2-cache.c to block-cache.c

Quoted 7 lines · Stefan Hajnoczi
Quoted 6 lines · Dong Xu Wang
block/qcow2.h +0 -0
index b4eb654..cb6fd7a 100644
@@ -27,6 +27,7 @@
 
Quoted 9 lines

block/qcow2.h line 31

Quoted 4 lines
block/qcow2.h +1 -0
 #include "aes.h"
 #include "qemu-coroutine.h"
+#include "block-cache.h"
 

Since block-cache.h is being included from qcow2.h you can drop the block-cache.h includes you added to qcow2-cluster.c and qcow2-refcount.c.

Okay, thank you Stefan.

Quoted 2 lines

Stefan

#9 lore raw

PATCH v13 5/6 add-cow file format core code.

In reply to Dong Xu Wang

add-cow file format core code. It use block-cache.c as cache code. It lacks of snapshot_blkdev support.

---
 block/Makefile.objs |    1 +
 block/add-cow.c     |  693 +++++++++++++++++++++++++++++++++++++++++++++++++++
 block/add-cow.h     |   85 +++++++
 block/block-cache.c |    4 +
 block/block-cache.h |    1 +
 block_int.h         |    2 +
 6 files changed, 786 insertions(+), 0 deletions(-)
 create mode 100644 block/add-cow.c
 create mode 100644 block/add-cow.h
block/Makefile.objs +1 -0
index f128b78..ed9222d 100644
@@ -1,5 +1,6 @@
 block-obj-y += raw.o cow.o qcow.o vdi.o vmdk.o cloop.o dmg.o bochs.o vpc.o vvfat.o
 block-obj-y += qcow2.o qcow2-refcount.o qcow2-cluster.o qcow2-snapshot.o
+block-obj-y += add-cow.o
 block-obj-y += block-cache.o
 block-obj-y += qed.o qed-gencb.o qed-l2-cache.o qed-table.o qed-cluster.o
 block-obj-y += qed-check.o
block/add-cow.c new file +693 -0

693 lines not on this page · raw message

block/add-cow.h new file +85 -0

85 lines not on this page · raw message

block/block-cache.c +4 -0
index bf5c57c..1a30462 100644
@@ -112,6 +112,8 @@ static int block_cache_entry_flush(BlockDriverState *bs, BlockCache *c, int i)
         BLKDBG_EVENT(bs->file, BLKDBG_REFBLOCK_UPDATE_PART);
     } else if (c->table_type == BLOCK_TABLE_L2) {
         BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE);
+    } else if (c->table_type == BLOCK_TABLE_BITMAP) {
+        BLKDBG_EVENT(bs->file, BLKDBG_COW_WRITE);
     }
 
     ret = bdrv_pwrite(bs->file, c->entries[i].offset,
@@ -245,6 +247,8 @@ static int block_cache_do_get(BlockDriverState *bs, BlockCache *c,
     if (read_from_disk) {
         if (c->table_type == BLOCK_TABLE_L2) {
             BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD);
+        } else if (c->table_type == BLOCK_TABLE_BITMAP) {
+            BLKDBG_EVENT(bs->file, BLKDBG_COW_READ);
         }
 
         ret = bdrv_pread(bs->file, offset, c->entries[i].table,
block/block-cache.h +1 -0
index 4efa06e..a3c4a1c 100644
@@ -37,6 +37,7 @@
 typedef enum {
     BLOCK_TABLE_REF,
     BLOCK_TABLE_L2,
+    BLOCK_TABLE_BITMAP,
 } BlockTableType;
 
 typedef struct BlockCachedTable {
block_int.h +2 -0
index a104e70..8a79045 100644
@@ -55,6 +55,8 @@
 #define BLOCK_OPT_SUBFMT            "subformat"
 #define BLOCK_OPT_COMPAT_LEVEL      "compat"
 #define BLOCK_OPT_LAZY_REFCOUNTS    "lazy_refcounts"
+#define BLOCK_OPT_IMAGE_FILE        "image_file"
+#define BLOCK_OPT_IMAGE_FMT         "image_format"
 
 typedef struct BdrvTrackedRequest BdrvTrackedRequest;
 
Signature · 1 line
1.7.1
#10 lore raw

Re: [Qemu-devel] [PATCH V13 5/6] add-cow file format core code.

Quoted 7 lines · Dong Xu Wang
block/add-cow.c new file +7 -0
+static void add_cow_header_cpu_to_le(const AddCowHeader *cpu, AddCowHeader *le)
+{
+    le->magic                       = cpu_to_le64(cpu->magic);
+    le->version                     = cpu_to_le32(cpu->version);
+
+    le->backing_filename_offset     = cpu_to_le32(cpu->backing_filename_offset);
+    le->backing_filename_size       = cpu_to_le32(cpu->backing_filename_size);

block/add-cow.c line 55

Quoted 10 lines
block/add-cow.c new file +10 -0
+
+    le->image_filename_offset       = cpu_to_le32(cpu->image_filename_offset);
+    le->image_filename_size         = cpu_to_le32(cpu->image_filename_size);
+
+    le->cluster_bits                = cpu_to_le32(cpu->cluster_bits);
+    le->features                    = cpu_to_le64(cpu->features);
+    le->optional_features           = cpu_to_le64(cpu->optional_features);
+    le->header_pages_size           = cpu_to_le32(cpu->header_pages_size);
+    memcpy(le->backing_fmt, cpu->backing_fmt, sizeof(cpu->backing_fmt));
+    memcpy(le->image_fmt, cpu->image_fmt, sizeof(cpu->image_fmt));

Minor style issue: sizeof(le->backing_fmt) is safer than sizeof(cpu->image_fmt) in case the types change or this code is copy-pasted elsewhere. Always use the size of the destination buffer.

block/add-cow.c line 61

Quoted 6 lines
block/add-cow.c new file +6 -0
+}
+
+static int add_cow_probe(const uint8_t *buf, int buf_size, const char *filename)
+{
+    const AddCowHeader *header = (const AddCowHeader *)buf;
+

In case .bdrv_probe() is exposed in a future stand-alone block libary like libqblock.so where we cannot make assumptions about buf_size:

if (buf_size < sizeof(*header)) {
    return 0;
}
Quoted 4 lines
block/add-cow.c new file +4 -0
+    ret = bdrv_file_open(&bs, filename, BDRV_O_RDWR);
+    if (ret < 0) {
+        return ret;
+    }

block/add-cow.c line 180

Quoted 10 lines
block/add-cow.c new file +10 -0
+    snprintf(header.backing_fmt, sizeof(header.backing_fmt),
+             "%s", backing_fmt ? backing_fmt : "");
+    snprintf(header.image_fmt, sizeof(header.image_fmt),
+             "%s", image_format ? image_format : "raw");
+    add_cow_header_cpu_to_le(&header, &le_header);
+    ret = bdrv_pwrite(bs, 0, &le_header, sizeof(le_header));
+    if (ret < 0) {
+        bdrv_delete(bs);
+        return ret;
+    }

Once...

block/add-cow.c line 180

Quoted 4 lines
block/add-cow.c new file +4 -0
+    if (ret < 0) {
+        bdrv_delete(bs);
+        return ret;
+    }

...twice. This can be dropped.

Quoted 6 lines
block/add-cow.c new file +6 -0
+
+    if (backing_filename) {
+        ret = bdrv_pwrite(bs, header.backing_filename_offset,
+                          backing_filename, header.backing_filename_size);
+        if (ret < 0) {
+            bdrv_delete(bs);

block/add-cow.c line 201

Quoted 10 lines
block/add-cow.c new file +10 -0
+            return ret;
+        }
+    }
+
+    ret = bdrv_pwrite(bs, header.image_filename_offset,
+                      image_filename, header.image_filename_size);
+    if (ret < 0) {
+        bdrv_delete(bs);
+        return ret;
+    }

I suggest writing the image filename before the backing filename so it's easier to implement .bdrv_change_backing_file() in the future.

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+    ret = bdrv_open(bs, filename, BDRV_O_RDWR | BDRV_O_NO_FLUSH, drv);

Forgot to bdrv_close(bs) before opening as add-cow.

block/add-cow.c line 258

Quoted 5 lines
block/add-cow.c new file +5 -0
+    if ((s->header.features & ADD_COW_F_ALL_ALLOCATED) == 0) {
+        ret = bdrv_read_string(bs->file, sizeof(s->header),
+                               sizeof(bs->backing_format) - 1,
+                               bs->backing_format,
+                               sizeof(bs->backing_format));

This looks wrong:

1. The header contains the backing format field, we've already read it. Now we just need to put a NUL-terminated string into bs->backing_format. No need for bdrv_read_string().

2. offset = sizeof(s->header) does not make sense because the backing_format field is part of the header.

3. n = sizeof(bs->backing_format) - 1 should be the size of the header backing_format field, not the destination buffer.

I'm wondering if I missed something or why add-cow files open successfully in your testing, because I think this line of code would cause it to use a junk bs->backing_format.

block/add-cow.c line 288

Quoted 2 lines
block/add-cow.c new file +2 -0
+    s->image_hd = bdrv_new("");
+    if (path_has_protocol(image_filename)) {

image_filename[] is uninitialized. Did you mean tmp_name?

block/add-cow.c line 295

Quoted 7 lines
block/add-cow.c new file +7 -0
+        pstrcpy(image_filename, sizeof(image_filename), tmp_name);
+    } else {
+        path_combine(image_filename, sizeof(image_filename),
+                     bs->filename, tmp_name);
+    }
+
+    ret = bdrv_open(s->image_hd, image_filename, flags, NULL);

What about header->image_format?

block/add-cow.c line 301

Quoted 6 lines
block/add-cow.c new file +6 -0
+    if (ret < 0) {
+        bdrv_delete(s->image_hd);
+        goto fail;
+    }
+
+    bs->total_sectors = bdrv_getlength(s->image_hd) >> 9;

/ BDRV_SECTOR_SIZE

block/add-cow.c line 303

Quoted 2 lines
block/add-cow.c new file +2 -0
+    s->cluster_size = 1 << s->header.cluster_bits;
+    sector_per_byte = SECTORS_PER_CLUSTER * 8;

SECTORS_PER_CLUSTER does not take s->cluster_size into account.

The add_cow_open() issues should have been visible during development/testing (backing_format, unitialized image_filename[], unused header->image_format, SECTORS_PER_CLUSTER). It looks like not much testing of image creation options has been done. I'll review more of this series in the next version, please test more.

Stefan

#11 lore raw

PATCH v13 6/6 qemu-iotests: add add-cow iotests support.

In reply to Dong Xu Wang

This patch will use qemu-iotests to test add-cow file format.

---
 tests/qemu-iotests/017       |    2 +-
 tests/qemu-iotests/020       |    2 +-
 tests/qemu-iotests/common    |    6 ++++++
 tests/qemu-iotests/common.rc |   15 ++++++++++++++-
 4 files changed, 22 insertions(+), 3 deletions(-)
tests/qemu-iotests/017 +1 -1
index 66951eb..d31432f 100755
@@ -40,7 +40,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15
 . ./common.pattern
 
 # Any format supporting backing files
-_supported_fmt qcow qcow2 vmdk qed
+_supported_fmt qcow qcow2 vmdk qed add-cow
 _supported_proto generic
 _supported_os Linux
 
tests/qemu-iotests/020 +1 -1
index 2fb0ff8..3dbb495 100755
@@ -42,7 +42,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15
 . ./common.pattern
 
 # Any format supporting backing files
-_supported_fmt qcow qcow2 vmdk qed
+_supported_fmt qcow qcow2 vmdk qed add-cow
 _supported_proto generic
 _supported_os Linux
 
tests/qemu-iotests/common +6 -0
index 1f6fdf5..4c06895 100644
@@ -128,6 +128,7 @@ common options
 check options
     -raw                test raw (default)
     -cow                test cow
+    -add-cow            test add-cow
     -qcow               test qcow
     -qcow2              test qcow2
     -qed                test qed
@@ -163,6 +164,11 @@ testlist options
 	    xpand=false
 	    ;;
 
+	-add-cow)
+        IMGFMT=add-cow
+        xpand=false
+        ;;
+
 	-qcow)
 	    IMGFMT=qcow
 	    xpand=false
tests/qemu-iotests/common.rc +14 -1
index d534e94..f48b02a 100644
@@ -97,6 +97,16 @@ _make_test_img()
     fi
     if [ \( "$IMGFMT" = "qcow2" -o "$IMGFMT" = "qed" \) -a -n "$CLUSTER_SIZE" ]; then
         optstr=$(_optstr_add "$optstr" "cluster_size=$CLUSTER_SIZE")
+    elif [ "$IMGFMT" = "add-cow" ]; then
+        local IMG="$TEST_IMG"".raw"
+        if [ "$1" = "-b" ]; then
+            IMG="$IMG"".b"
+            $QEMU_IMG create -f raw $IMG $image_size>/dev/null
+            extra_img_options="-o image_file=$IMG $extra_img_options"
+        else
+            $QEMU_IMG create -f raw $IMG $image_size>/dev/null
+            extra_img_options="-o image_file=$IMG"
+        fi
     fi
 
     if [ -n "$optstr" ]; then
@@ -114,7 +124,8 @@ _make_test_img()
             -e "s# compat='[^']*'##g" \
             -e "s# compat6=\\(on\\|off\\)##g" \
             -e "s# static=\\(on\\|off\\)##g" \
-            -e "s# lazy_refcounts=\\(on\\|off\\)##g"
+            -e "s# lazy_refcounts=\\(on\\|off\\)##g" \
+            -e "s# image_file='[^']*'##g"
 }
 
 _cleanup_test_img()
@@ -125,6 +136,8 @@ _cleanup_test_img()
             rm -f $TEST_DIR/t.$IMGFMT
             rm -f $TEST_DIR/t.$IMGFMT.orig
             rm -f $TEST_DIR/t.$IMGFMT.base
+            rm -f $TEST_DIR/t.$IMGFMT.raw
+            rm -f $TEST_DIR/t.$IMGFMT.raw.b
             ;;
 
         rbd)
Signature · 1 line
1.7.1