Skip to contentSkip to conversation

add-cow file format

PATCH Superseded Dong Xu Wang 2 participants 17 messages Dec 6, 2012 – Dec 11, 2012 Block layer

v17 of 20 Superseded Latest is v20 →

lore
Share & tools
lore
#0 lore raw

v17 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/204147/

v16->v17):
1) Use stringify.

v15->v16):
1) Rebased on QEMU upstream source tree.
2) Judge if opts is null in add_cow_create function.

v14->v15:
1) Fix typo and make some sentences more readable in docs.
2) Introduce STRINGIZER macro.

v13->v14:
1) Make some sentences more clear in docs.
2) Make MAGIC from 8 bytes to 4 bytes.
3) Fix some bugs.

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              |  690 ++++++++++++++++++++++++++++++++++++++++++
 block/add-cow.h              |   83 +++++
 block/block-cache.c          |  321 ++++++++++++++++++++
 block/block-cache.h          |   77 +++++
 block/qcow2-cache.c          |  323 --------------------
 block/qcow2-cluster.c        |   53 ++--
 block/qcow2-refcount.c       |   66 +++--
 block/qcow2.c                |   21 +-
 block/qcow2.h                |   24 +--
 block/qed.c                  |   34 +--
 block_int.h                  |    2 +
 docs/specs/add-cow.txt       |  154 ++++++++++
 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, 1473 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 v17 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 |  154 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 154 insertions(+), 0 deletions(-)
 create mode 100644 docs/specs/add-cow.txt
