Skip to contentSkip to conversation

block-queue: Delay and batch metadata writes

RFC PATCH Superseded Kevin Wolf 2 participants 10 messages Nov 5, 2010 – Nov 18, 2010

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Kevin Wolf

author
#0 lore raw

v2 cover block-queue: Delay and batch metadata writes

Still not quite there, but should be a bit more correct than the first RFC. I still need to add proper error handling, the rest of block-queue itself should be fairly complete. With respect to error handling, IDE, virtio-blk and scsi-disk can deal with failed bdrv_(aio_)flush now, so just failing the next flush seems to be a viable option now.

The integration into block.c and qcow2 is only a preliminary hack for now and is going to be replaced.

Kevin Wolf (4):
block: Fake a bdrv_aio_pwrite
Add block-queue
Test cases for block-queue
qcow2: Use block-queue

 Makefile               |    1 +
 Makefile.objs          |    2 +-
 block-queue.c          |  625 ++++++++++++++++++++++++++++++++++++++++++++++++
 block-queue.h          |   49 ++++
 block.c                |   30 +++
 block.h                |    2 +
 block/qcow2-cluster.c  |   39 ++--
 block/qcow2-refcount.c |   62 +++---
 block/qcow2.c          |   18 ++-
 block/qcow2.h          |    4 +
 check-block-queue.c    |  402 +++++++++++++++++++++++++++++++
 11 files changed, 1182 insertions(+), 52 deletions(-)
 create mode 100644 block-queue.c
 create mode 100644 block-queue.h
 create mode 100644 check-block-queue.c
Signature · 1 line
1.7.2.3
author
#1 lore raw

RFC PATCH v2 1/4 block: Fake a bdrv_aio_pwrite

Don't read this patch, it will be thrown away :-)

---
 block.c |   30 ++++++++++++++++++++++++++++++
 block.h |    2 ++
 2 files changed, 32 insertions(+), 0 deletions(-)
block.c +30 -0
index 985d0b7..f75da13 100644
@@ -2053,6 +2053,36 @@ BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
     return ret;
 }
 
+struct pread_acb {
+    QEMUBH *bh;
+    BlockDriverCompletionFunc *cb;
+    void *opaque;
+    int ret;
+};
+
+static void bdrv_aio_pwrite_cb(void *opaque)
+{
+    struct pread_acb *x = opaque;
+
+    x->cb(x->opaque, x->ret);
+    qemu_bh_delete(x->bh);
+    free(x);
+}
+
+/* TODO Yes, this is a really bad fake implementation */
+BlockDriverAIOCB *bdrv_aio_pwrite(BlockDriverState *bs, int64_t offset, void* buf,
+    size_t bytes, BlockDriverCompletionFunc *cb, void *opaque)
+{
+    struct pread_acb *x = qemu_malloc(sizeof(*x));
+    x->bh = qemu_bh_new(bdrv_aio_pwrite_cb, x);
+    x->cb = cb;
+    x->opaque = opaque;
+    x->ret = bdrv_pwrite(bs, offset, buf, bytes);
+    qemu_bh_schedule(x->bh);
+
+    return (BlockDriverAIOCB*) 42;
+}
+
 
 typedef struct MultiwriteCB {
     int error;
block.h +2 -0
index a4facf2..5798cdb 100644
@@ -116,6 +116,8 @@ BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
 BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
                                   QEMUIOVector *iov, int nb_sectors,
                                   BlockDriverCompletionFunc *cb, void *opaque);
+BlockDriverAIOCB *bdrv_aio_pwrite(BlockDriverState *bs, int64_t offset, void* buf,
+    size_t bytes, BlockDriverCompletionFunc *cb, void *opaque);
 BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
 				 BlockDriverCompletionFunc *cb, void *opaque);
 void bdrv_aio_cancel(BlockDriverAIOCB *acb);
Signature · 1 line
1.7.2.3
author
#2 lore raw

RFC PATCH v2 2/4 Add block-queue

