block: replace .bdrv_is_allocated() with .bdrv_co_is_allocated()
cover block: replace .bdrv_is_allocated() with .bdrv_co_is_allocated()
The bdrv_is_allocated() interface is not suitable for use while the VM is running. It is a synchronous interface so it may block the running VM for arbitrary amounts of time. It also assumes it is the only block driver operation and there is a risk that internal state could be corrupted if asynchronous I/O is currently pending.
This patch series refactors the block layer so that the bdrv_is_allocated() operation is performed internally in coroutine context. We can then introduce a new public bdrv_co_is_allocated() function that is suitable for use when the VM is running.
Making these changes without breaking git bisect means we need to first introduce BlockDriver .bdrv_co_is_allocated() and add wrapper code into bdrv_is_allocated() so existing callers can use converted BlockDrivers.
After converting all block drivers there are no .bdrv_is_allocated() functions left and the interface can be removed from BlockDriver.
Finally, we can add the new public bdrv_co_is_allocated() public interface and turn the old bdrv_is_allocated() into a wrapper.
This series is a prerequisite for copy-on-read, which needs to check whether sectors are allocated while the VM is running.
Stefan Hajnoczi (10):
block: use public bdrv_is_allocated() interface
block: add .bdrv_co_is_allocated()
qed: convert to .bdrv_co_is_allocated()
block: convert qcow2, qcow2, and vmdk to .bdrv_co_is_allocated()
vvfat: use public block layer interface
vvfat: convert to .bdrv_co_is_allocated()
vdi: convert to .bdrv_co_is_allocated()
cow: convert to .bdrv_co_is_allocated()
block: drop .bdrv_is_allocated() interface
block: add bdrv_co_is_allocated() interface
block.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++----- block.h | 2 + block/cow.c | 8 +++--- block/qcow.c | 8 ++++-- block/qcow2.c | 13 +++++++---- block/qed.c | 15 ++++++++++--- block/vdi.c | 6 ++-- block/vmdk.c | 8 ++++-- block/vvfat.c | 19 +++++++---------- block_int.h | 4 +- 10 files changed, 100 insertions(+), 41 deletions(-)
Signature · 1 line
1.7.7.1
PATCH 1/10 block: use public bdrv_is_allocated() interface
There is no need for bdrv_commit() to use the BlockDriver .bdrv_is_allocated() interface directly. Converting to the public interface gives us the freedom to drop .bdrv_is_allocated() entirely in favor of a new .bdrv_co_is_allocated() in the future.
--- block.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
block.c +1 -1
@@ -915,7 +915,7 @@ int bdrv_commit(BlockDriverState *bs) buf =g_malloc (COMMIT_BUF_SECTORS *BDRV_SECTOR_SIZE );for (sector =0 ; sector < total_sectors; sector += n) { -if (drv->bdrv_is_allocated (bs, sector,COMMIT_BUF_SECTORS , &n)) { +if (bdrv_is_allocated (bs, sector,COMMIT_BUF_SECTORS , &n)) {if (bdrv_read (bs, sector, buf, n) !=0 ) { ret = -EIO ;
Signature · 1 line
1.7.7.1
PATCH 2/10 block: add .bdrv_co_is_allocated()
In reply to Stefan HajnocziThis patch adds the .bdrv_co_is_allocated() interface which is identical to .bdrv_is_allocated() but runs in coroutine context. Running in coroutine context implies that other coroutines might be performing I/O
at the same time. Therefore it must be safe to run while the following
BlockDriver functions are in-flight:
.bdrv_co_readv()
.bdrv_co_writev()
.bdrv_co_flush()
.bdrv_co_is_allocated()The new .bdrv_co_is_allocated() interface is useful because it can be used when a VM is running, whereas .bdrv_is_allocated() is a synchronous interface that does not cope with parallel requests.
--- block.c | 37 +++++++++++++++++++++++++++++++++++++ block_int.h | 2 ++ 2 files changed, 39 insertions(+), 0 deletions(-)
block.c +37 -0
@@ -1771,6 +1771,26 @@ int bdrv_has_zero_init(BlockDriverState *bs)return 1 ; } +typedef struct BdrvCoIsAllocatedData { +BlockDriverState *bs; +int64_t sector_num; +int nb_sectors; +int *pnum; +int ret; +bool done; +}BdrvCoIsAllocatedData ; + +/* Coroutine wrapper for bdrv_is_allocated() */ +static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) +{ +BdrvCoIsAllocatedData *data = opaque; +BlockDriverState *bs = data->bs; + + data->ret = bs->drv->bdrv_co_is_allocated (bs, data->sector_num, + data->nb_sectors, data->pnum); + data->done = true; +} +/* * Returns true iff the specified sector is present in the disk image. Drivers * not implementing the functionality are assumed to not support backing files,
@@ -1786,6 +1806,23 @@ int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum) {int64_t n; +if (bs->drv->bdrv_co_is_allocated) { +Coroutine *co; +BdrvCoIsAllocatedData data = { + .bs = bs, + .sector_num = sector_num, + .nb_sectors = nb_sectors, + .pnum = pnum, + .done = false, + }; + + co =qemu_coroutine_create (bdrv_is_allocated_co_entry); +qemu_coroutine_enter (co, &data); +while (!data.done) { +qemu_aio_wait (); + } +return data.ret; + }if (!bs->drv->bdrv_is_allocated) {if (sector_num >= bs->total_sectors) { *pnum =0 ;
block_int.h +2 -0
@@ -87,6 +87,8 @@ struct BlockDriver { int coroutine_fn (*bdrv_co_flush)(BlockDriverState *bs); int coroutine_fn (*bdrv_co_discard)(BlockDriverState *bs,int64_t sector_num,int nb_sectors); + int coroutine_fn (*bdrv_co_is_allocated)(BlockDriverState *bs, +int64_t sector_num,int nb_sectors,int *pnum);int (*bdrv_aio_multiwrite)(BlockDriverState *bs,BlockRequest *reqs,int num_reqs);
Signature · 1 line
1.7.7.1
Re: [Qemu-devel] [PATCH 02/10] block: add .bdrv_co_is_allocated()
Quoted 58 lines · Stefan Hajnoczi
This patch adds the .bdrv_co_is_allocated() interface which is identical to .bdrv_is_allocated() but runs in coroutine context. Running in coroutine context implies that other coroutines might be performing I/O at the same time. Therefore it must be safe to run while the following BlockDriver functions are in-flight:
.bdrv_co_readv()
.bdrv_co_writev()
.bdrv_co_flush()
.bdrv_co_is_allocated()The new .bdrv_co_is_allocated() interface is useful because it can be used when a VM is running, whereas .bdrv_is_allocated() is a synchronous interface that does not cope with parallel requests.
Signed-off-by Stefan Hajnoczi <[email protected]>---
block.c | 37 +++++++++++++++++++++++++++++++++++++block_int.h| 2 ++2 files changed, 39 insertions(+), 0 deletions(-)block.c +22
-0@@ -1771,6 +1771,26 @@ int bdrv_has_zero_init(BlockDriverState *bs) return 1; } +typedef struct BdrvCoIsAllocatedData { + BlockDriverState *bs; + int64_t sector_num; + int nb_sectors; + int *pnum; + int ret; + bool done; +}BdrvCoIsAllocatedData ; + +/* Coroutine wrapper for bdrv_is_allocated() */ +static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) +{ + BdrvCoIsAllocatedData *data = opaque; + BlockDriverState *bs = data->bs; + + data->ret = bs->drv->bdrv_co_is_allocated(bs, data->sector_num, + data->nb_sectors, data->pnum); + data->done = true; +} + /** Returns true iff the specified sector is present in the disk image. Drivers * not implementing the functionality are assumed to not support backing files, @@ -1786,6 +1806,23 @@ int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum) { int64_t n; + if (bs->drv->bdrv_co_is_allocated) { + Coroutine *co;
block.c int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, · line 1820
Quoted 10 lines
block.c +10
-0+ BdrvCoIsAllocatedData data = { + .bs = bs, + .sector_num = sector_num, + .nb_sectors = nb_sectors, + .pnum = pnum, + .done = false, + }; + + co = qemu_coroutine_create(bdrv_is_allocated_co_entry); + qemu_coroutine_enter(co, &data);
Since this main process will stop within qemu_coroutine_enter() until bdrv_is_allocated_co_entry() is completed, three lines of condition codes below are unnecessary, right?
Quoted 26 lines
block.c +5
-0+ while (!data.done) { + qemu_aio_wait(); + } + return data.ret; + } if (!bs->drv->bdrv_is_allocated) { if (sector_num >= bs->total_sectors) { *pnum = 0;block_int.h +2
-1@@ -87,6 +87,8 @@ struct BlockDriver { int coroutine_fn (*bdrv_co_flush)(BlockDriverState *bs); int coroutine_fn (*bdrv_co_discard)(BlockDriverState *bs, int64_t sector_num, int nb_sectors); + int coroutine_fn (*bdrv_co_is_allocated)(BlockDriverState *bs, + int64_t sector_num, int nb_sectors, int *pnum); int (*bdrv_aio_multiwrite)(BlockDriverState *bs, BlockRequest *reqs, int num_reqs); --1.7.7.1
Signature · 3 lines
Regards, Zhi Yong Wu
Re: [Qemu-devel] [PATCH 02/10] block: add .bdrv_co_is_allocated()
Quoted 5 lines · Zhi Yong Wu
Quoted 2 lines
Quoted 2 lines
+ co =
qemu_coroutine_create(bdrv_is_allocated_co_entry);
+qemu_coroutine_enter(co,&data);Since this main process will stop within
qemu_coroutine_enter()untilbdrv_is_allocated_co_entry()is completed, three lines of condition codes below are unnecessary, right?
No, they are necessary. They are executed when bdrv_is_allocated_co_entry calls qemu_coroutine_yield.
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
+ while (!data.done) {
+qemu_aio_wait();
+ }
Paolo
Re: [Qemu-devel] [PATCH 02/10] block: add .bdrv_co_is_allocated()
Quoted 11 lines · Paolo Bonzini
Quoted 7 lines · Zhi Yong Wu
Quoted 3 lines
Quoted 3 lines
+ co =
qemu_coroutine_create(bdrv_is_allocated_co_entry);
+ qemu_coroutine_enter(co,&data);Since this main process will stop within
qemu_coroutine_enter()untilbdrv_is_allocated_co_entry()is completed, three lines of condition codes below are unnecessary, right?No, they are necessary. They are executed when
bdrv_is_allocated_co_entrycallsqemu_coroutine_yield.
Right, But i don't think that they are necessary.
after bdrv_is_allocated_co_entry has basically completed all main task, it call qemu_coroutine_yield to wake up this current process; At that point, it is equal to the setting of data.done. Why need you still the three lines of codes below?
Quoted 9 lines
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
+ while (!data.done) {
+ qemu_aio_wait();
+ }Paolo
Signature · 3 lines
Regards, Zhi Yong Wu
Re: [Qemu-devel] [PATCH 02/10] block: add .bdrv_co_is_allocated()
Quoted 8 lines · Zhi Yong Wu
Quoted 2 lines
Quoted 2 lines
No, they are necessary. They are executed when
bdrv_is_allocated_co_entrycallsqemu_coroutine_yield.Right, But i don't think that they are necessary.
after
bdrv_is_allocated_co_entryhas basically completed all main task, it callqemu_coroutine_yieldto wake up this current process; At that point, it is equal to the setting of data.done. Why need you still the three lines of codes below?
Any function _called_ by the driver's is_allocated member could call qemu_coroutine_yield, for example bdrv_read.
Paolo
Re: [Qemu-devel] [PATCH 02/10] block: add .bdrv_co_is_allocated()
Quoted 8 lines · Paolo Bonzini
Quoted 7 lines · Zhi Yong Wu
Quoted 4 lines
Quoted 4 lines
No, they are necessary. They are executed when
bdrv_is_allocated_co_entry
callsqemu_coroutine_yield.Right, But i don't think that they are necessary.
Quoted 7 lines
Quoted 4 lines
after
bdrv_is_allocated_co_entryhas basically completed all main task, it callqemu_coroutine_yieldto wake up this current process; At that point, it is equal to the setting of data.done. Why need you still the three lines of codes below?Any function _called_ by the driver's
is_allocatedmember could callqemu_coroutine_yield, for examplebdrv_read.
Good example. thanks.
Quoted 3 lines
Paolo
Signature · 3 lines
Regards, Zhi Yong Wu
Re: [Qemu-devel] [PATCH 02/10] block: add .bdrv_co_is_allocated()
In reply to Zhi Yong WuAm 14.11.2011 04:04, schrieb Zhi Yong Wu:
Quoted 65 lines
Quoted 63 lines · Stefan Hajnoczi
This patch adds the .bdrv_co_is_allocated() interface which is identical to .bdrv_is_allocated() but runs in coroutine context. Running in coroutine context implies that other coroutines might be performing I/O
at the same time. Therefore it must be safe to run while the following
BlockDriver functions are in-flight:.bdrv_co_readv() .bdrv_co_writev() .bdrv_co_flush() .bdrv_co_is_allocated()The new .bdrv_co_is_allocated() interface is useful because it can be used when a VM is running, whereas .bdrv_is_allocated() is a synchronous interface that does not cope with parallel requests.
Signed-off-by Stefan Hajnoczi <[email protected]>--- block.c | 37 +++++++++++++++++++++++++++++++++++++ block_int.h | 2 ++ 2 files changed, 39 insertions(+), 0 deletions(-)block.c +27
-0@@ -1771,6 +1771,26 @@ int bdrv_has_zero_init(BlockDriverState *bs)return 1 ; } +typedef struct BdrvCoIsAllocatedData { +BlockDriverState *bs; +int64_t sector_num; +int nb_sectors; +int *pnum; +int ret; +bool done; +}BdrvCoIsAllocatedData ; + +/* Coroutine wrapper for bdrv_is_allocated() */ +static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) +{ +BdrvCoIsAllocatedData *data = opaque; +BlockDriverState *bs = data->bs; + + data->ret = bs->drv->bdrv_co_is_allocated (bs, data->sector_num, + data->nb_sectors, data->pnum); + data->done = true; +} +/* * Returns true iff the specified sector is present in the disk image. Drivers * not implementing the functionality are assumed to not support backing files, @@ -1786,6 +1806,23 @@ int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum) {int64_t n; +if (bs->drv->bdrv_co_is_allocated) { +Coroutine *co; +BdrvCoIsAllocatedData data = { + .bs = bs, + .sector_num = sector_num, + .nb_sectors = nb_sectors, + .pnum = pnum,
Quoted 8 lines
block.c int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, · line 1820
Quoted 5 lines
block.c +5
-0+ .done = false, + }; + + co =qemu_coroutine_create (bdrv_is_allocated_co_entry); +qemu_coroutine_enter (co, &data);Since this main process will stop within
qemu_coroutine_enter()untilbdrv_is_allocated_co_entry()is completed, three lines of condition codes below are unnecessary, right?
No, they are needed. We want to wait until the whole operation has completed, but qemu_coroutine_enter() returns after the first qemu_coroutine_yield().
Kevin
Re: [Qemu-devel] [PATCH 02/10] block: add .bdrv_co_is_allocated()
Quoted 75 lines · Kevin Wolf
Am 14.11.2011 04:04, schrieb Zhi Yong Wu:
Quoted 65 lines
Quoted 63 lines · Stefan Hajnoczi
This patch adds the .bdrv_co_is_allocated() interface which is identical to .bdrv_is_allocated() but runs in coroutine context. Running in coroutine context implies that other coroutines might be performing I/O at the same time. Therefore it must be safe to run while the following BlockDriver functions are in-flight:
.bdrv_co_readv()
.bdrv_co_writev()
.bdrv_co_flush()
.bdrv_co_is_allocated()The new .bdrv_co_is_allocated() interface is useful because it can be used when a VM is running, whereas .bdrv_is_allocated() is a synchronous interface that does not cope with parallel requests.
Signed-off-by Stefan Hajnoczi <[email protected]>---
block.c | 37 +++++++++++++++++++++++++++++++++++++block_int.h| 2 ++2 files changed, 39 insertions(+), 0 deletions(-)block.c +27
-0@@ -1771,6 +1771,26 @@ int bdrv_has_zero_init(BlockDriverState *bs) return 1; } +typedef struct BdrvCoIsAllocatedData { + BlockDriverState *bs; + int64_t sector_num; + int nb_sectors; + int *pnum; + int ret; + bool done; +}BdrvCoIsAllocatedData ; + +/* Coroutine wrapper for bdrv_is_allocated() */ +static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) +{ + BdrvCoIsAllocatedData *data = opaque; + BlockDriverState *bs = data->bs; + + data->ret = bs->drv->bdrv_co_is_allocated(bs, data->sector_num, + data->nb_sectors, data->pnum); + data->done = true; +} + /** Returns true iff the specified sector is present in the disk image. Drivers * not implementing the functionality are assumed to not support backing files, @@ -1786,6 +1806,23 @@ int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum) { int64_t n; + if (bs->drv->bdrv_co_is_allocated) { + Coroutine *co; + BdrvCoIsAllocatedData data = { + .bs = bs, + .sector_num = sector_num, + .nb_sectors = nb_sectors, + .pnum = pnum,Quoted 8 lines
block.c int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, · line 1820
Quoted 5 lines
block.c +5
-0+ .done = false, + }; + + co = qemu_coroutine_create(bdrv_is_allocated_co_entry); + qemu_coroutine_enter(co, &data);Since this main process will stop within
qemu_coroutine_enter()untilbdrv_is_allocated_co_entry()is completed, three lines of condition codes below are unnecessary, right?
Quoted 3 lines
No, they are needed. We want to wait until the whole operation has completed, but
qemu_coroutine_enter()returns after the firstqemu_coroutine_yield().
got it. thanks.
Quoted 3 lines
Kevin
Signature · 3 lines
Regards, Zhi Yong Wu
PATCH 3/10 qed: convert to .bdrv_co_is_allocated()
In reply to Stefan HajnocziThe bdrv_qed_is_allocated() function is a synchronous wrapper around qed_find_cluster(), which performs the cluster lookup. In order to convert the synchronous function to a coroutine function we yield instead of using qemu_aio_wait(). Note that QED's cache is already safe for parallel requests so no locking is needed.
--- block/qed.c | 15 +++++++++++---- 1 files changed, 11 insertions(+), 4 deletions(-)
block/qed.c +11 -4
@@ -651,6 +651,7 @@ static int bdrv_qed_create(const char *filename, QEMUOptionParameter *options) }typedef struct { +Coroutine *co;int is_allocated;int *pnum; }QEDIsAllocatedCB ;
@@ -660,10 +661,14 @@ static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t lQEDIsAllocatedCB *cb = opaque; *cb->pnum = len /BDRV_SECTOR_SIZE ; cb->is_allocated = (ret ==QED_CLUSTER_FOUND || ret ==QED_CLUSTER_ZERO ); +if (cb->co) { +qemu_coroutine_enter (cb->co,NULL ); + } } -static int bdrv_qed_is_allocated (BlockDriverState *bs,int64_t sector_num, -int nb_sectors,int *pnum) +static int coroutine_fn bdrv_qed_co_is_allocated(BlockDriverState *bs, +int64_t sector_num, +int nb_sectors,int *pnum) {BDRVQEDState *s = bs->opaque;uint64_t pos = (uint64_t )sector_num *BDRV_SECTOR_SIZE ;
@@ -676,8 +681,10 @@ static int bdrv_qed_is_allocated(BlockDriverState *bs, int64_t sector_num,qed_find_cluster (s, &request, pos, len, qed_is_allocated_cb, &cb); +/* Now sleep if the callback wasn't invoked immediately */ while (cb.is_allocated ==-1 ) { -qemu_aio_wait (); + cb.co =qemu_coroutine_self (); +qemu_coroutine_yield (); }qed_unref_l2_cache_entry (request.l2_table);
@@ -1475,7 +1482,7 @@ static BlockDriver bdrv_qed = { .bdrv_open = bdrv_qed_open, .bdrv_close = bdrv_qed_close, .bdrv_create = bdrv_qed_create, - .bdrv_is_allocated = bdrv_qed_is_allocated, + .bdrv_co_is_allocated = bdrv_qed_co_is_allocated, .bdrv_make_empty = bdrv_qed_make_empty, .bdrv_aio_readv = bdrv_qed_aio_readv, .bdrv_aio_writev = bdrv_qed_aio_writev,
Signature · 1 line
1.7.7.1
PATCH 4/10 block: convert qcow2, qcow2, and vmdk to .bdrv_co_is_allocated()
In reply to Stefan HajnocziThe qcow2, qcow, and vmdk block drivers are based on coroutines. They have a coroutine mutex which protects internal state. We can convert the .bdrv_is_allocated() function to .bdrv_co_is_allocated() by holding the mutex around the cluster lookup operation.
--- block/qcow.c | 8 +++++--- block/qcow2.c | 13 ++++++++----- block/vmdk.c | 8 +++++--- 3 files changed, 18 insertions(+), 11 deletions(-)
block/qcow.c +5 -3
@@ -360,14 +360,16 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,return cluster_offset; } -static int qcow_is_allocated (BlockDriverState *bs,int64_t sector_num, -int nb_sectors,int *pnum) +static int coroutine_fn qcow_co_is_allocated(BlockDriverState *bs, +int64_t sector_num,int nb_sectors,int *pnum) {BDRVQcowState *s = bs->opaque;int index_in_cluster, n;uint64_t cluster_offset; +qemu_co_mutex_lock (&s->lock); cluster_offset =get_cluster_offset (bs, sector_num <<9 ,0 ,0 ,0 ,0 ); +qemu_co_mutex_unlock (&s->lock); index_in_cluster = sector_num & (s->cluster_sectors -1 ); n = s->cluster_sectors - index_in_cluster;if (n > nb_sectors)
@@ -828,7 +830,7 @@ static BlockDriver bdrv_qcow = { .bdrv_open = qcow_open, .bdrv_close = qcow_close, .bdrv_create = qcow_create, - .bdrv_is_allocated = qcow_is_allocated, + .bdrv_co_is_allocated = qcow_co_is_allocated, .bdrv_set_key = qcow_set_key, .bdrv_make_empty = qcow_make_empty, .bdrv_co_readv = qcow_co_readv,
block/qcow2.c +8 -5
@@ -342,16 +342,19 @@ static int qcow2_set_key(BlockDriverState *bs, const char *key)return 0 ; } -static int qcow2_is_allocated (BlockDriverState *bs,int64_t sector_num, -int nb_sectors,int *pnum) +static int coroutine_fn qcow2_co_is_allocated(BlockDriverState *bs, +int64_t sector_num,int nb_sectors,int *pnum) { +BDRVQcowState *s = bs->opaque;uint64_t cluster_offset;int ret; *pnum = nb_sectors; -/* FIXME We can get errors here, but the bdrv_is_allocated interface can't -* pass them on today */ +/* FIXME We can get errors here, but the bdrv_co_is_allocated interface +* can't pass them on today */ +qemu_co_mutex_lock (&s->lock); ret =qcow2_get_cluster_offset (bs, sector_num <<9 , pnum, &cluster_offset); +qemu_co_mutex_unlock (&s->lock);if (ret <0 ) { *pnum =0 ; }
@@ -1239,7 +1242,7 @@ static BlockDriver bdrv_qcow2 = { .bdrv_open = qcow2_open, .bdrv_close = qcow2_close, .bdrv_create = qcow2_create, - .bdrv_is_allocated = qcow2_is_allocated, + .bdrv_co_is_allocated = qcow2_co_is_allocated, .bdrv_set_key = qcow2_set_key, .bdrv_make_empty = qcow2_make_empty,
block/vmdk.c +5 -3
@@ -852,8 +852,8 @@ static VmdkExtent *find_extent(BDRVVmdkState *s,return NULL ; } -static int vmdk_is_allocated (BlockDriverState *bs,int64_t sector_num, -int nb_sectors,int *pnum) +static int coroutine_fn vmdk_co_is_allocated(BlockDriverState *bs, +int64_t sector_num,int nb_sectors,int *pnum) {BDRVVmdkState *s = bs->opaque;int64_t index_in_cluster, n, ret;
@@ -864,8 +864,10 @@ static int vmdk_is_allocated(BlockDriverState *bs, int64_t sector_num,if (!extent) {return 0 ; } +qemu_co_mutex_lock (&s->lock); ret =get_cluster_offset (bs, extent,NULL , sector_num *512 ,0 , &offset); +qemu_co_mutex_unlock (&s->lock);/* get_cluster_offset returning 0 means success */ ret = !ret;
@@ -1582,7 +1584,7 @@ static BlockDriver bdrv_vmdk = { .bdrv_close = vmdk_close, .bdrv_create = vmdk_create, .bdrv_co_flush = vmdk_co_flush, - .bdrv_is_allocated = vmdk_is_allocated, + .bdrv_co_is_allocated = vmdk_co_is_allocated, .bdrv_get_allocated_file_size = vmdk_get_allocated_file_size, .create_options = vmdk_create_options,
Signature · 1 line
1.7.7.1
PATCH 5/10 vvfat: use public block layer interface
In reply to Stefan HajnocziFor some reason vvfat.c invokes BlockDriver functions directly and does not go through block.h functions. Change all direct calls except for the .bdrv_make_empty() call for which there is no public interface.
This change makes the conversion to .bdrv_co_is_allocated() possible.
--- block/vvfat.c | 15 ++++++--------- 1 files changed, 6 insertions(+), 9 deletions(-)
block/vvfat.c +6 -9
@@ -1254,10 +1254,9 @@ static int vvfat_read(BlockDriverState *bs, int64_t sector_num,return -1 ;if (s->qcow) {int n; -if (s->qcow->drv->bdrv_is_allocated (s->qcow, - sector_num, nb_sectors-i, &n)) { +if (bdrv_is_allocated (s->qcow, sector_num, nb_sectors-i, &n)) {DLOG (fprintf (stderr,"sectors %d+%d allocated\n" , (int )sector_num, n)); -if (s->qcow->drv->bdrv_read (s->qcow, sector_num, buf+i*0x200 , n)) +if (bdrv_read (s->qcow, sector_num, buf+i*0x200 , n))return -1 ; i += n -1 ; sector_num += n -1 ;
@@ -1516,7 +1515,7 @@ static inline int cluster_was_modified(BDRVVVFATState* s, uint32_t cluster_num)return 0 ;for (i =0 ; !was_modified && i < s->sectors_per_cluster; i++) - was_modified = s->qcow->drv->bdrv_is_allocated (s->qcow, + was_modified =bdrv_is_allocated (s->qcow,cluster2sector (s, cluster_num) + i,1 , &dummy);return was_modified;
@@ -1666,13 +1665,11 @@ static uint32_t get_cluster_count_for_direntry(BDRVVVFATState* s,vvfat_close_current_file (s);for (i =0 ; i < s->sectors_per_cluster; i++) -if (!s->qcow->drv->bdrv_is_allocated (s->qcow, - offset + i,1 , &dummy)) { +if (!bdrv_is_allocated (s->qcow, offset + i,1 , &dummy)) {if (vvfat_read (s->bs, offset, s->cluster_buffer,1 ))return -1 ; -if (s->qcow->drv->bdrv_write (s->qcow, - offset, s->cluster_buffer,1 )) +if (bdrv_write (s->qcow, offset, s->cluster_buffer,1 ))return -2 ; } }
@@ -2714,7 +2711,7 @@ DLOG(checkpoint());* Use qcow backend. Commit later. */ DLOG (fprintf (stderr,"Write to qcow backend: %d + %d\n" , (int )sector_num, nb_sectors)); - ret = s->qcow->drv->bdrv_write (s->qcow, sector_num, buf, nb_sectors); + ret =bdrv_write (s->qcow, sector_num, buf, nb_sectors);if (ret <0 ) {fprintf (stderr,"Error writing to qcow backend\n" );return ret;
Signature · 1 line
1.7.7.1
Re: [Qemu-devel] [PATCH 05/10] vvfat: use public block layer interface
Am 11.11.2011 17:47, schrieb Stefan Hajnoczi:
Quoted 7 lines
For some reason
vvfat.cinvokes BlockDriver functions directly and does not go throughblock.hfunctions. Change all direct calls except for the .bdrv_make_empty() call for which there is no public interface.This change makes the conversion to .bdrv_co_is_allocated() possible.
Signed-off-by Stefan Hajnoczi <[email protected]>
This conflicts with the block branch (I made more or less the same change in order to fix read-write vvfat disks)
Kevin
Re: [Qemu-devel] [PATCH 05/10] vvfat: use public block layer interface
Quoted 11 lines · Kevin Wolf
Am 11.11.2011 17:47, schrieb Stefan Hajnoczi:
Quoted 7 lines
For some reason
vvfat.cinvokes BlockDriver functions directly and does not go throughblock.hfunctions. Change all direct calls except for the .bdrv_make_empty() call for which there is no public interface.This change makes the conversion to .bdrv_co_is_allocated() possible.
Signed-off-by Stefan Hajnoczi <[email protected]>This conflicts with the block branch (I made more or less the same change in order to fix read-write vvfat disks)
Okay, I'll rebase on your block branch.
Stefan
PATCH 6/10 vvfat: convert to .bdrv_co_is_allocated()
In reply to Stefan HajnocziIt is trivial to switch from the synchronous .bdrv_is_allocated() interface to .bdrv_co_is_allocated() since vvfat_is_allocated() does not block.
--- block/vvfat.c | 4 ++-- 1 files changed, 2 insertions(+), 2 deletions(-)
block/vvfat.c +2 -2
@@ -2741,7 +2741,7 @@ static coroutine_fn int vvfat_co_write(BlockDriverState *bs, int64_t sector_num,return ret; } -static int vvfat_is_allocated (BlockDriverState *bs, +static int coroutine_fn vvfat_co_is_allocated(BlockDriverState *bs,int64_t sector_num,int nb_sectors,int * n) {BDRVVVFATState * s = bs->opaque;
@@ -2833,7 +2833,7 @@ static BlockDriver bdrv_vvfat = { .bdrv_read = vvfat_co_read, .bdrv_write = vvfat_co_write, .bdrv_close = vvfat_close, - .bdrv_is_allocated = vvfat_is_allocated, + .bdrv_co_is_allocated = vvfat_co_is_allocated, .protocol_name = "fat", };
Signature · 1 line
1.7.7.1
PATCH 7/10 vdi: convert to .bdrv_co_is_allocated()
In reply to Stefan HajnocziIt is trivial to switch from the synchronous .bdrv_is_allocated() interface to .bdrv_co_is_allocated() since vdi_is_allocated() does not block.
--- block/vdi.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
block/vdi.c +3 -3
@@ -463,8 +463,8 @@ static int vdi_open(BlockDriverState *bs, int flags)return -1 ; } -static int vdi_is_allocated (BlockDriverState *bs,int64_t sector_num, -int nb_sectors,int *pnum) +static int coroutine_fn vdi_co_is_allocated(BlockDriverState *bs, +int64_t sector_num,int nb_sectors,int *pnum) {/* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */ BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
@@ -981,7 +981,7 @@ static BlockDriver bdrv_vdi = { .bdrv_close = vdi_close, .bdrv_create = vdi_create, .bdrv_co_flush = vdi_co_flush, - .bdrv_is_allocated = vdi_is_allocated, + .bdrv_co_is_allocated = vdi_co_is_allocated, .bdrv_make_empty = vdi_make_empty, .bdrv_aio_readv = vdi_aio_readv,
Signature · 1 line
1.7.7.1
PATCH 8/10 cow: convert to .bdrv_co_is_allocated()
In reply to Stefan HajnocziThe cow block driver does not keep internal state for cluster lookups. This means it is safe to perform cluster lookups in coroutine context without risk of race conditions that corrupt internal state.
--- block/cow.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
block/cow.c +4 -4
@@ -132,8 +132,8 @@ static inline int is_bit_set(BlockDriverState *bs, int64_t bitnum)/* Return true if first block has been changed (ie. current version is * in COW file). Set the number of continuous blocks for which that * is true. */ -static int cow_is_allocated (BlockDriverState *bs,int64_t sector_num, -int nb_sectors,int *num_same) +static int coroutine_fn cow_co_is_allocated(BlockDriverState *bs, +int64_t sector_num,int nb_sectors,int *num_same) {int changed;
@@ -178,7 +178,7 @@ static int cow_read(BlockDriverState *bs, int64_t sector_num,int ret, n;while (nb_sectors >0 ) { -if (cow_is_allocated (bs, sector_num, nb_sectors, &n)) { +if (bdrv_is_allocated (bs, sector_num, nb_sectors, &n)) { ret =bdrv_pread (bs->file, s->cow_sectors_offset + sector_num *512 , buf, n *512 );
@@ -335,7 +335,7 @@ static BlockDriver bdrv_cow = { .bdrv_close = cow_close, .bdrv_create = cow_create, .bdrv_co_flush = cow_co_flush, - .bdrv_is_allocated = cow_is_allocated, + .bdrv_co_is_allocated = cow_co_is_allocated, .create_options = cow_create_options, };
Signature · 1 line
1.7.7.1
PATCH 9/10 block: drop .bdrv_is_allocated() interface
In reply to Stefan HajnocziNow that all block drivers have been converted to .bdrv_co_is_allocated() we can drop .bdrv_is_allocated().
Note that the public bdrv_is_allocated() interface is still available but is in fact a synchronous wrapper around .bdrv_co_is_allocated().
--- block.c | 38 ++++++++++++++++++-------------------- block_int.h | 2 -- 2 files changed, 18 insertions(+), 22 deletions(-)
block.c +18 -20
@@ -1805,25 +1805,8 @@ static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque)int bdrv_is_allocated (BlockDriverState *bs,int64_t sector_num,int nb_sectors,int *pnum) { -int64_t n; -if (bs->drv->bdrv_co_is_allocated) { -Coroutine *co; -BdrvCoIsAllocatedData data = { - .bs = bs, - .sector_num = sector_num, - .nb_sectors = nb_sectors, - .pnum = pnum, - .done = false, - }; - - co =qemu_coroutine_create (bdrv_is_allocated_co_entry); -qemu_coroutine_enter (co, &data); -while (!data.done) { -qemu_aio_wait (); - } -return data.ret; - } -if (!bs->drv->bdrv_is_allocated) { +if (!bs->drv->bdrv_co_is_allocated) { +int64_t n;if (sector_num >= bs->total_sectors) { *pnum =0 ;return 0 ;
@@ -1832,7 +1815,22 @@ int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, *pnum = (n < nb_sectors) ? (n) : (nb_sectors);return 1 ; } -return bs->drv->bdrv_is_allocated (bs, sector_num, nb_sectors, pnum); + +Coroutine *co; +BdrvCoIsAllocatedData data = { + .bs = bs, + .sector_num = sector_num, + .nb_sectors = nb_sectors, + .pnum = pnum, + .done = false, + }; + + co =qemu_coroutine_create (bdrv_is_allocated_co_entry); +qemu_coroutine_enter (co, &data); +while (!data.done) { +qemu_aio_wait (); + } +return data.ret; } void bdrv_mon_event(const BlockDriverState *bdrv,
block_int.h +0 -2
@@ -63,8 +63,6 @@ struct BlockDriver { const uint8_t *buf, int nb_sectors);void (*bdrv_close)(BlockDriverState *bs);int (*bdrv_create)(const char *filename,QEMUOptionParameter *options); -int (*bdrv_is_allocated)(BlockDriverState *bs,int64_t sector_num, -int nb_sectors,int *pnum);int (*bdrv_set_key)(BlockDriverState *bs,const char *key);int (*bdrv_make_empty)(BlockDriverState *bs);/* aio */
Signature · 1 line
1.7.7.1
PATCH 10/10 block: add bdrv_co_is_allocated() interface
In reply to Stefan HajnocziThis patch introduce the public bdrv_co_is_allocated() interface which can be used to query image allocation status while the VM is running.
--- block.c | 37 ++++++++++++++++++++++++------------- block.h | 2 ++ 2 files changed, 26 insertions(+), 13 deletions(-)
block.c +24 -13
@@ -1780,17 +1780,6 @@ typedef struct BdrvCoIsAllocatedData {bool done; } BdrvCoIsAllocatedData; -/* Coroutine wrapper for bdrv_is_allocated() */ -static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) -{ -BdrvCoIsAllocatedData *data = opaque; -BlockDriverState *bs = data->bs; - - data->ret = bs->drv->bdrv_co_is_allocated (bs, data->sector_num, - data->nb_sectors, data->pnum); - data->done = true; -} -/* * Returns true iff the specified sector is present in the disk image. Drivers * not implementing the functionality are assumed to not support backing files,
@@ -1802,8 +1791,8 @@ static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque)* * 'nb_sectors' is the max value 'pnum' should be set to. */ -int bdrv_is_allocated (BlockDriverState *bs,int64_t sector_num,int nb_sectors, -int *pnum) +int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num, +int nb_sectors,int *pnum) {if (!bs->drv->bdrv_co_is_allocated) {int64_t n;
@@ -1816,6 +1805,28 @@ int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,return 1 ; } +return bs->drv->bdrv_co_is_allocated (bs, sector_num, nb_sectors, pnum); +} + +/* Coroutine wrapper for bdrv_is_allocated() */ +static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) +{ +BdrvCoIsAllocatedData *data = opaque; +BlockDriverState *bs = data->bs; + + data->ret =bdrv_co_is_allocated (bs, data->sector_num, data->nb_sectors, + data->pnum); + data->done = true; +} + +/* +* Synchronous wrapper around bdrv_co_is_allocated(). +* +* See bdrv_co_is_allocated() for details. +*/ +int bdrv_is_allocated (BlockDriverState *bs,int64_t sector_num,int nb_sectors, +int *pnum) +{Coroutine *co;BdrvCoIsAllocatedData data = { .bs = bs,
block.h +2 -0
@@ -128,6 +128,8 @@ int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, int nb_sectors, QEMUIOVector *qiov); int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,int nb_sectors,QEMUIOVector *qiov); +int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num, +int nb_sectors,int *pnum);int bdrv_truncate (BlockDriverState *bs,int64_t offset);int64_t bdrv_getlength (BlockDriverState *bs);int64_t bdrv_get_allocated_file_size (BlockDriverState *bs);
Signature · 1 line
1.7.7.1