docs/specs/add-cow.txt new file +154 -0
new file mode 100644
index 0000000..24e9a11
@@ -0,0 +1,154 @@
+== 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 a 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 an 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.
+
+image_fmt can be omitted, in that case image_fmt should be set as "raw".
+backing_fmt can also be omitted, add-cow should do a probe operation and determine
+what the backing file's format is.
+
+=Specification=
+
+The file format looks like this:
+
+ +---------------+-------------------------------+
+ |     Header    |           COW bitmap          |
+ +---------------+-------------------------------+
+
+All numbers in add-cow are stored in Little Endian byte order.
+
+== Header ==
+
+The Header is included in the first bytes:
+(HEADER_SIZE is defined in 44-47 bytes.)
+    Byte    0  -  3:    magic
+                        add-cow magic string ("ACOW").
+
+            4  -  7:    version
+                        Version number (only valid value is 1 now).
+
+            8  - 11:    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).
+
+            12 - 15:    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. Backing file name offset + size
+                        must be no more than HEADER_SIZE.
+
+            16 - 19:    image file name offset
+                        Offset in the add-cow file at which the image file name
+                        is stored (NB: The string is not NUL-terminated). It
+                        must be between 80 and [HEADER_SIZE - 2]. Image file
+                        name size + offset must be no more than HEADER_SIZE.
+
+            20 - 23:    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.
+
+            24 - 27:    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.
+
+            28 - 35:    features
+                        Bitmask of features. If a feature bit is set but not recognized,
+                        the add-cow file should be dropped. They are not used in v1.
+
+                        Bits 0-63:  Reserved (set to 0)
+
+            36 - 43:    compatible features
+                        Bitmask of compatible 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)
+
+            44 - 47:    HEADER_SIZE
+                        The header field is variable-sized. This field indicates
+                        how many bytes will be used to store add-cow header.
+                        In add-cow v1, it is fixed to 4096.
+
+            48 - 63:    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.
+
+            64 - 79:    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.
+
+            80 - [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 are 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.  It is tracking whether the sector in image file
+is allocated 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
+image file 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.
+
+The size of bitmap is calculated according to virtual size of image file, and
+the size of bitmap should be multiple of add-cow file's cluster size, 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, it indicates the sector has not been allocated in image file,
+data should be loaded from backing file while reading; if the bit is 1, it
+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 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.
+
+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 via qemu-img. If image file name and backing file
+name are the same, the add-cow image must be treated as invalid.
Signature · 1 line
1.7.1
#2 lore raw

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

Am 06.12.2012 07:51, schrieb Dong Xu Wang:

Quoted 14 lines

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

---
 docs/specs/add-cow.txt |  154 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 154 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..24e9a11

docs/specs/add-cow.txt line 9

Quoted 9 lines
docs/specs/add-cow.txt new file +9 -0
@@ -0,0 +1,154 @@
+== 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 a 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::

Double colon.

docs/specs/add-cow.txt line 22

Quoted 13 lines
docs/specs/add-cow.txt new file +13 -0
+(ubuntu.img is a disk image which has an 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.
+
+image_fmt can be omitted, in that case image_fmt should be set as "raw".

By "should be set as" you mean "is assumed to be"?

docs/specs/add-cow.txt line 23

Quoted 1 line
docs/specs/add-cow.txt new file +1 -0
+backing_fmt can also be omitted, add-cow should do a probe operation and determine

This line takes more than 80 characters. More follow, I won't comment on each.

Quoted 50 lines
docs/specs/add-cow.txt new file +50 -0
+what the backing file's format is.
+
+=Specification=
+
+The file format looks like this:
+
+ +---------------+-------------------------------+
+ |     Header    |           COW bitmap          |
+ +---------------+-------------------------------+
+
+All numbers in add-cow are stored in Little Endian byte order.
+
+== Header ==
+
+The Header is included in the first bytes:
+(HEADER_SIZE is defined in 44-47 bytes.)
+    Byte    0  -  3:    magic
+                        add-cow magic string ("ACOW").
+
+            4  -  7:    version
+                        Version number (only valid value is 1 now).
+
+            8  - 11:    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).
+
+            12 - 15:    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. Backing file name offset + size
+                        must be no more than HEADER_SIZE.
+
+            16 - 19:    image file name offset
+                        Offset in the add-cow file at which the image file name
+                        is stored (NB: The string is not NUL-terminated). It
+                        must be between 80 and [HEADER_SIZE - 2]. Image file
+                        name size + offset must be no more than HEADER_SIZE.
+
+            20 - 23:    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.
+
+            24 - 27:    cluster bits
+                        Number of bits that are used for addressing an offset

docs/specs/add-cow.txt line 83

Quoted 10 lines
docs/specs/add-cow.txt new file +10 -0
+                        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.
+
+            28 - 35:    features
+                        Bitmask of features. If a feature bit is set but not recognized,
+                        the add-cow file should be dropped. They are not used in v1.

Does v1 mean header.version = 1? I think this is wrong, we will want to add incompatible feature flags without increasing header.version (that's the whole point of them)

Quoted 6 lines
docs/specs/add-cow.txt new file +6 -0
+
+                        Bits 0-63:  Reserved (set to 0)
+
+            36 - 43:    compatible features
+                        Bitmask of compatible features. An implementation can
+                        safely ignore any unknown bits that are set.

docs/specs/add-cow.txt line 99

Quoted 10 lines
docs/specs/add-cow.txt new file +10 -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.
+
+                        Bits 1-63:  Reserved (set to 0)
+
+            44 - 47:    HEADER_SIZE
+                        The header field is variable-sized. This field indicates
+                        how many bytes will be used to store add-cow header.
+                        In add-cow v1, it is fixed to 4096.

Same question about v1. If it's fixed, why have a field for it?

Quoted 17 lines
docs/specs/add-cow.txt new file +17 -0
+
+            48 - 63:    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.
+
+            64 - 79:    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.
+
+            80 - [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 are not pointing to

docs/specs/add-cow.txt line 126

Quoted 10 lines
docs/specs/add-cow.txt new file +10 -0
+                        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.  It is tracking whether the sector in image file
+is allocated 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

clusters bit_s_

Quoted 4 lines
docs/specs/add-cow.txt new file +4 -0
+image file 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.

docs/specs/add-cow.txt line 140

Quoted 10 lines
docs/specs/add-cow.txt new file +10 -0
+
+The size of bitmap is calculated according to virtual size of image file, and
+the size of bitmap should be multiple of add-cow file's cluster size, 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, it indicates the sector has not been allocated in image file,

s/sector/cluster/

More instances follow, not commenting on each.

docs/specs/add-cow.txt line 150

Quoted 10 lines
docs/specs/add-cow.txt new file +10 -0
+data should be loaded from backing file while reading; if the bit is 1, it
+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 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.
+
+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.

Don't refer to a "raw image", it could be any image format.

docs/specs/add-cow.txt line 154

Quoted 4 lines
docs/specs/add-cow.txt new file +4 -0
+
+Image file name and backing file name must NOT be the same, we prevent this
+while creating add-cow files via qemu-img. If image file name and backing file
+name are the same, the add-cow image must be treated as invalid.

Kevin

#3 lore raw

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

Quoted 18 lines · Kevin Wolf

Am 06.12.2012 07:51, schrieb Dong Xu Wang:

Quoted 17 lines

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

---
 docs/specs/add-cow.txt |  154 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 154 insertions(+), 0 deletions(-)
 create mode 100644 docs/specs/add-cow.txt
docs/specs/add-cow.txt new file +3 -0
new file mode 100644
index 0000000..24e9a11
@@ -0,0 +1,154 @@
+== General ==
+
+The raw file format does not support backing files or copy on write feature.
Quoted 8 lines

docs/specs/add-cow.txt line 9

Quoted 6 lines
docs/specs/add-cow.txt new file +6 -0
+The add-cow image format makes it possible to use backing files with a 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::

Double colon.

Okay.

Quoted 17 lines
Quoted 4 lines
docs/specs/add-cow.txt new file +3 -0
+(ubuntu.img is a disk image which has an installed OS.)
+    1)  Create a raw image with the same size of ubuntu.img
+            qemu-img create -f raw test.raw 8G

docs/specs/add-cow.txt line 22

Quoted 10 lines
docs/specs/add-cow.txt new file +10 -0
+    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.
+
+image_fmt can be omitted, in that case image_fmt should be set as "raw".

By "should be set as" you mean "is assumed to be"?

Okay. Will fix.

Quoted 5 lines

docs/specs/add-cow.txt line 23

Quoted 1 line
docs/specs/add-cow.txt new file +1 -0
+backing_fmt can also be omitted, add-cow should do a probe operation and determine

This line takes more than 80 characters. More follow, I won't comment on each.

Okay, will fix.

Quoted 56 lines
Quoted 56 lines
docs/specs/add-cow.txt new file +56 -0
+what the backing file's format is.
+
+=Specification=
+
+The file format looks like this:
+
+ +---------------+-------------------------------+
+ |     Header    |           COW bitmap          |
+ +---------------+-------------------------------+
+
+All numbers in add-cow are stored in Little Endian byte order.
+
+== Header ==
+
+The Header is included in the first bytes:
+(HEADER_SIZE is defined in 44-47 bytes.)
+    Byte    0  -  3:    magic
+                        add-cow magic string ("ACOW").
+
+            4  -  7:    version
+                        Version number (only valid value is 1 now).
+
+            8  - 11:    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).
+
+            12 - 15:    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. Backing file name offset + size
+                        must be no more than HEADER_SIZE.
+
+            16 - 19:    image file name offset
+                        Offset in the add-cow file at which the image file name
+                        is stored (NB: The string is not NUL-terminated). It
+                        must be between 80 and [HEADER_SIZE - 2]. Image file
+                        name size + offset must be no more than HEADER_SIZE.
+
+            20 - 23:    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.
+
+            24 - 27:    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 8 lines

docs/specs/add-cow.txt line 83

Quoted 4 lines
docs/specs/add-cow.txt new file +4 -0
+
+            28 - 35:    features
+                        Bitmask of features. If a feature bit is set but not recognized,
+                        the add-cow file should be dropped. They are not used in v1.

Does v1 mean header.version = 1? I think this is wrong, we will want to add incompatible feature flags without increasing header.version (that's the whole point of them)

Okay, will fix.

Quoted 11 lines
Quoted 11 lines
docs/specs/add-cow.txt new file +10 -0
+
+                        Bits 0-63:  Reserved (set to 0)
+
+            36 - 43:    compatible features
+                        Bitmask of compatible 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.
+
Quoted 8 lines

docs/specs/add-cow.txt line 99

Quoted 6 lines
docs/specs/add-cow.txt new file +6 -0
+                        Bits 1-63:  Reserved (set to 0)
+
+            44 - 47:    HEADER_SIZE
+                        The header field is variable-sized. This field indicates
+                        how many bytes will be used to store add-cow header.
+                        In add-cow v1, it is fixed to 4096.

Same question about v1. If it's fixed, why have a field for it?

Okay, will make more clear in next version.

Quoted 22 lines
Quoted 22 lines
docs/specs/add-cow.txt new file +21 -0
+
+            48 - 63:    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.
+
+            64 - 79:    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.
+
+            80 - [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 are not pointing to
+                        backing file and image file names must be set to 0.
+
+== COW bitmap ==
+
Quoted 9 lines

docs/specs/add-cow.txt line 126

Quoted 6 lines
docs/specs/add-cow.txt new file +6 -0
+The "COW bitmap" field starts at offset HEADER_SIZE, stores a bitmap related to
+backing file and image file.  It is tracking whether the sector in image file
+is allocated 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

clusters bit_s_

Okay. "cluster_bits is".

Quoted 17 lines
Quoted 4 lines
docs/specs/add-cow.txt new file +4 -0
+image file 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.

docs/specs/add-cow.txt line 140

Quoted 10 lines
docs/specs/add-cow.txt new file +10 -0
+
+The size of bitmap is calculated according to virtual size of image file, and
+the size of bitmap should be multiple of add-cow file's cluster size, 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, it indicates the sector has not been allocated in image file,

s/sector/cluster/

Quoted 2 lines

More instances follow, not commenting on each.

Okay.

Quoted 4 lines
Quoted 4 lines
docs/specs/add-cow.txt new file +4 -0
+data should be loaded from backing file while reading; if the bit is 1, it
+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 there is no backing file, or if the image file is larger than the backing
Quoted 9 lines

docs/specs/add-cow.txt line 150

Quoted 6 lines
docs/specs/add-cow.txt new file +6 -0
+file and the offset is beyond the end of the backing file, then the data should
+be read as all zero bytes instead.
+
+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.

Don't refer to a "raw image", it could be any image format.

Okay, will fix.

Quoted 7 lines

docs/specs/add-cow.txt line 154

Quoted 4 lines
docs/specs/add-cow.txt new file +4 -0
+
+Image file name and backing file name must NOT be the same, we prevent this
+while creating add-cow files via qemu-img. If image file name and backing file
+name are the same, the add-cow image must be treated as invalid.

Kevin

#4 lore raw

PATCH v17 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 e972a05..2b0329b 100644
@@ -199,7 +199,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 af3bdde..1757528 100644
@@ -333,6 +333,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 v17 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 2b0329b..67d050f 100644
@@ -216,6 +216,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 1757528..53848b7 100644
@@ -170,6 +170,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 6d86954..3cef645 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 v17 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  |   53 ++++----
 block/qcow2-refcount.c |   66 ++++++-----
 block/qcow2.c          |   21 ++--
 block/qcow2.h          |   24 +---
 trace-events           |   13 +-
 9 files changed, 481 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 7f01510..d23c250 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 blkdebug.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 +28 -25

176 lines not on this page · raw message

block/qcow2-refcount.c +36 -30

175 lines not on this page · raw message

block/qcow2.c +12 -9
index 38f467c..6a66e9b 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);
@@ -1554,14 +1557,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 6c6cbf1..be611ba 100644
@@ -470,12 +470,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 V17 4/6] rename qcow2-cache.c to block-cache.c

Am 06.12.2012 07:51, schrieb Dong Xu Wang:

Quoted 69 lines

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  |   53 ++++----
 block/qcow2-refcount.c |   66 ++++++-----
 block/qcow2.c          |   21 ++--
 block/qcow2.h          |   24 +---
 trace-events           |   13 +-
 9 files changed, 481 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 7f01510..d23c250 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 blkdebug.o blkverify.o
block/block-cache.c new file +30 -0
new file mode 100644
index 0000000..bf5c57c
@@ -0,0 +1,317 @@
+/*
+ * QEMU Block Layer Cache
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Dong Xu Wang <[email protected]>
+ *
+ * This file is based on qcow2-cache.c, see its copyrights below:
+ *
+ * L2/refcount table cache for the QCOW2 format
+ *
+ * Copyright (c) 2010 Kevin Wolf <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

block/block-cache.c line 40

Quoted 10 lines
block/block-cache.c new file +10 -0
+ * THE SOFTWARE.
+ */
+
+#include "block_int.h"
+#include "qemu-common.h"
+#include "trace.h"
+#include "block-cache.h"
+
+BlockCache *block_cache_create(BlockDriverState *bs, int num_tables,
+                               size_t cluster_size, BlockTableType type)

cluster_size should probably be called table_size. It just happens that qcow2 tables are always one cluster, but it may be different for other formats using this implementation in the future.

Quoted 7 lines
block/block-cache.c new file +7 -0
+int block_cache_put(BlockDriverState *bs, BlockCache *c, void **table)
+{
+    int i;
+
+    for (i = 0; i < c->size; i++) {
+        if (c->entries[i].table == *table) {
+            goto found;

block/block-cache.c line 302

Quoted 10 lines
block/block-cache.c new file +10 -0
+        }
+    }
+    return -ENOENT;
+
+found:
+    c->entries[i].ref--;
+    assert(c->entries[i].ref >= 0);
+    *table = NULL;
+    return 0;
+}

Why did you swap the assert() and *table = NULL?

Quoted 36 lines
block/block-cache.h new file +30 -0
new file mode 100644
index 0000000..4efa06e
@@ -0,0 +1,76 @@
+/*
+ * QEMU Block Layer Cache
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Dong Xu Wang <[email protected]>
+ *
+ * This file is based on qcow2-cache.c, see its copyrights below:
+ *
+ * L2/refcount table cache for the QCOW2 format
+ *
+ * Copyright (c) 2010 Kevin Wolf <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

block/block-cache.h line 40

Quoted 10 lines
block/block-cache.h new file +10 -0
+ * THE SOFTWARE.
+ */
+
+#ifndef BLOCK_CACHE_H
+#define BLOCK_CACHE_H
+
+typedef enum {
+    BLOCK_TABLE_REF,
+    BLOCK_TABLE_L2,
+} BlockTableType;

I'm not excited about exposing these types, but it's okay for now. We can always change the implementation later to be nicer.

Quoted 7 lines
block/block-cache.h new file +7 -0
+
+typedef struct BlockCachedTable {
+    void    *table;
+    int64_t offset;
+    bool    dirty;
+    int     cache_hits;
+    int     ref;

block/block-cache.h line 57

Quoted 10 lines
block/block-cache.h new file +10 -0
+} BlockCachedTable;
+
+struct BlockCache {
+    BlockCachedTable    *entries;
+    struct BlockCache   *depends;
+    int                 size;
+    size_t              cluster_size;
+    BlockTableType      table_type;
+    bool                depends_on_flush;
+};

Why have these definitions been moved to the header file? They are supposed to be private to the implementation.

Quoted 8 lines
block/block-cache.h new file +8 -0
+
+struct BlockCache;
+typedef struct BlockCache BlockCache;
+
+BlockCache *block_cache_create(BlockDriverState *bs, int num_tables,
+                               size_t cluster_size, BlockTableType type);
+int block_cache_destroy(BlockDriverState *bs, BlockCache *c);
+int block_cache_flush(BlockDriverState *bs, BlockCache *c);

block/block-cache.h line 75

Quoted 10 lines
block/block-cache.h new file +10 -0
+int block_cache_set_dependency(BlockDriverState *bs,
+                               BlockCache *c,
+                               BlockCache *dependency);
+void block_cache_depends_on_flush(BlockCache *c);
+int block_cache_get(BlockDriverState *bs, BlockCache *c, uint64_t offset,
+                    void **table);
+int block_cache_get_empty(BlockDriverState *bs, BlockCache *c,
+                          uint64_t offset, void **table);
+int block_cache_put(BlockDriverState *bs, BlockCache *c, void **table);
+void block_cache_entry_mark_dirty(BlockCache *c, void *table);

Kevin

#8 lore raw

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

Quoted 76 lines · Kevin Wolf

Am 06.12.2012 07:51, schrieb Dong Xu Wang:

Quoted 75 lines

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  |   53 ++++----
 block/qcow2-refcount.c |   66 ++++++-----
 block/qcow2.c          |   21 ++--
 block/qcow2.h          |   24 +---
 trace-events           |   13 +-
 9 files changed, 481 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 7f01510..d23c250 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 blkdebug.o blkverify.o
block/block-cache.c new file +36 -0
new file mode 100644
index 0000000..bf5c57c
@@ -0,0 +1,317 @@
+/*
+ * QEMU Block Layer Cache
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Dong Xu Wang <[email protected]>
+ *
+ * This file is based on qcow2-cache.c, see its copyrights below:
+ *
+ * L2/refcount table cache for the QCOW2 format
+ *
+ * Copyright (c) 2010 Kevin Wolf <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "block_int.h"
+#include "qemu-common.h"
+#include "trace.h"
Quoted 9 lines

block/block-cache.c line 40

Quoted 4 lines
block/block-cache.c new file +4 -0
+#include "block-cache.h"
+
+BlockCache *block_cache_create(BlockDriverState *bs, int num_tables,
+                               size_t cluster_size, BlockTableType type)

cluster_size should probably be called table_size. It just happens that qcow2 tables are always one cluster, but it may be different for other formats using this implementation in the future.

Okay.

Quoted 11 lines
Quoted 11 lines
block/block-cache.c new file +11 -0
+int block_cache_put(BlockDriverState *bs, BlockCache *c, void **table)
+{
+    int i;
+
+    for (i = 0; i < c->size; i++) {
+        if (c->entries[i].table == *table) {
+            goto found;
+        }
+    }
+    return -ENOENT;
+
Quoted 9 lines

block/block-cache.c line 302

Quoted 6 lines
block/block-cache.c new file +6 -0
+found:
+    c->entries[i].ref--;
+    assert(c->entries[i].ref >= 0);
+    *table = NULL;
+    return 0;
+}

Why did you swap the assert() and *table = NULL?

I will use original code here. I have no reason to swap them..

Quoted 62 lines
Quoted 36 lines
block/block-cache.h new file +30 -0
new file mode 100644
index 0000000..4efa06e
@@ -0,0 +1,76 @@
+/*
+ * QEMU Block Layer Cache
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Dong Xu Wang <[email protected]>
+ *
+ * This file is based on qcow2-cache.c, see its copyrights below:
+ *
+ * L2/refcount table cache for the QCOW2 format
+ *
+ * Copyright (c) 2010 Kevin Wolf <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

block/block-cache.h line 40

Quoted 10 lines
block/block-cache.h new file +10 -0
+ * THE SOFTWARE.
+ */
+
+#ifndef BLOCK_CACHE_H
+#define BLOCK_CACHE_H
+
+typedef enum {
+    BLOCK_TABLE_REF,
+    BLOCK_TABLE_L2,
+} BlockTableType;

I'm not excited about exposing these types, but it's okay for now. We can always change the implementation later to be nicer.

Quoted 12 lines
block/block-cache.h new file +12 -0
+
+typedef struct BlockCachedTable {
+    void    *table;
+    int64_t offset;
+    bool    dirty;
+    int     cache_hits;
+    int     ref;
+} BlockCachedTable;
+
+struct BlockCache {
+    BlockCachedTable    *entries;
+    struct BlockCache   *depends;
Quoted 9 lines

block/block-cache.h line 57

Quoted 5 lines
block/block-cache.h new file +5 -0
+    int                 size;
+    size_t              cluster_size;
+    BlockTableType      table_type;
+    bool                depends_on_flush;
+};

Why have these definitions been moved to the header file? They are supposed to be private to the implementation.

Both qcow2.h:BDRVQcowState and add-cow.c: BDRVAddCowState have a member typed BlockCache,
such as:

typedef struct BDRVAddCowState {
    BlockDriverState    *image_hd;
    CoMutex             lock;
    int                 cluster_size;
    BlockCache          *bitmap_cache;
    uint64_t            bitmap_size;
    AddCowHeader        header;
    char                backing_fmt[16];
    char                image_fmt[16];
} BDRVAddCowState;

So I have to move the definitions to the header file, so that both add-cow.c and qcow2.h can use BlockCache.
Is it Okay?

Quoted 12 lines
Quoted 12 lines
block/block-cache.h new file +12 -0
+
+struct BlockCache;
+typedef struct BlockCache BlockCache;
+
+BlockCache *block_cache_create(BlockDriverState *bs, int num_tables,
+                               size_t cluster_size, BlockTableType type);
+int block_cache_destroy(BlockDriverState *bs, BlockCache *c);
+int block_cache_flush(BlockDriverState *bs, BlockCache *c);
+int block_cache_set_dependency(BlockDriverState *bs,
+                               BlockCache *c,
+                               BlockCache *dependency);
+void block_cache_depends_on_flush(BlockCache *c);
Quoted 9 lines

block/block-cache.h line 75

Quoted 6 lines
block/block-cache.h new file +6 -0
+int block_cache_get(BlockDriverState *bs, BlockCache *c, uint64_t offset,
+                    void **table);
+int block_cache_get_empty(BlockDriverState *bs, BlockCache *c,
+                          uint64_t offset, void **table);
+int block_cache_put(BlockDriverState *bs, BlockCache *c, void **table);
+void block_cache_entry_mark_dirty(BlockCache *c, void *table);

Kevin

Thank you Kevin.

#9 lore raw

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

Am 11.12.2012 09:25, schrieb Dong Xu Wang:

Quoted 94 lines · Kevin Wolf
Quoted 70 lines

Am 06.12.2012 07:51, schrieb Dong Xu Wang:

Quoted 43 lines

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/block-cache.h new file +30 -0
new file mode 100644
index 0000000..4efa06e
@@ -0,0 +1,76 @@
+/*
+ * QEMU Block Layer Cache
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Dong Xu Wang <[email protected]>
+ *
+ * This file is based on qcow2-cache.c, see its copyrights below:
+ *
+ * L2/refcount table cache for the QCOW2 format
+ *
+ * Copyright (c) 2010 Kevin Wolf <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

block/block-cache.h line 40

Quoted 10 lines
block/block-cache.h new file +10 -0
+ * THE SOFTWARE.
+ */
+
+#ifndef BLOCK_CACHE_H
+#define BLOCK_CACHE_H
+
+typedef enum {
+    BLOCK_TABLE_REF,
+    BLOCK_TABLE_L2,
+} BlockTableType;

I'm not excited about exposing these types, but it's okay for now. We can always change the implementation later to be nicer.

Quoted 12 lines
block/block-cache.h new file +12 -0
+
+typedef struct BlockCachedTable {
+    void    *table;
+    int64_t offset;
+    bool    dirty;
+    int     cache_hits;
+    int     ref;
+} BlockCachedTable;
+
+struct BlockCache {
+    BlockCachedTable    *entries;
+    struct BlockCache   *depends;
Quoted 9 lines

block/block-cache.h line 57

Quoted 5 lines
block/block-cache.h new file +5 -0
+    int                 size;
+    size_t              cluster_size;
+    BlockTableType      table_type;
+    bool                depends_on_flush;
+};

Why have these definitions been moved to the header file? They are supposed to be private to the implementation.

Both qcow2.h:BDRVQcowState and add-cow.c: BDRVAddCowState have a member typed BlockCache,
such as:

typedef struct BDRVAddCowState {
    BlockDriverState    *image_hd;
    CoMutex             lock;
    int                 cluster_size;
    BlockCache          *bitmap_cache;
    uint64_t            bitmap_size;
    AddCowHeader        header;
    char                backing_fmt[16];
    char                image_fmt[16];
} BDRVAddCowState;
Quoted 3 lines

So I have to move the definitions to the header file, so that both add-cow.c and qcow2.h can use BlockCache.
Is it Okay?

The fields are actually of the type BlockCache*, i.e. a pointer. They don't need the full definition of the struct, a forward declaration is sufficient. So it's enough to have these lines from qcow2.h in the header:

struct Qcow2Cache;
typedef struct Qcow2Cache Qcow2Cache;

Kevin

#10 lore raw

PATCH v17 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.

v16->v17:
1) Use stringify.