In reply to Kevin Wolf

Instead of directly executing writes and fsyncs, queue them and execute them asynchronously. What makes this interesting is that we can delay syncs and if multiple syncs occur, we can merge them into one bdrv_flush.

A typical sequence in qcow2 (simple cluster allocation) looks like this:

1. Update refcount table
2. bdrv_flush
3. Update L2 entry

If we delay the operation and get three of these sequences queued before actually executing, we end up with the following result, saving two syncs:

1. Update refcount table (req 1)
2. Update refcount table (req 2)
3. Update refcount table (req 3)
4. bdrv_flush
5. Update L2 entry (req 1)
6. Update L2 entry (req 2)
7. Update L2 entry (req 3)

This patch only commits a sync if either the guests has requested a flush or if a certain number of requests is in the queue, so usually we batch more than just three requests.

---
 Makefile.objs |    2 +-
 block-queue.c |  625 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 block-queue.h |   49 +++++
 3 files changed, 675 insertions(+), 1 deletions(-)
 create mode 100644 block-queue.c
 create mode 100644 block-queue.h
Makefile.objs +1 -1
index faf485e..83f77a5 100644
@@ -14,7 +14,7 @@ oslib-obj-$(CONFIG_POSIX) += oslib-posix.o
 # block-obj-y is code used by both qemu system emulation and qemu-img
 
 block-obj-y = cutils.o cache-utils.o qemu-malloc.o qemu-option.o module.o
-block-obj-y += nbd.o block.o aio.o aes.o qemu-config.o
+block-obj-y += nbd.o block.o aio.o aes.o qemu-config.o block-queue.o
 block-obj-$(CONFIG_POSIX) += posix-aio-compat.o
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
 
block-queue.c new file +625 -0

625 lines not on this page · raw message

block-queue.h new file +49 -0

49 lines not on this page · raw message

Signature · 1 line
1.7.2.3
#3 lore raw

Re: [Qemu-devel] [RFC PATCH v2 2/4] Add block-queue

Quoted 3 lines · Kevin Wolf

Instead of directly executing writes and fsyncs, queue them and execute them asynchronously. What makes this interesting is that we can delay syncs and if multiple syncs occur, we can merge them into one bdrv_flush.

The block-queue concept adds another layer to the cache= considerations. We're going to complete write requests that have not yet been issued to the host.

This won't work for cluster filesystems where multiple VMs are accessing the block device, although that is a niche case. I guess they can assume writes are visible after they receive a completion.

There must be a trade-off between holding back requests until a flush is forced and issuing requests as they come (what we do today). At some point if you have too much data queued the flush is going to be painful and might result in odd timing patterns. This feels similar to batching tx packets on the network.

Quoted 9 lines

A typical sequence in qcow2 (simple cluster allocation) looks like this:

1. Update refcount table
2. bdrv_flush
3. Update L2 entry

If we delay the operation and get three of these sequences queued before actually executing, we end up with the following result, saving two syncs:

Quoted 7 lines

1. Update refcount table (req 1)
2. Update refcount table (req 2)
3. Update refcount table (req 3)
4. bdrv_flush
5. Update L2 entry (req 1)
6. Update L2 entry (req 2)
7. Update L2 entry (req 3)

How does block-queue group writes 1-3 and 5-7 together? I thought reqs 1-3 will each have their own context but from a quick look at the code I don't think that is the case for qcow2 in patch 4.

Another way of asking is, how does block-queue know that it is safe to put writes 1-3 together before a bdrv_flush? Why doesn't it also put writes 5-7 before the flush?

Perhaps your current qcow2 implementation isn't taking advantage of block-queue bdrv_flush() batching for concurrent write requests?

I'm missing something ;).

Stefan

author
#4 lore raw

Re: [Qemu-devel] [RFC PATCH v2 2/4] Add block-queue

Am 17.11.2010 13:43, schrieb Stefan Hajnoczi:

Quoted 5 lines · Kevin Wolf
Quoted 3 lines

