add-cow file format
v19 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/247508/
v18->v19:
1) support parallel aio write.
2) fix flush method.
3) other small fix.
v17 -> v18:
1) remove version field.
2) header size is maximum value and cluster size value.
3) fix type.
4) move struct to source file.
5) cluster_size->table_size.
6) use error_report, not fprintf.
7) remove version field from header.
8) header_size is MAX(cluster_size, 4096).
9) introduce s->cluster_sectors.
10) use BLKDBG_L2_LOAD/UPDATE.
11) add 037 and 038 tests.
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 (8):
V18: 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
Make block-cache.c be common interface
add debug event for add-cow
add-cow file format core code.
qemu-iotests: add add-cow iotests support
block.c | 29 +- block/Makefile.objs | 4 +- block/add-cow.c | 812 +++++++++++++++++++++++++++++++++++++++++++ block/blkdebug.c | 3 + block/block-cache.c | 342 ++++++++++++++++++ block/qcow2-cache.c | 323 ----------------- block/qcow2-cluster.c | 52 +-- block/qcow2-refcount.c | 62 ++-- block/qcow2.c | 21 +- block/qcow2.h | 24 +- block/qed.c | 34 +- docs/specs/add-cow.txt | 172 +++++++++ include/block/block-cache.h | 59 ++++ include/block/block.h | 6 + include/block/block_int.h | 2 + tests/qemu-iotests/017 | 2 +- tests/qemu-iotests/020 | 2 +- tests/qemu-iotests/037 | 2 +- tests/qemu-iotests/038 | 2 +- tests/qemu-iotests/common | 6 + tests/qemu-iotests/common.rc | 15 +- trace-events | 13 +- 22 files changed, 1536 insertions(+), 451 deletions(-) create mode 100644 block/add-cow.c create mode 100644 block/block-cache.c delete mode 100644 block/qcow2-cache.c create mode 100644 docs/specs/add-cow.txt create mode 100644 include/block/block-cache.h
Signature · 1 line
1.7.11.7
PATCH v19 1/8 V18: docs: document for add-cow file format
From: Dong Xu Wang <[email protected]>
Document for add-cow format, the usage and spec of add-cow are introduced.
v18-v19:
1) backing_fmt and image_fmt NUL-terminated.
2) other fix.V17->V18:
1) remove version field.
2) header size is maximum value and cluster size value.
3) fix type.
--- docs/specs/add-cow.txt | 172 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 docs/specs/add-cow.txt
docs/specs/add-cow.txt new file +172 -0
@@ -0,0 +1,172 @@ +== 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 an image by keeping a separate .add-cow metadata file. +Once all clusters have been written into the image it is safe to +discard the .add-cow and backing files, then we can use the image +directly. + +An example usage of add-cow would look like: +(ubuntu.img is a disk image which has an installed OS.) + 1) Create an image, such as raw format, 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 is assumed to be +"raw". backing_fmt can also be omitted, add-cow should do a probe +operation and determine what the backing file's format is. It is +recommended to always specify the format for any raw file, because +probing a raw file is a security hole. + +=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 40-43 bytes.) + Byte 0 - 3: magic + add-cow magic string ("ACOW"). It is coded in + free-form ASCII. + + 4 - 7: 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 76 and [HEADER_SIZE + - 2](a file name must be at least 1 byte). + + 8 - 11: 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 - 76] to fit in the reserved + part of the header. Backing file name offset + + size must be no more than HEADER_SIZE. + + 12 - 15: 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 76 and + [HEADER_SIZE - 2]. Image file name size + offset + must be no more than HEADER_SIZE. + + 16 - 19: image file name size + Length of the image file name in bytes. + Must be less than [HEADER_SIZE - 76] to fit in + the reserved part of the header. + + 20 - 23: 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 12 + (i.e. 4096 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. + + 24 - 31: features + Bitmask of features. If a feature bit is set + but not recognized, the opening add-cow file must + fail. No features bits are currently defined. + + Bits 0-63: Reserved (set to 0) + + 32 - 39: 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) + + 40 - 43: HEADER_SIZE + The header field is variable-sized. This field + indicates how many bytes will be used to store + add-cow header. By default, it is maximum value + of 4096 and cluster size value. + + 44 - 59: 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 NUL-terminated. Zero padded on the right. If + backing_fmt is omitted, must do a probe operation + and store the real format here. + + 60 - 75: image file format + Format of image file. It must be non-empty. It + is coded in free-form ASCII, and is + NUL-terminated. Zero padded on the right. If + image_fmt is omitted, "raw" will be stored + here. + + 76 - [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 +cluster 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 cluster 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 cluster has been dirty, should +be loaded from image file while reading. Writing to a cluster 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 image file is not an even multiple of cluster bytes, bits that +correspond to bytes beyond the image 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.11.7
Re: [Qemu-devel] [PATCH V19 1/8] V18: docs: document for add-cow file format
Quoted 13 lines · Dongxu Wang
From: Dong Xu Wang <[email protected]>
Document for add-cow format, the usage and spec of add-cow are introduced.
v18-v19:
1)backing_fmtandimage_fmtNUL-terminated.
2) other fix.V17->V18:
1) remove version field.
2) header size is maximum value and cluster size value.
3) fix type.
Quoted 2 lines
Signed-off-by Dong Xu Wang <[email protected]>Signed-off-by Dongxu Wang <[email protected]>
Changed git config? I suggest you could remove the old SOB line (if respin).
Re: [Qemu-devel] [PATCH V19 1/8] V18: docs: document for add-cow file format
Quoted 14 lines · Fam Zheng
Quoted 13 lines · Dongxu Wang
From: Dong Xu Wang <[email protected]>
Document for add-cow format, the usage and spec of add-cow are introduced.
v18-v19:
1)backing_fmtandimage_fmtNUL-terminated.
2) other fix.V17->V18:
1) remove version field.
2) header size is maximum value and cluster size value.
3) fix type.
Quoted 6 lines
Quoted 2 lines
Signed-off-by Dong Xu Wang <[email protected]>Signed-off-by Dongxu Wang <[email protected]>Changed git config? I suggest you could remove the old SOB line (if respin).
Oh, yes, I changed my gitconfig file, and I did not notice these wrong SOB line, please ignore these boring lines. :)
Quoted 1 line
PATCH v19 2/8 make path_has_protocol non static
In reply to Dongxu WangFrom: Dong Xu Wang <[email protected]>
We will use path_has_protocol outside block.c, so just make it public.
--- block.c | 2 +- include/block/block.h | 1 + 2 files changed, 2 insertions(+), 1 deletion(-)
block.c +1 -1
@@ -193,7 +193,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;
include/block/block.h +1 -0
@@ -345,6 +345,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.11.7
PATCH v19 3/8 qed_read_string to bdrv_read_string
In reply to Dongxu WangFrom: Dong Xu Wang <[email protected]>
Make qed_read_string function to a common interface, so move it to block.c.
--- block.c | 27 +++++++++++++++++++++++++++ block/qed.c | 34 ++++------------------------------ include/block/block.h | 2 ++ 3 files changed, 33 insertions(+), 30 deletions(-)
block.c +27 -0
@@ -210,6 +210,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/qed.c +4 -30
@@ -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, QDict *options, 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; }
include/block/block.h +2 -0
@@ -175,6 +175,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,
Signature · 1 line
1.7.11.7
PATCH v19 4/8 rename qcow2-cache.c to block-cache.c
In reply to Dongxu WangFrom: Dong Xu Wang <[email protected]>
Block layer will use qcow2-cache as common cache code, so rename it to block-cache.c.
v18->v19:
1) only rename, did not touch other code.
--- block/Makefile.objs | 3 +- block/block-cache.c | 323 +++++++++++++++++++++++++++++++++++++++++++++++++ block/qcow2-cache.c | 323 ------------------------------------------------- block/qcow2-cluster.c | 48 ++++---- block/qcow2-refcount.c | 42 +++---- block/qcow2.c | 18 +-- block/qcow2.h | 28 ++--- trace-events | 12 +- 8 files changed, 399 insertions(+), 398 deletions(-) create mode 100644 block/block-cache.c delete mode 100644 block/qcow2-cache.c
block/Makefile.objs +2 -1
@@ -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 += vhdx.o
block/block-cache.c new file +323 -0
323 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 +24 -24
180 lines not on this page · raw message
block/qcow2-refcount.c +21 -21
157 lines not on this page · raw message
block/qcow2.c +9 -9
@@ -454,8 +454,8 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, 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->refcount_block_cache =block_cache_create (bs,REFCOUNT_CACHE_SIZE ); s->cluster_cache =g_malloc (s->cluster_size);/* one more sector for decompressed data alignment */
@@ -556,7 +556,7 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, 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);
@@ -921,13 +921,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);
@@ -1632,14 +1632,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 +14 -14
@@ -97,8 +97,8 @@ typedef struct QCowSnapshot {uint64_t vm_clock_nsec; } QCowSnapshot; -struct Qcow2Cache ; -typedef struct Qcow2Cache Qcow2Cache ; +struct BlockCache ; +typedef struct BlockCache BlockCache ; typedef struct Qcow2UnknownHeaderExtension { uint32_t magic;
@@ -149,8 +149,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;
@@ -393,19 +393,19 @@ 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); +BlockCache *block_cache_create (BlockDriverState *bs,int num_tables); +int block_cache_destroy (BlockDriverState * bs,BlockCache *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); +void block_cache_entry_mark_dirty (BlockCache *c,void *table); +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); -int qcow2_cache_get (BlockDriverState *bs,Qcow2Cache *c,uint64_t offset, +int block_cache_get (BlockDriverState *bs,BlockCache *c,uint64_t offset,void **table); -int qcow2_cache_get_empty (BlockDriverState *bs,Qcow2Cache *c,uint64_t offset, +int block_cache_get_empty (BlockDriverState *bs,BlockCache *c,uint64_t offset,void **table); -int qcow2_cache_put (BlockDriverState *bs,Qcow2Cache *c,void **table); +int block_cache_put (BlockDriverState *bs,BlockCache *c,void **table);#endif
trace-events +6 -6
@@ -503,12 +503,12 @@ 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_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.11.7
PATCH v19 5/8 Make block-cache.c be common interface
In reply to Dongxu WangFrom: Dong Xu Wang <[email protected]>
To use block-cache.c be common cache interface, we need to add some parameter to identify cache's type.
Define a struct named BlockTableType, pass BlockTableType and table size parameters to block cache initialization function.
v17-v18:
1) move struct to source file.
2) cluster_size->table_size.
--- block/block-cache.c | 75 ++++++++++++++++++++++++++------------------- block/qcow2-cluster.c | 30 +++++++++--------- block/qcow2-refcount.c | 30 +++++++++--------- block/qcow2.c | 7 +++-- block/qcow2.h | 24 ++------------- include/block/block-cache.h | 55 +++++++++++++++++++++++++++++++++ trace-events | 1 + 7 files changed, 139 insertions(+), 83 deletions(-) create mode 100644 include/block/block-cache.h
block/block-cache.c +43 -32
@@ -1,4 +1,8 @@/* +* QEMU Block Layer Cache +* +* 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]>
@@ -24,11 +28,11 @@#include "block/block_int.h" #include "qemu-common.h" -#include "qcow2.h" #include "trace.h" +#include "block/block-cache.h" typedef struct BlockCachedTable { - void* table; + void *table; int64_t offset; bool dirty; int cache_hits;
@@ -36,30 +40,34 @@ typedef struct BlockCachedTable { } BlockCachedTable;struct BlockCache { -BlockCachedTable * entries; -struct BlockCache * depends; -int size; -bool depends_on_flush; +BlockCachedTable *entries; +struct BlockCache *depends; +int size; +size_t table_size; +BlockTableType table_type; +bool depends_on_flush; }; -BlockCache *block_cache_create (BlockDriverState *bs,int num_tables) +BlockCache *block_cache_create (BlockDriverState *bs,int num_tables, +size_t table_size,BlockTableType type) { -BDRVQcowState *s = bs->opaque;BlockCache *c;int i; c =g_malloc0 (sizeof (*c)); c->size = num_tables; c->entries =g_malloc0 (sizeof (*c->entries) * num_tables); + c->table_type = type; + c->table_size = table_size;for (i =0 ; i < c->size; i++) { - c->entries[i].table =qemu_blockalign (bs, s->cluster_size); + c->entries[i].table =qemu_blockalign (bs, table_size); }return c; } -int block_cache_destroy (BlockDriverState * bs,BlockCache *c) +int block_cache_destroy (BlockDriverState *bs,BlockCache *c) {int i;
@@ -91,15 +99,13 @@ static int block_cache_flush_dependency(BlockDriverState *bs, BlockCache *c)static int block_cache_entry_flush (BlockDriverState *bs,BlockCache *c,int i) { -BDRVQcowState *s = bs->opaque;int ret =0 ;if (!c->entries[i].dirty || !c->entries[i].offset) {return 0 ; } -trace_block_cache_entry_flush (qemu_coroutine_self (), - c == s->l2_table_cache, i); +trace_block_cache_entry_flush (qemu_coroutine_self (), c->table_type, i);if (c->depends) { ret =block_cache_flush_dependency (bs, c);
@@ -114,14 +120,16 @@ static int block_cache_entry_flush(BlockDriverState *bs, BlockCache *c, int i)return ret; } -if (c == s->refcount_block_cache) { +if (c->table_type ==BLOCK_TABLE_REF ) {BLKDBG_EVENT (bs->file,BLKDBG_REFBLOCK_UPDATE_PART ); - }else if (c == s->l2_table_cache) { + }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, c->entries[i].table, - s->cluster_size); + ret =bdrv_pwrite (bs->file, c->entries[i].offset, + c->entries[i].table, c->table_size);if (ret <0 ) {return ret; }
@@ -133,12 +141,11 @@ static int block_cache_entry_flush(BlockDriverState *bs, BlockCache *c, int i)int block_cache_flush (BlockDriverState *bs,BlockCache *c) { -BDRVQcowState *s = bs->opaque;int result =0 ;int ret;int i; -trace_block_cache_flush (qemu_coroutine_self (), c == s->l2_table_cache); +trace_block_cache_flush (qemu_coroutine_self (), c->table_type);for (i =0 ; i < c->size; i++) { ret =block_cache_entry_flush (bs, c, i);
@@ -157,8 +164,9 @@ int block_cache_flush(BlockDriverState *bs, BlockCache *c)return result; } -int block_cache_set_dependency (BlockDriverState *bs,BlockCache *c, -BlockCache *dependency) +int block_cache_set_dependency (BlockDriverState *bs, +BlockCache *c, +BlockCache *dependency) {int ret;
@@ -216,13 +224,13 @@ static int block_cache_find_entry_to_replace(BlockCache *c) }static int block_cache_do_get (BlockDriverState *bs,BlockCache *c, -uint64_t offset,void **table,bool read_from_disk) +uint64_t offset,void **table, +bool read_from_disk) { -BDRVQcowState *s = bs->opaque;int i;int ret; -trace_block_cache_get (qemu_coroutine_self (), c == s->l2_table_cache, +trace_block_cache_get (qemu_coroutine_self (), c->table_type, offset, read_from_disk);/* Check if the table is already cached */
@@ -235,7 +243,7 @@ static int block_cache_do_get(BlockDriverState *bs, BlockCache *c,/* If not, write a table back and replace it */ i =block_cache_find_entry_to_replace (c);trace_block_cache_get_replace_entry (qemu_coroutine_self (), - c == s->l2_table_cache, i); + c->table_type, i);if (i <0 ) {return i; }
@@ -246,14 +254,17 @@ static int block_cache_do_get(BlockDriverState *bs, BlockCache *c, }trace_block_cache_get_read (qemu_coroutine_self (), - c == s->l2_table_cache, i); + c->table_type, i); c->entries[i].offset =0 ;if (read_from_disk) { -if (c == s->l2_table_cache) { +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, s->cluster_size); + ret =bdrv_pread (bs->file, offset, c->entries[i].table, + c->table_size);if (ret <0 ) {return ret; }
@@ -271,19 +282,19 @@ found: *table = c->entries[i].table;trace_block_cache_get_done (qemu_coroutine_self (), - c == s->l2_table_cache, i); + c->table_type, i);return 0 ; }int block_cache_get (BlockDriverState *bs,BlockCache *c,uint64_t offset, -void **table) +void **table) {return block_cache_do_get (bs, c, offset, table, true); } -int block_cache_get_empty (BlockDriverState *bs,BlockCache *c,uint64_t offset, -void **table) +int block_cache_get_empty (BlockDriverState *bs,BlockCache *c, +uint64_t offset,void **table) {return block_cache_do_get (bs, c, offset, table, false); }
block/qcow2-cluster.c +16 -14
@@ -125,7 +125,8 @@ static int l2_load(BlockDriverState *bs, uint64_t l2_offset,BDRVQcowState *s = bs->opaque;int ret; - ret =block_cache_get (bs, s->l2_table_cache, l2_offset, (void **) l2_table); + ret =block_cache_get (bs, s->l2_table_cache, l2_offset, + (void **) l2_table);return ret; }
@@ -194,7 +195,8 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)/* allocate a new entry in the l2 cache */ trace_qcow2_l2_allocate_get_empty (bs, l1_index); - ret =block_cache_get_empty (bs, s->l2_table_cache, l2_offset, (void **) table); + ret =block_cache_get_empty (bs, s->l2_table_cache, l2_offset, + (void **) table);if (ret <0 ) {return ret; }
@@ -211,14 +213,14 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)BLKDBG_EVENT (bs->file,BLKDBG_L2_ALLOC_COW_READ ); ret =block_cache_get (bs, s->l2_table_cache, old_l2_offset &L1E_OFFSET_MASK , - (void **) &old_table); + (void **) &old_table);if (ret <0 ) { goto fail; }memcpy (l2_table, old_table, s->cluster_size); - ret =block_cache_put (bs, s->l2_table_cache, (void **) &old_table); + ret =block_cache_put (bs, s->l2_table_cache, (void **) &old_table);if (ret <0 ) { goto fail; }
@@ -248,7 +250,7 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table) fail:trace_qcow2_l2_allocate_done (bs, l1_index, ret); -block_cache_put (bs, s->l2_table_cache, (void **) table); +block_cache_put (bs, s->l2_table_cache, (void **) table); s->l1_table[l1_index] = old_l2_offset;return ret; }
@@ -484,7 +486,7 @@ int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,abort (); } -block_cache_put (bs, s->l2_table_cache, (void **) &l2_table); +block_cache_put (bs, s->l2_table_cache, (void **) &l2_table); nb_available = (c * s->cluster_sectors);
@@ -594,13 +596,13 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,* allocated. */ cluster_offset =be64_to_cpu (l2_table[l2_index]);if (cluster_offset &L2E_OFFSET_MASK ) { -block_cache_put (bs, s->l2_table_cache, (void **) &l2_table); +block_cache_put (bs, s->l2_table_cache, (void **) &l2_table);return 0 ; } cluster_offset =qcow2_alloc_bytes (bs, compressed_size);if (cluster_offset <0 ) { -block_cache_put (bs, s->l2_table_cache, (void **) &l2_table); +block_cache_put (bs, s->l2_table_cache, (void **) &l2_table);return 0 ; }
@@ -617,7 +619,7 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,BLKDBG_EVENT (bs->file,BLKDBG_L2_UPDATE_COMPRESSED );block_cache_entry_mark_dirty (s->l2_table_cache, l2_table); l2_table[l2_index] =cpu_to_be64 (cluster_offset); - ret =block_cache_put (bs, s->l2_table_cache, (void **) &l2_table); + ret =block_cache_put (bs, s->l2_table_cache, (void **) &l2_table);if (ret <0 ) {return 0 ; }
@@ -707,7 +709,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) } - ret =block_cache_put (bs, s->l2_table_cache, (void **) &l2_table); + ret =block_cache_put (bs, s->l2_table_cache, (void **) &l2_table);if (ret <0 ) { goto err; }
@@ -916,7 +918,7 @@ static int handle_copied(BlockDriverState *bs, uint64_t guest_offset,/* Cleanup */ out: - pret =block_cache_put (bs, s->l2_table_cache, (void **) &l2_table); + pret =block_cache_put (bs, s->l2_table_cache, (void **) &l2_table);if (pret <0 ) {return pret; }
@@ -1044,7 +1046,7 @@ static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset,* wrong with our code. */ assert (nb_clusters >0 ); - ret =block_cache_put (bs, s->l2_table_cache, (void **) &l2_table); + ret =block_cache_put (bs, s->l2_table_cache, (void **) &l2_table);if (ret <0 ) {return ret; }
@@ -1342,7 +1344,7 @@ static int discard_single_l2(BlockDriverState *bs, uint64_t offset,qcow2_free_any_clusters (bs, old_offset,1 ); } - ret =block_cache_put (bs, s->l2_table_cache, (void **) &l2_table); + ret =block_cache_put (bs, s->l2_table_cache, (void **) &l2_table);if (ret <0 ) {return ret; }
@@ -1421,7 +1423,7 @@ static int zero_single_l2(BlockDriverState *bs, uint64_t offset, } } - ret =block_cache_put (bs, s->l2_table_cache, (void **) &l2_table); + ret =block_cache_put (bs, s->l2_table_cache, (void **) &l2_table);if (ret <0 ) {return ret; }
block/qcow2-refcount.c +16 -14
@@ -99,7 +99,7 @@ static int get_refcount(BlockDriverState *bs, int64_t cluster_index)return 0 ; ret =block_cache_get (bs, s->refcount_block_cache, refcount_block_offset, - (void **) &refcount_block); + (void **) &refcount_block);if (ret <0 ) {return ret; }
@@ -109,7 +109,7 @@ static int get_refcount(BlockDriverState *bs, int64_t cluster_index) refcount =be16_to_cpu (refcount_block[block_index]); ret =block_cache_put (bs, s->refcount_block_cache, - (void **) &refcount_block); + (void **) &refcount_block);if (ret <0 ) {return ret; }
@@ -172,7 +172,7 @@ static int alloc_refcount_block(BlockDriverState *bs,/* If it's already there, we're done */ if (refcount_block_offset) {return load_refcount_block (bs, refcount_block_offset, - (void **) refcount_block); + (void **) refcount_block); } }
@@ -221,7 +221,7 @@ static int alloc_refcount_block(BlockDriverState *bs,if (in_same_refcount_block (s, new_block, cluster_index << s->cluster_bits)) {/* Zero the new refcount block before updating it */ ret =block_cache_get_empty (bs, s->refcount_block_cache, new_block, - (void **) refcount_block); + (void **) refcount_block);if (ret <0 ) { goto fail_block; }
@@ -248,7 +248,7 @@ static int alloc_refcount_block(BlockDriverState *bs,/* Initialize the new refcount block only after updating its refcount, * update_refcount uses the refcount cache itself */ ret =block_cache_get_empty (bs, s->refcount_block_cache, new_block, - (void **) refcount_block); + (void **) refcount_block);if (ret <0 ) { goto fail_block; }
@@ -279,7 +279,8 @@ static int alloc_refcount_block(BlockDriverState *bs,return 0 ; } - ret =block_cache_put (bs, s->refcount_block_cache, (void **) refcount_block); + ret =block_cache_put (bs, s->refcount_block_cache, + (void **) refcount_block);if (ret <0 ) { goto fail_block; }
@@ -402,7 +403,7 @@ static int alloc_refcount_block(BlockDriverState *bs,qcow2_free_clusters (bs, old_table_offset, old_table_size *sizeof (uint64_t )); s->free_cluster_index = old_free_cluster_index; - ret =load_refcount_block (bs, new_block, (void **) refcount_block); + ret =load_refcount_block (bs, new_block, (void **) refcount_block);if (ret <0 ) {return ret; }
@@ -413,7 +414,7 @@ fail_table:g_free (new_table); fail_block:if (*refcount_block !=NULL ) { -block_cache_put (bs, s->refcount_block_cache, (void **) refcount_block); +block_cache_put (bs, s->refcount_block_cache, (void **) refcount_block); }return ret; }
@@ -457,7 +458,7 @@ static int QEMU_WARN_UNUSED_RESULT update_refcount(BlockDriverState *bs,if (table_index != old_table_index) {if (refcount_block) { ret =block_cache_put (bs, s->refcount_block_cache, - (void **) &refcount_block); + (void **) &refcount_block);if (ret <0 ) { goto fail; }
@@ -494,7 +495,7 @@ fail:if (refcount_block) {int wret; wret =block_cache_put (bs, s->refcount_block_cache, - (void **) &refcount_block); + (void **) &refcount_block);if (wret <0 ) {return ret <0 ? ret : wret; }
@@ -768,7 +769,7 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs, l2_offset &=L1E_OFFSET_MASK ; ret =block_cache_get (bs, s->l2_table_cache, l2_offset, - (void **) &l2_table); + (void **) &l2_table);if (ret <0 ) { goto fail; }
@@ -815,12 +816,13 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs, s->refcount_block_cache); } l2_table[j] =cpu_to_be64 (offset); -block_cache_entry_mark_dirty (s->l2_table_cache, l2_table); +block_cache_entry_mark_dirty (s->l2_table_cache, + l2_table); } } } - ret =block_cache_put (bs, s->l2_table_cache, (void **) &l2_table); + ret =block_cache_put (bs, s->l2_table_cache, (void **) &l2_table);if (ret <0 ) { goto fail; }
@@ -847,7 +849,7 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs, ret =bdrv_flush (bs); fail:if (l2_table) { -block_cache_put (bs, s->l2_table_cache, (void **) &l2_table); +block_cache_put (bs, s->l2_table_cache, (void **) &l2_table); }/* Update L1 only if it isn't deleted anyway (addend = -1) */
block/qcow2.c +5 -2
@@ -454,8 +454,11 @@ static int qcow2_open(BlockDriverState *bs, QDict *options, int flags) }/* alloc L2 table/refcount block cache */ - s->l2_table_cache =block_cache_create (bs,L2_CACHE_SIZE ); - s->refcount_block_cache =block_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 */
block/qcow2.h +3 -21
@@ -27,6 +27,7 @@#include "qemu/aes.h" #include "block/coroutine.h" +#include "block/block-cache.h" //#define DEBUG_ALLOC //#define DEBUG_ALLOC2
@@ -97,9 +98,6 @@ typedef struct QCowSnapshot {uint64_t vm_clock_nsec; } QCowSnapshot; -struct BlockCache ; -typedef struct BlockCache BlockCache ; - typedef struct Qcow2UnknownHeaderExtension { uint32_t magic; uint32_t len;
@@ -149,8 +147,8 @@ typedef struct BDRVQcowState {uint64_t l1_table_offset;uint64_t *l1_table; -BlockCache * l2_table_cache; -BlockCache * refcount_block_cache; +BlockCache *l2_table_cache; +BlockCache *refcount_block_cache;uint8_t *cluster_cache;uint8_t *cluster_data;
@@ -392,20 +390,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 */ -BlockCache *block_cache_create (BlockDriverState *bs,int num_tables); -int block_cache_destroy (BlockDriverState * bs,BlockCache *c); - -void block_cache_entry_mark_dirty (BlockCache *c,void *table); -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); - -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); -#endif
include/block/block-cache.h new file +55 -0
55 lines not on this page · raw message
trace-events +1 -0
@@ -503,6 +503,7 @@ 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" +# 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"
Signature · 1 line
1.7.11.7
PATCH v19 6/8 add debug event for add-cow
In reply to Dongxu WangFrom: Dong Xu Wang <[email protected]>
--- block/blkdebug.c | 3 +++ block/block-cache.c | 4 ++-- include/block/block.h | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-)
block/blkdebug.c +3 -0
@@ -182,6 +182,9 @@ static const char *event_names[BLKDBG_EVENT_MAX] = { [BLKDBG_CLUSTER_ALLOC] = "cluster_alloc", [BLKDBG_CLUSTER_ALLOC_BYTES] = "cluster_alloc_bytes", [BLKDBG_CLUSTER_FREE] = "cluster_free", + + [BLKDBG_ADDCOW_READ] = "add_cow_read", + [BLKDBG_ADDCOW_WRITE] = "add_cow_write", };static int get_event_by_name (const char *name,BlkDebugEvent *event)
block/block-cache.c +2 -2
@@ -125,7 +125,7 @@ static int block_cache_entry_flush(BlockDriverState *bs, BlockCache *c, int i) }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 ); +BLKDBG_EVENT (bs->file,BLKDBG_ADDCOW_WRITE ); } ret = bdrv_pwrite(bs->file, c->entries[i].offset,
@@ -260,7 +260,7 @@ static int block_cache_do_get(BlockDriverState *bs, BlockCache *c,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 ); +BLKDBG_EVENT (bs->file,BLKDBG_ADDCOW_READ ); } ret = bdrv_pread(bs->file, offset, c->entries[i].table,
include/block/block.h +3 -0
@@ -451,6 +451,9 @@ typedef enum {BLKDBG_CLUSTER_ALLOC_BYTES ,BLKDBG_CLUSTER_FREE , +BLKDBG_ADDCOW_READ , +BLKDBG_ADDCOW_WRITE , +BLKDBG_EVENT_MAX , } BlkDebugEvent;
Signature · 1 line
1.7.11.7
Re: [Qemu-devel] [PATCH V19 6/8] add debug event for add-cow
Quoted 14 lines · Dongxu Wang
block/block-cache.c +1
-1@@ -125,7 +125,7 @@ static int block_cache_entry_flush(BlockDriverState *bs, BlockCache *c, int i) }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 ); +BLKDBG_EVENT (bs->file,BLKDBG_ADDCOW_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 266
Quoted 8 lines
block/block-cache.c +1
-1@@ -260,7 +260,7 @@ static int block_cache_do_get(BlockDriverState *bs, BlockCache *c,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 ); +BLKDBG_EVENT (bs->file,BLKDBG_ADDCOW_READ ); } ret = bdrv_pread(bs->file, offset, c->entries[i].table,
These patches are in the wrong order. The previous patch should not use the incorrect BLKDBG_COW_READ/BLKDBG_COW_WRITE values.
Put the blkdebug.c and block.h changes from this patch before the previous patch so block-cache.c can use BLKDBG_ADDCOW_READ/BLKDBG_ADDCOW_WRITE from the start.
Re: [Qemu-devel] [PATCH V19 6/8] add debug event for add-cow
In reply to Dongxu WangAm 30.05.2013 um 12:00 hat Dongxu Wang geschrieben:
Quoted 4 lines
From: Dong Xu Wang <[email protected]>
Signed-off-by Dong Xu Wang <[email protected]>Signed-off-by Dongxu Wang <[email protected]>
One of these should surely be enough?
Quoted 34 lines
--- block/blkdebug.c | 3 +++ block/block-cache.c | 4 ++-- include/block/block.h | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-)block/blkdebug.c +3
-0@@ -182,6 +182,9 @@ static const char *event_names[BLKDBG_EVENT_MAX] = { [BLKDBG_CLUSTER_ALLOC] = "cluster_alloc", [BLKDBG_CLUSTER_ALLOC_BYTES] = "cluster_alloc_bytes", [BLKDBG_CLUSTER_FREE] = "cluster_free", + + [BLKDBG_ADDCOW_READ] = "add_cow_read", + [BLKDBG_ADDCOW_WRITE] = "add_cow_write", };static int get_event_by_name (const char *name,BlkDebugEvent *event)block/block-cache.c +1
-1@@ -125,7 +125,7 @@ static int block_cache_entry_flush(BlockDriverState *bs, BlockCache *c, int i) }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 ); +BLKDBG_EVENT (bs->file,BLKDBG_ADDCOW_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 264
Quoted 6 lines
block/block-cache.c +1
-1@@ -260,7 +260,7 @@ static int block_cache_do_get(BlockDriverState *bs, BlockCache *c,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 ); +BLKDBG_EVENT (bs->file,BLKDBG_ADDCOW_READ ); }
Doesn't this break qcow2 test cases that don't get the BLKDBG_COW_READ/WRITE events any more now?
I guess you need to extend the cache creation function to also take function pointers for the blkdebug events that should be generated in the various places. Maybe it's enough data now that it would be worth having a BlockCache struct somewhere (e.g. embedded in BDRVQcow2State) and change the cache creation function to an init function that only takes a pointer to this struct.
Kevin
Re: [Qemu-devel] [PATCH V19 6/8] add debug event for add-cow
Quoted 40 lines · Kevin Wolf
Am 30.05.2013 um 12:00 hat Dongxu Wang geschrieben:
Quoted 4 lines
From: Dong Xu Wang <[email protected]>
Signed-off-by Dong Xu Wang <[email protected]>Signed-off-by Dongxu Wang <[email protected]>One of these should surely be enough?
Quoted 32 lines
--- block/blkdebug.c | 3 +++ block/block-cache.c | 4 ++-- include/block/block.h | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-)block/blkdebug.c +3
-0@@ -182,6 +182,9 @@ static const char *event_names[BLKDBG_EVENT_MAX] = { [BLKDBG_CLUSTER_ALLOC] = "cluster_alloc", [BLKDBG_CLUSTER_ALLOC_BYTES] = "cluster_alloc_bytes", [BLKDBG_CLUSTER_FREE] = "cluster_free", + + [BLKDBG_ADDCOW_READ] = "add_cow_read", + [BLKDBG_ADDCOW_WRITE] = "add_cow_write", };static int get_event_by_name (const char *name,BlkDebugEvent *event)block/block-cache.c +1
-1@@ -125,7 +125,7 @@ static int block_cache_entry_flush(BlockDriverState *bs, BlockCache *c, int i) }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 ); +BLKDBG_EVENT (bs->file,BLKDBG_ADDCOW_WRITE ); }
Quoted 11 lines
block/block-cache.c static int block_cache_do_get(BlockDriverState *bs, BlockCache *c, · line 264
Quoted 8 lines
block/block-cache.c +1
-1ret = bdrv_pwrite(bs->file, c->entries[i].offset,@@ -260,7 +260,7 @@ static int block_cache_do_get(BlockDriverState *bs, BlockCache *c,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 ); +BLKDBG_EVENT (bs->file,BLKDBG_ADDCOW_READ ); }Doesn't this break qcow2 test cases that don't get the BLKDBG_COW_READ/WRITE events any more now?
This event is only raised for BLKDBG_TABLE_BITMAP, which is new for add-cow only. qcow2 only uses L2 and refcount table types, not bitmap.
PATCH v19 7/8 add-cow file format core code.
In reply to Dongxu WangFrom: Dong Xu Wang <[email protected]>
add-cow file format core code. It use block-cache.c as cache code. It lacks of snapshot_blkdev support.
v18->v19:
1) add aio parallel write support.
2) fix flush method.
v17-v18:
1) use error_report, not fprintf.
2) remove version field from header.
3) header_size is MAX(cluster_size, 4096).
4) introduce s->cluster_sectors.
5) use BLKDBG_L2_LOAD/UPDATE.
v16->v17:
1) Use stringify.
v15->v16:
1) Judge if opts is null in add_cow_create function.
Conflicts:
block/block-cache.c
--- block/Makefile.objs | 1 + block/add-cow.c | 812 ++++++++++++++++++++++++++++++++++++++++++++ block/block-cache.c | 16 +- include/block/block-cache.h | 4 + include/block/block_int.h | 2 + 5 files changed, 831 insertions(+), 4 deletions(-) create mode 100644 block/add-cow.c
block/Makefile.objs +1 -0
@@ -1,6 +1,7 @@ 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 += block-cache.o +block-obj-y += add-cow.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 += vhdx.o
block/add-cow.c new file +812 -0
812 lines not on this page · raw message
block/block-cache.c +12 -4
@@ -164,14 +164,15 @@ int block_cache_flush(BlockDriverState *bs, BlockCache *c)return result; } -int block_cache_set_dependency (BlockDriverState *bs, -BlockCache *c, -BlockCache *dependency) +int block_cache_set_dependency_two_bs (BlockDriverState *bs, +BlockCache *c, +BlockDriverState *depend_bs, +BlockCache *dependency) {int ret;if (dependency->depends) { - ret =block_cache_flush_dependency (bs, dependency); + ret =block_cache_flush_dependency (depend_bs, dependency);if (ret <0 ) {return ret; }
@@ -188,6 +189,13 @@ int block_cache_set_dependency(BlockDriverState *bs,return 0 ; } +int block_cache_set_dependency (BlockDriverState *bs, +BlockCache *c, +BlockCache *dependency) +{ +return block_cache_set_dependency_two_bs (bs, c, bs, dependency); +} +void block_cache_depends_on_flush (BlockCache *c) { c->depends_on_flush = true;
include/block/block-cache.h +4 -0
@@ -45,6 +45,10 @@ int block_cache_flush(BlockDriverState *bs, BlockCache *c);int block_cache_set_dependency (BlockDriverState *bs,BlockCache *c,BlockCache *dependency); +int block_cache_set_dependency_two_bs (BlockDriverState *bs, +BlockCache *c, +BlockDriverState *depend_bs, +BlockCache *dependency);void block_cache_depends_on_flush (BlockCache *c);int block_cache_get (BlockDriverState *bs,BlockCache *c,uint64_t offset,void **table);
include/block/block_int.h +2 -0
@@ -57,6 +57,8 @@#define BLOCK_OPT_COMPAT_LEVEL "compat"#define BLOCK_OPT_LAZY_REFCOUNTS "lazy_refcounts"#define BLOCK_OPT_ADAPTER_TYPE "adapter_type" +#define BLOCK_OPT_IMAGE_FILE "image_file" +#define BLOCK_OPT_IMAGE_FMT "image_fmt"typedef struct BdrvTrackedRequest BdrvTrackedRequest ;
Signature · 1 line
1.7.11.7
Re: [Qemu-devel] [PATCH V19 7/8] add-cow file format core code.
Quoted 23 lines · Dongxu Wang
block/add-cow.c new file +23
-0+static coroutine_fn int add_cow_co_writev(BlockDriverState *bs, +int64_t sector_num, +int remaining_sectors, +QEMUIOVector *qiov) +{ +BDRVAddCowState *s = bs->opaque; +int ret =0 , i; +QEMUIOVector hd_qiov; +uint8_t *table; +uint64_t offset; +int mask = s->cluster_sectors -1 ; +int cluster_mask = s->cluster_size -1 ; +AddCowMeta *meta =NULL ; + +if (remaining_sectors ==0 ) { +return ret; + } + +qemu_co_mutex_lock (&s->lock); + + meta =g_malloc0 (sizeof (*meta)); +again: + *meta = (AddCowMeta ) {
block/add-cow.c line 614
Quoted 10 lines
block/add-cow.c new file +10
-0+ .next = QLIST_FIRST(&s->cluster_allocs), + .start_sector = sector_num, + .remaining_sectors = remaining_sectors, + }; +if (handle_dependencies (bs, sector_num, remaining_sectors) == -EAGAIN ) { + goto again; + } +qemu_co_queue_init (&meta->dependent_requests); +QLIST_INSERT_HEAD (&s->cluster_allocs, meta, next_in_flight); +qemu_iovec_init (&hd_qiov, qiov->niov);
hd_qiov is unused.
Quoted 1 line
+ ret =
bdrv_co_writev(s->image_hd, sector_num, remaining_sectors, qiov);
s->lock is held so write requests block other requests, this is not parallel.
PATCH v19 8/8 qemu-iotests: add add-cow iotests support
In reply to Dongxu WangFrom: Dong Xu Wang <[email protected]>
This patch will use qemu-iotests to test add-cow file format.
v17-v18:
1) add 037 and 038 tests.
--- tests/qemu-iotests/017 | 2 +- tests/qemu-iotests/020 | 2 +- tests/qemu-iotests/037 | 2 +- tests/qemu-iotests/038 | 2 +- tests/qemu-iotests/common | 6 ++++++ tests/qemu-iotests/common.rc | 15 ++++++++++++++- 6 files changed, 24 insertions(+), 5 deletions(-)
tests/qemu-iotests/017 +1 -1
@@ -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
@@ -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/037 +1 -1
@@ -38,7 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.rc . ./common.filter -_supported_fmt qcow qcow2 vmdk qed +_supported_fmt qcow qcow2 vmdk qed add-cow _supported_proto generic _supported_os Linux
tests/qemu-iotests/038 +1 -1
@@ -38,7 +38,7 @@ trap "_cleanup; exit \$status" 0 1 2 3 15 . ./common.rc . ./common.filter -_supported_fmt qcow2 qed +_supported_fmt qcow2 qed add-cow _supported_proto generic _supported_os Linux
tests/qemu-iotests/common +6 -0
@@ -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
@@ -165,6 +166,11 @@ testlist options xpand=false ;; + -add-cow) + IMGFMT=add-cow + xpand=false + ;; + -qcow) IMGFMT=qcow xpand=false
tests/qemu-iotests/common.rc +14 -1
@@ -110,6 +110,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
@@ -130,7 +140,8 @@ _make_test_img() -e "s# zeroed_grain=\\(on\\|off\\)##g" \ -e "s# subformat='[^']*'##g" \ -e "s# adapter_type='[^']*'##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
@@ -152,6 +163,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.11.7
Quoted 68 lines · Dongxu Wang
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_protocolandqed_read_string, so we will make them public.
snapshot_blkdevare 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/247508/
v18->v19:
1) support parallel aio write.
2) fix flush method.
3) other small fix.
v17 -> v18:
1) remove version field.
2) header size is maximum value and cluster size value.
3) fix type.
4) move struct to source file.
5)cluster_size->table_size.
6) useerror_report, not fprintf.
7) remove version field from header.
8)header_sizeisMAX(cluster_size, 4096).
9) introduces->cluster_sectors.
10) use BLKDBG_L2_LOAD/UPDATE.
11) add 037 and 038 tests.
v16->v17):
1) Use stringify.
v15->v16):
1) Rebased on QEMU upstream source tree.
2) Judge if opts is null inadd_cow_createfunction.
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_sizeconfiguable
3) Refactorblock-cache.c
4) Correct qemu-iotests script.
5) Other bug fix.
v11->v12:
1) Removed un-used feature bit.
2) Share cache code withqcow2.c.
3) Removesnapshot_blkdevsupport, will add it in another patch.
5) COW Bitmap field in add-cow file will be multiple of 65536.
6) fix grammer and typo.
Quoted 2 lines
Dong Xu Wang (8):
V18: docs: document for add-cow file format
Why mention V18 here?
Signature · 1 line
Fam
Quoted 101 lines · Dongxu Wang
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_protocolandqed_read_string, so we will make them public.
snapshot_blkdevare 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/247508/
v18->v19:
1) support parallel aio write.
2) fix flush method.
3) other small fix.
v17 -> v18:
1) remove version field.
2) header size is maximum value and cluster size value.
3) fix type.
4) move struct to source file.
5)cluster_size->table_size.
6) useerror_report, not fprintf.
7) remove version field from header.
8)header_sizeisMAX(cluster_size, 4096).
9) introduces->cluster_sectors.
10) use BLKDBG_L2_LOAD/UPDATE.
11) add 037 and 038 tests.
v16->v17):
1) Use stringify.
v15->v16):
1) Rebased on QEMU upstream source tree.
2) Judge if opts is null inadd_cow_createfunction.
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_sizeconfiguable
3) Refactorblock-cache.c
4) Correct qemu-iotests script.
5) Other bug fix.
v11->v12:
1) Removed un-used feature bit.
2) Share cache code withqcow2.c.
3) Removesnapshot_blkdevsupport, 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 (8):
V18: docs: document for add-cow file format
makepath_has_protocolnon static
qed_read_stringtobdrv_read_string
renameqcow2-cache.ctoblock-cache.c
Makeblock-cache.cbe common interface
add debug event for add-cow
add-cow file format core code.
qemu-iotests: add add-cow iotests supportblock.c | 29 +- block/Makefile.objs | 4 +- block/add-cow.c | 812 +++++++++++++++++++++++++++++++++++++++++++ block/blkdebug.c | 3 + block/block-cache.c | 342 ++++++++++++++++++ block/qcow2-cache.c | 323 ----------------- block/qcow2-cluster.c | 52 +-- block/qcow2-refcount.c | 62 ++-- block/qcow2.c | 21 +- block/qcow2.h | 24 +- block/qed.c | 34 +- docs/specs/add-cow.txt | 172 +++++++++ include/block/block-cache.h | 59 ++++ include/block/block.h | 6 + include/block/block_int.h | 2 + tests/qemu-iotests/017 | 2 +- tests/qemu-iotests/020 | 2 +- tests/qemu-iotests/037 | 2 +- tests/qemu-iotests/038 | 2 +- tests/qemu-iotests/common | 6 + tests/qemu-iotests/common.rc | 15 +- trace-events | 13 +- 22 files changed, 1536 insertions(+), 451 deletions(-)
Quoted 6 lines
create mode 100644
block/add-cow.c
create mode 100644block/block-cache.c
delete mode 100644block/qcow2-cache.c
create mode 100644docs/specs/add-cow.txt
create mode 100644include/block/block-cache.h
To add dirty block tracking about block, I think add-cow file format
will help, hope it can progress fast.
Signature · 3 lines
Best Regards Wenchao Xia