v15->v16:
1) Judge if opts is null in add_cow_create function.

---
 block/Makefile.objs |    1 +
 block/add-cow.c     |  690 +++++++++++++++++++++++++++++++++++++++++++++++++++
 block/add-cow.h     |   83 ++++++
 block/block-cache.c |    4 +
 block/block-cache.h |    1 +
 block_int.h         |    2 +
 6 files changed, 781 insertions(+), 0 deletions(-)
 create mode 100644 block/add-cow.c
 create mode 100644 block/add-cow.h
block/Makefile.objs +1 -0
index d23c250..f0ff067 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 +690 -0

690 lines not on this page · raw message

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

83 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 63f1a07..16355f3 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_fmt"
 
 typedef struct BdrvTrackedRequest BdrvTrackedRequest;
 
Signature · 1 line
1.7.1
#11 lore raw

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

Am 06.12.2012 07:51, schrieb Dong Xu Wang:

Quoted 45 lines

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

v16->v17:
1) Use stringify.

v15->v16:
1) Judge if opts is null in add_cow_create function.

---
 block/Makefile.objs |    1 +
 block/add-cow.c     |  690 +++++++++++++++++++++++++++++++++++++++++++++++++++
 block/add-cow.h     |   83 ++++++
 block/block-cache.c |    4 +
 block/block-cache.h |    1 +
 block_int.h         |    2 +
 6 files changed, 781 insertions(+), 0 deletions(-)
 create mode 100644 block/add-cow.c
 create mode 100644 block/add-cow.h