Instead of directly executing writes and fsyncs, queue them and execute them asynchronously. What makes this interesting is that we can delay syncs and if multiple syncs occur, we can merge them into one bdrv_flush.

Quoted 7 lines

The block-queue concept adds another layer to the cache= considerations. We're going to complete write requests that have not yet been issued to the host.

This won't work for cluster filesystems where multiple VMs are accessing the block device, although that is a niche case. I guess they can assume writes are visible after they receive a completion.

Right, but that doesn't work for anything but raw anyway. What I intend block-queue for is just metadata writes, so nothing that raw would use.

Quoted 5 lines

There must be a trade-off between holding back requests until a flush is forced and issuing requests as they come (what we do today). At some point if you have too much data queued the flush is going to be painful and might result in odd timing patterns. This feels similar to batching tx packets on the network.

I agree. Though as long as you use it only for metadata, the requests are relatively small and they overwrite each other so that there's a chance that you can drop some of the from the queue before submitting them.

What I implemented here is that the queue is flushed if it reaches some maximum length. So this value is where you'd need to find the right trade-off.

Quoted 21 lines
Quoted 9 lines

A typical sequence in qcow2 (simple cluster allocation) looks like this:

1. Update refcount table
2. bdrv_flush
3. Update L2 entry

If we delay the operation and get three of these sequences queued before actually executing, we end up with the following result, saving two syncs:

Quoted 7 lines

1. Update refcount table (req 1)
2. Update refcount table (req 2)
3. Update refcount table (req 3)
4. bdrv_flush
5. Update L2 entry (req 1)
6. Update L2 entry (req 2)
7. Update L2 entry (req 3)

How does block-queue group writes 1-3 and 5-7 together? I thought reqs 1-3 will each have their own context but from a quick look at the code I don't think that is the case for qcow2 in patch 4.

Quoted 8 lines

Another way of asking is, how does block-queue know that it is safe to put writes 1-3 together before a bdrv_flush? Why doesn't it also put writes 5-7 before the flush?

Perhaps your current qcow2 implementation isn't taking advantage of block-queue bdrv_flush() batching for concurrent write requests?

I'm missing something ;).

Yes, you are. ;-)

The contexts are indeed the mechanism that it uses to achieve this. Have a look at qcow_aio_setup, patch 4/4 adds a blkqueue_init_context() call there.

Kevin

#5 lore raw

Re: [Qemu-devel] [RFC PATCH v2 2/4] Add block-queue

Quoted 29 lines · Kevin Wolf

Am 17.11.2010 13:43, schrieb Stefan Hajnoczi:

Quoted 28 lines
Quoted 9 lines

A typical sequence in qcow2 (simple cluster allocation) looks like this:

1. Update refcount table
2. bdrv_flush
3. Update L2 entry

If we delay the operation and get three of these sequences queued before actually executing, we end up with the following result, saving two syncs:

Quoted 7 lines

1. Update refcount table (req 1)
2. Update refcount table (req 2)
3. Update refcount table (req 3)
4. bdrv_flush
5. Update L2 entry (req 1)
6. Update L2 entry (req 2)
7. Update L2 entry (req 3)

How does block-queue group writes 1-3 and 5-7 together?  I thought reqs 1-3 will each have their own context but from a quick look at the code I don't think that is the case for qcow2 in patch 4.

Another way of asking is, how does block-queue know that it is safe to put writes 1-3 together before a bdrv_flush?  Why doesn't it also put writes 5-7 before the flush?

Perhaps your current qcow2 implementation isn't taking advantage of block-queue bdrv_flush() batching for concurrent write requests?

Quoted 7 lines
Quoted 1 line

I'm missing something ;).

Yes, you are. ;-)

The contexts are indeed the mechanism that it uses to achieve this. Have a look at qcow_aio_setup, patch 4/4 adds a blkqueue_init_context() call there.

But there is a single context per qcow2 state. So this works for sequential requests but I guess the concurrent aio requests case doesn't work?

Stefan