block/Makefile.objs +1 -0
index d23c250..f0ff067 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 +7 -0
new file mode 100644
index 0000000..8cd7305
@@ -0,0 +1,690 @@
+/*
+ * QEMU ADD-COW Disk Format
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Dong Xu Wang <[email protected]>

block/add-cow.c line 17

Quoted 10 lines
block/add-cow.c new file +10 -0
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
+ */
+
+#include "qemu-common.h"
+#include "block_int.h"
+#include "module.h"
+#include "add-cow.h"

You have only this source file, so there's no need to have a header.

block/add-cow.c line 285

Quoted 12 lines
block/add-cow.c new file +12 -0
+    s->image_hd = bdrv_new("");
+    if (path_has_protocol(tmp_name)) {
+        pstrcpy(image_filename, sizeof(image_filename), tmp_name);
+    } else {
+        path_combine(image_filename, sizeof(image_filename),
+                     bs->filename, tmp_name);
+    }
+    bdrv_get_full_backing_filename(bs, backing_filename,
+                                   sizeof(backing_filename));
+    if (!strcmp(backing_filename, image_filename)) {
+        fprintf(stderr, "Error: backing file and image file are same: %s\n",
+                backing_filename);

error_report().

Also, s->image_hd is leaked.

Quoted 12 lines
block/add-cow.c new file +12 -0
+        ret = EINVAL;
+        goto fail;
+    }
+    ret = bdrv_open(s->image_hd, image_filename, flags,
+                    bdrv_find_format(s->header.image_fmt));
+    if (ret < 0) {
+        bdrv_delete(s->image_hd);
+        goto fail;
+    }
+
+    bs->total_sectors = bdrv_getlength(s->image_hd) / BDRV_SECTOR_SIZE;
+    s->cluster_size = 1 << s->header.cluster_bits;

block/add-cow.c line 307

Quoted 10 lines
block/add-cow.c new file +10 -0
+    sector_per_byte = (s->cluster_size / BDRV_SECTOR_SIZE) * 8;
+    s->bitmap_size =
+        (bs->total_sectors + sector_per_byte - 1) / sector_per_byte;
+    s->bitmap_cache =  block_cache_create(bs, ADD_COW_CACHE_SIZE,
+                                          ADD_COW_CACHE_ENTRY_SIZE,
+                                          BLOCK_TABLE_BITMAP);
+    qemu_co_mutex_init(&s->lock);
+    return 0;
+fail:
+    if (s->bitmap_cache) {

This is never true.

block/add-cow.c line 311

Quoted 4 lines
block/add-cow.c new file +4 -0
+        block_cache_destroy(bs, s->bitmap_cache);
+    }
+    return ret;
+}

header.header_size isn't checked to be 4096 as required in the spec.

Quoted 5 lines
block/add-cow.c new file +5 -0
+
+static void add_cow_close(BlockDriverState *bs)
+{
+    BDRVAddCowState *s = bs->opaque;
+    if (s->bitmap_cache) {

block/add-cow.c line 326

Quoted 10 lines
block/add-cow.c new file +10 -0
+        block_cache_destroy(bs, s->bitmap_cache);
+    }
+    bdrv_delete(s->image_hd);
+}
+
+static bool is_allocated(BlockDriverState *bs, int64_t sector_num)
+{
+    BDRVAddCowState *s  = bs->opaque;
+    BlockCache *c = s->bitmap_cache;
+    int64_t cluster_num = sector_num / (s->cluster_size / BDRV_SECTOR_SIZE);

s->cluster_size / BDRV_SECTOR_SIZE is so common that it could be worth having another field s->cluster_secs like in qcow2.

block/add-cow.c line 333

Quoted 7 lines
block/add-cow.c new file +7 -0
+    uint8_t *table      = NULL;
+    bool val = false;
+    int ret;
+
+    uint64_t offset = s->header.header_size
+        + (offset_in_bitmap(sector_num, s->cluster_size / BDRV_SECTOR_SIZE)
+            & (~(c->cluster_size - 1)));

It becomes very obvious here that your choice of the image format layout means that all bitmap blocks are not aligned to cluster boundaries, but offset by header_size.

This isn't necessarily a big problem, but I'm not sure if it's a good idea either.

block/add-cow.c line 340

Quoted 7 lines
block/add-cow.c new file +7 -0
+    ret = block_cache_get(bs, s->bitmap_cache, offset, (void **)&table);
+    if (ret < 0) {
+        return ret;
+    }
+
+    val = table[cluster_num / 8 % ADD_COW_CACHE_ENTRY_SIZE]
+        & (1 << (cluster_num % 8));

Here you calculate the array index using ADD_COW_CACHE_ENTRY_SIZE, above you select the offset of the table based on the cluster size instead. One of both is wrong.

You should give add-cow some testing with non-standard cluster sizes.

Quoted 16 lines
block/add-cow.c new file +15 -0
+    ret = block_cache_put(bs, s->bitmap_cache, (void **)&table);
+    if (ret < 0) {
+        return ret;
+    }
+    return val;
+}
 