author
#6 lore raw

Re: [Qemu-devel] [RFC PATCH v2 2/4] Add block-queue

Am 17.11.2010 17:23, schrieb Stefan Hajnoczi:

Quoted 34 lines · Kevin Wolf
Quoted 33 lines

Am 17.11.2010 13:43, schrieb Stefan Hajnoczi:

Quoted 21 lines
Quoted 9 lines

A typical sequence in qcow2 (simple cluster allocation) looks like this:

1. Update refcount table
2. bdrv_flush
3. Update L2 entry

If we delay the operation and get three of these sequences queued before actually executing, we end up with the following result, saving two syncs:

Quoted 7 lines

1. Update refcount table (req 1)
2. Update refcount table (req 2)
3. Update refcount table (req 3)
4. bdrv_flush
5. Update L2 entry (req 1)
6. Update L2 entry (req 2)
7. Update L2 entry (req 3)

How does block-queue group writes 1-3 and 5-7 together? I thought reqs 1-3 will each have their own context but from a quick look at the code I don't think that is the case for qcow2 in patch 4.

Quoted 8 lines

Another way of asking is, how does block-queue know that it is safe to put writes 1-3 together before a bdrv_flush? Why doesn't it also put writes 5-7 before the flush?

Perhaps your current qcow2 implementation isn't taking advantage of block-queue bdrv_flush() batching for concurrent write requests?

I'm missing something ;).

Yes, you are. ;-)

Quoted 7 lines
Quoted 3 lines

The contexts are indeed the mechanism that it uses to achieve this. Have a look at qcow_aio_setup, patch 4/4 adds a blkqueue_init_context() call there.

But there is a single context per qcow2 state. So this works for sequential requests but I guess the concurrent aio requests case doesn't work?

Yes, it needs to become a per-acb field. I think there's a TOOD comment. Probably the qcow2 conversion is going to be a patch series of its own because the ACB needs to be passed down everywhere for this to work. I mean I could come up with another hack like restoring s->bq_context from the ACB at the start of the callbacks, but that's certainly not the way to go for a real implementation.

Kevin

#7 lore raw

Re: [Qemu-devel] [RFC PATCH v2 2/4] Add block-queue

In reply to Kevin Wolf
Quoted 29 lines · Kevin Wolf

Am 17.11.2010 13:43, schrieb Stefan Hajnoczi:

Quoted 28 lines
Quoted 9 lines

A typical sequence in qcow2 (simple cluster allocation) looks like this:

1. Update refcount table
2. bdrv_flush
3. Update L2 entry

If we delay the operation and get three of these sequences queued before actually executing, we end up with the following result, saving two syncs:

Quoted 7 lines

1. Update refcount table (req 1)
2. Update refcount table (req 2)
3. Update refcount table (req 3)
4. bdrv_flush
5. Update L2 entry (req 1)
6. Update L2 entry (req 2)
7. Update L2 entry (req 3)

How does block-queue group writes 1-3 and 5-7 together?  I thought reqs 1-3 will each have their own context but from a quick look at the code I don't think that is the case for qcow2 in patch 4.

Another way of asking is, how does block-queue know that it is safe to put writes 1-3 together before a bdrv_flush?  Why doesn't it also put writes 5-7 before the flush?

Perhaps your current qcow2 implementation isn't taking advantage of block-queue bdrv_flush() batching for concurrent write requests?

Quoted 7 lines
Quoted 1 line

I'm missing something ;).

Yes, you are. ;-)

The contexts are indeed the mechanism that it uses to achieve this. Have a look at qcow_aio_setup, patch 4/4 adds a blkqueue_init_context() call there.

But there is a single context per qcow2 state. So this works for sequential requests but I guess the concurrent aio requests case doesn't work?

Stefan

author
#8 lore raw

RFC PATCH v2 3/4 Test cases for block-queue

In reply to Kevin Wolf
---
 Makefile            |    1 +
 check-block-queue.c |  402 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 403 insertions(+), 0 deletions(-)
 create mode 100644 check-block-queue.c