+static coroutine_fn int add_cow_co_writev(BlockDriverState *bs,
+                                          int64_t sector_num,
+                                          int remaining_sectors,
+                                          QEMUIOVector *qiov)
+{
+    BDRVAddCowState *s = bs->opaque;
+    BlockCache *c = s->bitmap_cache;
+    int ret = 0, i;
+    QEMUIOVector hd_qiov;

block/add-cow.c line 501

Quoted 10 lines
block/add-cow.c new file +10 -0
+    uint8_t *table;
+    uint64_t offset;
+    int mask = (s->cluster_size / BDRV_SECTOR_SIZE) - 1;
+    int cluster_mask = c->cluster_size - 1;
+    int64_t sector_per_cluster = s->cluster_size / BDRV_SECTOR_SIZE;
+
+    qemu_co_mutex_lock(&s->lock);
+    qemu_iovec_init(&hd_qiov, qiov->niov);
+    ret = bdrv_co_writev(s->image_hd, sector_num,
+                         remaining_sectors, qiov);

So you hold s->lock during all writes, not only during cluster allocation. I'm almost sure that this will hurt performance a lot.

Quoted 13 lines

index bf5c57c..1a30462 100644

block/block-cache.c +2 -0
@@ -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,

block/block-cache.c static int block_cache_do_get(BlockDriverState *bs, BlockCache *c, · line 252

Quoted 6 lines
block/block-cache.c +2 -0
@@ -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);
         }

I must admit that I don't like this table_type stuff at all, even more so if every new format adds new types to it. Not sure what to suggest here.

But anyway, even if we leave it, aren't BLKDBG_COW_READ/WRITE something completely different?

Kevin

#12 lore raw

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

Quoted 50 lines · Kevin Wolf

Am 06.12.2012 07:51, schrieb Dong Xu Wang:

Quoted 49 lines

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

v16->v17:
1) Use stringify.

v15->v16:
1) Judge if opts is null in add_cow_create function.

---
 block/Makefile.objs |    1 +
 block/add-cow.c     |  690 +++++++++++++++++++++++++++++++++++++++++++++++++++
 block/add-cow.h     |   83 ++++++
 block/block-cache.c |    4 +
 block/block-cache.h |    1 +
 block_int.h         |    2 +
 6 files changed, 781 insertions(+), 0 deletions(-)
 create mode 100644 block/add-cow.c
 create mode 100644 block/add-cow.h
block/Makefile.objs +1 -0
index d23c250..f0ff067 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 +11 -0
new file mode 100644
index 0000000..8cd7305
@@ -0,0 +1,690 @@
+/*
+ * QEMU ADD-COW Disk Format
+ *
+ * Copyright IBM, Corp. 2012
+ *
+ * Authors:
+ *  Dong Xu Wang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2 or later.
+ * See the COPYING.LIB file in the top-level directory.
+ *
Quoted 9 lines

block/add-cow.c line 17

Quoted 6 lines
block/add-cow.c new file +6 -0
+ */
+
+#include "qemu-common.h"
+#include "block_int.h"
+#include "module.h"
+#include "add-cow.h"

You have only this source file, so there's no need to have a header.

Okay.

Quoted 13 lines

block/add-cow.c line 285

Quoted 12 lines
block/add-cow.c new file +12 -0
+    s->image_hd = bdrv_new("");
+    if (path_has_protocol(tmp_name)) {
+        pstrcpy(image_filename, sizeof(image_filename), tmp_name);
+    } else {
+        path_combine(image_filename, sizeof(image_filename),
+                     bs->filename, tmp_name);
+    }
+    bdrv_get_full_backing_filename(bs, backing_filename,
+                                   sizeof(backing_filename));
+    if (!strcmp(backing_filename, image_filename)) {
+        fprintf(stderr, "Error: backing file and image file are same: %s\n",
+                backing_filename);
Quoted 4 lines

error_report().

Also, s->image_hd is leaked.

Okay.

Quoted 16 lines
Quoted 16 lines
block/add-cow.c new file +16 -0
+        ret = EINVAL;
+        goto fail;
+    }
+    ret = bdrv_open(s->image_hd, image_filename, flags,
+                    bdrv_find_format(s->header.image_fmt));
+    if (ret < 0) {
+        bdrv_delete(s->image_hd);
+        goto fail;
+    }
+
+    bs->total_sectors = bdrv_getlength(s->image_hd) / BDRV_SECTOR_SIZE;
+    s->cluster_size = 1 << s->header.cluster_bits;
+    sector_per_byte = (s->cluster_size / BDRV_SECTOR_SIZE) * 8;
+    s->bitmap_size =
+        (bs->total_sectors + sector_per_byte - 1) / sector_per_byte;
+    s->bitmap_cache =  block_cache_create(bs, ADD_COW_CACHE_SIZE,
Quoted 9 lines

block/add-cow.c line 307

Quoted 6 lines
block/add-cow.c new file +6 -0
+                                          ADD_COW_CACHE_ENTRY_SIZE,
+                                          BLOCK_TABLE_BITMAP);
+    qemu_co_mutex_init(&s->lock);
+    return 0;
+fail:
+    if (s->bitmap_cache) {

This is never true.

Okay.

Quoted 7 lines

block/add-cow.c line 311

Quoted 4 lines
block/add-cow.c new file +4 -0
+        block_cache_destroy(bs, s->bitmap_cache);
+    }
+    return ret;
+}

header.header_size isn't checked to be 4096 as required in the spec.

Okay, will check in newer version.

Quoted 10 lines
Quoted 10 lines
block/add-cow.c new file +10 -0
+
+static void add_cow_close(BlockDriverState *bs)
+{
+    BDRVAddCowState *s = bs->opaque;
+    if (s->bitmap_cache) {
+        block_cache_destroy(bs, s->bitmap_cache);
+    }
+    bdrv_delete(s->image_hd);
+}
+
Quoted 9 lines

block/add-cow.c line 326