Makefile +1 -0
index 02698e9..7e2785f 100644
@@ -148,6 +148,7 @@ check-qdict: check-qdict.o qdict.o qfloat.o qint.o qstring.o qbool.o qemu-malloc
 check-qlist: check-qlist.o qlist.o qint.o qemu-malloc.o $(trace-obj-y)
 check-qfloat: check-qfloat.o qfloat.o qemu-malloc.o $(trace-obj-y)
 check-qjson: check-qjson.o qfloat.o qint.o qdict.o qstring.o qlist.o qbool.o qjson.o json-streamer.o json-lexer.o json-parser.o qemu-malloc.o $(trace-obj-y)
+check-block-queue: check-block-queue.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(filter-out block-queue.o,$(block-obj-y)) $(qobject-obj-y) qemu-timer-common.o
 
 clean:
 # avoid old build problems by removing potentially incorrect old files
check-block-queue.c new file +402 -0

402 lines not on this page · raw message

Signature · 1 line
1.7.2.3
author
#9 lore raw

RFC PATCH v2 4/4 qcow2: Use block-queue

In reply to Kevin Wolf

This needs to be reworked. It was a mostly automatic conversion that hasn't had a review and with just some fixes in places where things broke. It was good enough for some testing, though.

---
 block/qcow2-cluster.c  |   39 +++++++++++++++--------------
 block/qcow2-refcount.c |   62 ++++++++++++++++++++++++------------------------
 block/qcow2.c          |   18 +++++++++++++-
 block/qcow2.h          |    4 +++
 4 files changed, 72 insertions(+), 51 deletions(-)
block/qcow2-cluster.c +20 -19
index 4f7dc59..9f81593 100644
@@ -67,12 +67,13 @@ int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size)
         qemu_free(new_l1_table);
         return new_l1_table_offset;
     }
-    bdrv_flush(bs->file);
+    blkqueue_barrier(&s->bq_context);
 
     BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_WRITE_TABLE);
     for(i = 0; i < s->l1_size; i++)
         new_l1_table[i] = cpu_to_be64(new_l1_table[i]);
-    ret = bdrv_pwrite_sync(bs->file, new_l1_table_offset, new_l1_table, new_l1_size2);
+    ret = blkqueue_pwrite(&s->bq_context, new_l1_table_offset, new_l1_table, new_l1_size2);
+    blkqueue_barrier(&s->bq_context);
     if (ret < 0)
         goto fail;
     for(i = 0; i < s->l1_size; i++)
@@ -82,7 +83,8 @@ int qcow2_grow_l1_table(BlockDriverState *bs, int min_size, bool exact_size)
     BLKDBG_EVENT(bs->file, BLKDBG_L1_GROW_ACTIVATE_TABLE);
     cpu_to_be32w((uint32_t*)data, new_l1_size);
     cpu_to_be64w((uint64_t*)(data + 4), new_l1_table_offset);
-    ret = bdrv_pwrite_sync(bs->file, offsetof(QCowHeader, l1_size), data,sizeof(data));
+    ret = blkqueue_pwrite(&s->bq_context, offsetof(QCowHeader, l1_size), data, sizeof(data));
+    blkqueue_barrier(&s->bq_context);
     if (ret < 0) {
         goto fail;
     }
@@ -185,8 +187,7 @@ static int l2_load(BlockDriverState *bs, uint64_t l2_offset,
     *l2_table = s->l2_cache + (min_index << s->l2_bits);
 
     BLKDBG_EVENT(bs->file, BLKDBG_L2_LOAD);
-    ret = bdrv_pread(bs->file, l2_offset, *l2_table,
-        s->l2_size * sizeof(uint64_t));
+    ret = blkqueue_pread(&s->bq_context, l2_offset, *l2_table, s->l2_size * sizeof(uint64_t));
     if (ret < 0) {
         return ret;
     }
@@ -215,8 +216,8 @@ static int write_l1_entry(BlockDriverState *bs, int l1_index)
     }
 
     BLKDBG_EVENT(bs->file, BLKDBG_L1_UPDATE);
-    ret = bdrv_pwrite_sync(bs->file, s->l1_table_offset + 8 * l1_start_index,
-        buf, sizeof(buf));
+    ret = blkqueue_pwrite(&s->bq_context, s->l1_table_offset + 8 * l1_start_index, buf, sizeof(buf));
+    blkqueue_barrier(&s->bq_context);
     if (ret < 0) {
         return ret;
     }
@@ -251,7 +252,7 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
     if (l2_offset < 0) {
         return l2_offset;
     }
-    bdrv_flush(bs->file);
+    blkqueue_barrier(&s->bq_context);
 
     /* allocate a new entry in the l2 cache */
 
@@ -264,16 +265,15 @@ static int l2_allocate(BlockDriverState *bs, int l1_index, uint64_t **table)
     } else {
         /* if there was an old l2 table, read it from the disk */
         BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_COW_READ);
-        ret = bdrv_pread(bs->file, old_l2_offset, l2_table,
-            s->l2_size * sizeof(uint64_t));
+        ret = blkqueue_pread(&s->bq_context, old_l2_offset, l2_table, s->l2_size * sizeof(uint64_t));
         if (ret < 0) {
             goto fail;
         }
     }
     /* write the l2 table to the file */
     BLKDBG_EVENT(bs->file, BLKDBG_L2_ALLOC_WRITE);
-    ret = bdrv_pwrite_sync(bs->file, l2_offset, l2_table,
-        s->l2_size * sizeof(uint64_t));
+    ret = blkqueue_pwrite(&s->bq_context, l2_offset, l2_table, s->l2_size * sizeof(uint64_t));
+    blkqueue_barrier(&s->bq_context);
     if (ret < 0) {
         goto fail;
     }