Quoted 5 lines
block/add-cow.c new file +5 -0
+static bool is_allocated(BlockDriverState *bs, int64_t sector_num)
+{
+    BDRVAddCowState *s  = bs->opaque;
+    BlockCache *c = s->bitmap_cache;
+    int64_t cluster_num = sector_num / (s->cluster_size / BDRV_SECTOR_SIZE);

s->cluster_size / BDRV_SECTOR_SIZE is so common that it could be worth having another field s->cluster_secs like in qcow2.

Okay. will add cluster_secs field.

Quoted 50 lines

block/add-cow.c line 333

Quoted 7 lines
block/add-cow.c new file +7 -0
+    uint8_t *table      = NULL;
+    bool val = false;
+    int ret;
+
+    uint64_t offset = s->header.header_size
+        + (offset_in_bitmap(sector_num, s->cluster_size / BDRV_SECTOR_SIZE)
+            & (~(c->cluster_size - 1)));

It becomes very obvious here that your choice of the image format layout means that all bitmap blocks are not aligned to cluster boundaries, but offset by header_size.

This isn't necessarily a big problem, but I'm not sure if it's a good idea either.

block/add-cow.c line 340

Quoted 7 lines
block/add-cow.c new file +7 -0
+    ret = block_cache_get(bs, s->bitmap_cache, offset, (void **)&table);
+    if (ret < 0) {
+        return ret;
+    }
+
+    val = table[cluster_num / 8 % ADD_COW_CACHE_ENTRY_SIZE]
+        & (1 << (cluster_num % 8));

Here you calculate the array index using ADD_COW_CACHE_ENTRY_SIZE, above you select the offset of the table based on the cluster size instead. One of both is wrong.

You should give add-cow some testing with non-standard cluster sizes.

Quoted 21 lines
block/add-cow.c new file +20 -0
+    ret = block_cache_put(bs, s->bitmap_cache, (void **)&table);
+    if (ret < 0) {
+        return ret;
+    }
+    return val;
+}
 
+static coroutine_fn int add_cow_co_writev(BlockDriverState *bs,
+                                          int64_t sector_num,
+                                          int remaining_sectors,
+                                          QEMUIOVector *qiov)
+{
+    BDRVAddCowState *s = bs->opaque;
+    BlockCache *c = s->bitmap_cache;
+    int ret = 0, i;
+    QEMUIOVector hd_qiov;
+    uint8_t *table;
+    uint64_t offset;
+    int mask = (s->cluster_size / BDRV_SECTOR_SIZE) - 1;
+    int cluster_mask = c->cluster_size - 1;
+    int64_t sector_per_cluster = s->cluster_size / BDRV_SECTOR_SIZE;
Quoted 9 lines

block/add-cow.c line 501

Quoted 5 lines
block/add-cow.c new file +5 -0
+
+    qemu_co_mutex_lock(&s->lock);
+    qemu_iovec_init(&hd_qiov, qiov->niov);
+    ret = bdrv_co_writev(s->image_hd, sector_num,
+                         remaining_sectors, qiov);

So you hold s->lock during all writes, not only during cluster allocation. I'm almost sure that this will hurt performance a lot.

Okay, will optimize in next version.

Quoted 23 lines
Quoted 13 lines

index bf5c57c..1a30462 100644

block/block-cache.c +2 -0
@@ -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,

block/block-cache.c static int block_cache_do_get(BlockDriverState *bs, BlockCache *c, · line 252

Quoted 6 lines
block/block-cache.c +2 -0
@@ -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);
         }

I must admit that I don't like this table_type stuff at all, even more so if every new format adds new types to it. Not sure what to suggest here.

Quoted 2 lines

But anyway, even if we leave it, aren't BLKDBG_COW_READ/WRITE something completely different?

Sorry, I have read enum BlkDebugEvent to find out which type would be suitable for this case. In the previous
comments, you said existing DebugEvent types would be enough, do you think which types should be picked up
for add-cow BLKDBG_EVENT?

Thank you Kevin.

Quoted 3 lines

Kevin

#13 lore raw

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

Am 11.12.2012 09:11, schrieb Dong Xu Wang:

Quoted 23 lines
Quoted 23 lines
Quoted 13 lines

index bf5c57c..1a30462 100644

block/block-cache.c +2 -0
@@ -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,

block/block-cache.c static int block_cache_do_get(BlockDriverState *bs, BlockCache *c, · line 252

Quoted 6 lines
block/block-cache.c +2 -0
@@ -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);
         }

I must admit that I don't like this table_type stuff at all, even more so if every new format adds new types to it. Not sure what to suggest here.

Quoted 8 lines
Quoted 2 lines

But anyway, even if we leave it, aren't BLKDBG_COW_READ/WRITE something completely different?

Sorry, I have read enum BlkDebugEvent to find out which type would be suitable for this case. In the previous
comments, you said existing DebugEvent types would be enough, do you think which types should be picked up
for add-cow BLKDBG_EVENT?

Maybe BLKDBG_L2_LOAD/UPDATE?

Kevin

#14 lore raw

PATCH v17 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 b3aad89..1ea4a7f 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
@@ -164,6 +165,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 aef5f52..4461f69 100644
@@ -107,6 +107,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
@@ -124,7 +134,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"
 
     # Start an NBD server on the image file, which is what we'll be talking to
     if [ $IMGPROTO = "nbd" ]; then
@@ -146,6 +157,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
#15 lore raw

Re: [Qemu-devel] [PATCH V17 6/6] qemu-iotests: add add-cow iotests support.

Am 06.12.2012 07:51, schrieb Dong Xu Wang:

Quoted 9 lines

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(-)

I think at least tests 037 and 038 would work as well.

Kevin

#16 lore raw

Re: [Qemu-devel] [PATCH V17 6/6] qemu-iotests: add add-cow iotests support.

Quoted 11 lines · Kevin Wolf

Am 06.12.2012 07:51, schrieb Dong Xu Wang:

Quoted 9 lines

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(-)
Quoted 2 lines

I think at least tests 037 and 038 would work as well.

Okay.

Quoted 2 lines

Kevin