@@ -393,8 +393,8 @@ static int qcow_read(BlockDriverState *bs, int64_t sector_num,
             memcpy(buf, s->cluster_cache + index_in_cluster * 512, 512 * n);
         } else {
             BLKDBG_EVENT(bs->file, BLKDBG_READ);
-            ret = bdrv_pread(bs->file, cluster_offset + index_in_cluster * 512, buf, n * 512);
-            if (ret != n * 512)
+            ret = blkqueue_pread(&s->bq_context, cluster_offset + index_in_cluster * 512, buf, n * 512);
+            if (ret < 0)
                 return -1;
             if (s->crypt_method) {
                 qcow2_encrypt_sectors(s, sector_num, buf, buf, n, 0,
@@ -646,11 +646,12 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
 
     BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED);
     l2_table[l2_index] = cpu_to_be64(cluster_offset);
-    if (bdrv_pwrite_sync(bs->file,
+    if (blkqueue_pwrite(&s->bq_context,
                     l2_offset + l2_index * sizeof(uint64_t),
                     l2_table + l2_index,
                     sizeof(uint64_t)) < 0)
         return 0;
+    blkqueue_barrier(&s->bq_context);
 
     return cluster_offset;
 }
@@ -663,6 +664,7 @@ uint64_t qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
 static int write_l2_entries(BlockDriverState *bs, uint64_t *l2_table,
     uint64_t l2_offset, int l2_index, int num)
 {
+    BDRVQcowState *s = bs->opaque;
     int l2_start_index = l2_index & ~(L1_ENTRIES_PER_SECTOR - 1);
     int start_offset = (8 * l2_index) & ~511;
     int end_offset = (8 * (l2_index + num) + 511) & ~511;
@@ -670,8 +672,7 @@ static int write_l2_entries(BlockDriverState *bs, uint64_t *l2_table,
     int ret;
 
     BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE);
-    ret = bdrv_pwrite(bs->file, l2_offset + start_offset,
-        &l2_table[l2_start_index], len);
+    ret = blkqueue_pwrite(&s->bq_context, l2_offset + start_offset, &l2_table[l2_start_index], len);
     if (ret < 0) {
         return ret;
     }
@@ -732,7 +733,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
      * need to be sure that the refcounts have been increased and COW was
      * handled.
      */
-    bdrv_flush(bs->file);
+    blkqueue_barrier(&s->bq_context);
 
     ret = write_l2_entries(bs, l2_table, l2_offset, l2_index, m->nb_clusters);
     if (ret < 0) {
@@ -745,7 +746,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
      * Also flush bs->file to get the right order for L2 and refcount update.
      */
     if (j != 0) {
-        bdrv_flush(bs->file);
+        blkqueue_barrier(&s->bq_context);
         for (i = 0; i < j; i++) {
             qcow2_free_any_clusters(bs,
                 be64_to_cpu(old_cluster[i]) & ~QCOW_OFLAG_COPIED, 1);
block/qcow2-refcount.c +31 -31

188 lines not on this page · raw message

block/qcow2.c +17 -1
index b816d87..b76ce6b 100644
@@ -237,6 +237,10 @@ static int qcow_open(BlockDriverState *bs, int flags)
     if (qcow2_read_snapshots(bs) < 0)
         goto fail;
 
+    /* Block queue */
+    s->bq = blkqueue_create(bs->file);
+    blkqueue_init_context(&s->bq_context, s->bq);
+
 #ifdef DEBUG_ALLOC
     qcow2_check_refcounts(bs);
 #endif
@@ -519,6 +523,7 @@ static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
         BlockDriverCompletionFunc *cb, void *opaque, int is_write)
 {
+    BDRVQcowState *s = bs->opaque;
     QCowAIOCB *acb;
 
     acb = qemu_aio_get(&qcow_aio_pool, bs, cb, opaque);
@@ -536,6 +541,10 @@ static QCowAIOCB *qcow_aio_setup(BlockDriverState *bs,
     acb->cluster_offset = 0;
     acb->l2meta.nb_clusters = 0;
     QLIST_INIT(&acb->l2meta.dependent_requests);
+
+    /* TODO Push the context into l2meta */
+    blkqueue_init_context(&s->bq_context, s->bq);
+
     return acb;
 }
 
@@ -694,6 +703,7 @@ static void qcow_close(BlockDriverState *bs)
     qemu_free(s->cluster_cache);
     qemu_free(s->cluster_data);
     qcow2_refcount_close(bs);
+    blkqueue_destroy(s->bq);
 }
 
 /*
@@ -1150,13 +1160,19 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,
 
 static void qcow_flush(BlockDriverState *bs)
 {
+    BDRVQcowState *s = bs->opaque;
+    blkqueue_flush(s->bq);
     bdrv_flush(bs->file);
 }
 
 static BlockDriverAIOCB *qcow_aio_flush(BlockDriverState *bs,
          BlockDriverCompletionFunc *cb, void *opaque)
 {
-    return bdrv_aio_flush(bs->file, cb, opaque);
+    BDRVQcowState *s = bs->opaque;
+    BlockQueueContext context;
+
+    blkqueue_init_context(&context, s->bq);
+    return blkqueue_aio_flush(&context, cb, opaque);
 }
 
 static int64_t qcow_vm_state_offset(BDRVQcowState *s)
block/qcow2.h +4 -0
index 2d22e5e..75b00a6 100644
@@ -26,6 +26,7 @@
 #define BLOCK_QCOW2_H
 
 #include "aes.h"
+#include "block-queue.h"
 
 //#define DEBUG_ALLOC
 //#define DEBUG_ALLOC2
@@ -108,6 +109,9 @@ typedef struct BDRVQcowState {
     int64_t free_cluster_index;
     int64_t free_byte_offset;
 
+    BlockQueue *bq;
+    BlockQueueContext bq_context;
+
     uint32_t crypt_method; /* current crypt method, 0 if no key yet */
     uint32_t crypt_method_header;
     AES_KEY aes_encrypt_key;
Signature · 1 line
1.7.2.3