block: Block driver callbacks fixes
v5 cover block: Block driver callbacks fixes
This series makes implementing some of the bdrv_* callbacks easier for block filters by passing requests to bs->file if bs->drv doesn't implement it instead of failing, and adding default bdrv_co_get_block_status() implementations.
This is based against Kevin Wolf's block branch, commit da4bd74d2450ab72a7c26bbabb10c6a287dd043e
v5:
rebase against ced1484322 of https://github.com/XanClic/qemu.git block to fix apply conflicts
add suggested commit message in block: remove unused bdrv_media_changed add is_filter implication in commit message of block: remove bdrv_truncate callback in blkdebug
v4:
forward only for block filters
new patch: remove bdrv_media_changed
dropped commit block: Use defaults of bdrv_* callbacks in raw, since raw is not a filter driver and is incompatible with the changes.
v3:
minor changes by Eric Blake's suggestion new patch: remove bdrv_truncate method from blkdebug
v2:
do not pass to bs->file if bs->drv is NULL
move bs->file check outside of bdrv_inc_in_flight() area in bdrv_co_ioctl() new patch: remove duplicate code from block/raw-format.c
Manos Pitsidianakis (4):
block: pass bdrv_* methods to bs->file by default in block filters
block: remove unused bdrv_media_changed
block: remove bdrv_truncate callback in blkdebug
block: add default implementations for bdrv_co_get_block_status()
block.c | 35 +++++++++++++++++++---------------- block/blkdebug.c | 20 ++------------------ block/commit.c | 12 +----------- block/io.c | 26 ++++++++++++++++++++++++++ block/mirror.c | 12 +----------- block/raw-format.c | 6 ------ include/block/block.h | 1 - include/block/block_int.h | 25 +++++++++++++++++++++++-- 8 files changed, 72 insertions(+), 65 deletions(-)
Signature · 1 line
2.11.0
PATCH v5 1/4 block: pass bdrv_* methods to bs->file by default in block filters
The following functions fail if bs->drv is a filter and does not implement them:
bdrv_probe_blocksizesbdrv_probe_geometrybdrv_truncatebdrv_has_zero_initbdrv_get_info
Instead, the call should be passed to bs->file if it exists, to allow filter drivers to support those methods without implementing them. This commit makes drv->is_filter = true imply that these callbacks will be forwarded to bs->file by default, so disabling support for these functions must be done explicitly.
--- block.c | 21 +++++++++++++++++++-- include/block/block_int.h | 6 +++++- 2 files changed, 24 insertions(+), 3 deletions(-)
block.c +19 -2
@@ -494,6 +494,8 @@ int bdrv_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)if (drv && drv->bdrv_probe_blocksizes) {return drv->bdrv_probe_blocksizes (bs, bsz); + }else if (drv && drv->is_filter && bs->file) { +return bdrv_probe_blocksizes (bs->file->bs, bsz); }return -ENOTSUP ;
@@ -511,6 +513,8 @@ int bdrv_probe_geometry(BlockDriverState *bs, HDGeometry *geo)if (drv && drv->bdrv_probe_geometry) {return drv->bdrv_probe_geometry (bs, geo); + }else if (drv && drv->is_filter && bs->file) { +return bdrv_probe_geometry (bs->file->bs, geo); }return -ENOTSUP ;
@@ -3420,11 +3424,15 @@ int bdrv_truncate(BdrvChild *child, int64_t offset, PreallocMode prealloc,assert (child->perm &BLK_PERM_RESIZE ); +/* if bs->drv == NULL, bs is closed, so there's nothing to do here */ if (!drv) {error_setg (errp,"No medium inserted" );return -ENOMEDIUM ; }if (!drv->bdrv_truncate) { +if (bs->file && drv->is_filter) { +return bdrv_truncate (bs->file, prealloc, offset, errp); + }error_setg (errp,"Image format driver does not support resize" );return -ENOTSUP ; }
@@ -3761,6 +3769,9 @@ int bdrv_has_zero_init(BlockDriverState *bs)if (bs->drv->bdrv_has_zero_init) {return bs->drv->bdrv_has_zero_init (bs); } +if (bs->file && bs->drv->is_filter) { +return bdrv_has_zero_init (bs->file->bs); + }/* safe default */ return 0 ;
@@ -3815,10 +3826,16 @@ void bdrv_get_backing_filename(BlockDriverState *bs,int bdrv_get_info (BlockDriverState *bs,BlockDriverInfo *bdi) {BlockDriver *drv = bs->drv; -if (!drv) +/* if bs->drv == NULL, bs is closed, so there's nothing to do here */ +if (!drv) {return -ENOMEDIUM ; -if (!drv->bdrv_get_info) + } +if (!drv->bdrv_get_info) { +if (bs->file && drv->is_filter) { +return bdrv_get_info (bs->file->bs, bdi); + }return -ENOTSUP ; + }memset (bdi,0 ,sizeof (*bdi));return drv->bdrv_get_info (bs, bdi); }
include/block/block_int.h +5 -1
@@ -87,7 +87,11 @@ struct BlockDriver {const char *format_name;int instance_size; -/* set to true if the BlockDriver is a block filter */ +/* set to true if the BlockDriver is a block filter. Block filters pass +* certain callbacks that refer to data (see block.c) to their bs->file if +* the driver doesn't implement them. Drivers that do not wish to forward +* must implement them and return -ENOTSUP. +*/ bool is_filter;/* for snapshots block filter like Quorum can implement the * following recursive callback.
Signature · 1 line
2.11.0
Re: [Qemu-devel] [PATCH v5 1/4] block: pass bdrv_* methods to bs->file by default in block filters
Quoted 9 lines · Manos Pitsidianakis
The following functions fail if
bs->drvis a filter and does not implement them:
bdrv_probe_blocksizesbdrv_probe_geometrybdrv_truncatebdrv_has_zero_initbdrv_get_info
Quoted 7 lines
Instead, the call should be passed to
bs->fileif it exists, to allow filter drivers to support those methods without implementing them. This commit makesdrv->is_filter = trueimply that these callbacks will be forwarded tobs->fileby default, so disabling support for these functions must be done explicitly.Reviewed-by Eric Blake <[email protected]>
I would have dropped R-b on the grounds that you added comments to 'is_filter' (good! but non-trivial), to make sure it gets a grammar/wording review of the new content. Furthermore, your patch had a semantic change due to the rebase, and looking at that change...
Quoted 6 lines
Reviewed-by Stefan Hajnoczi <[email protected]>Signed-off-by Manos Pitsidianakis <[email protected]>---
block.c +0
-0@@ -3420,11 +3424,15 @@ int bdrv_truncate(BdrvChild *child, int64_t offset, PreallocMode prealloc,
block.c int bdrv_truncate(BdrvChild *child, int64_t offset, PreallocMode prealloc, · line 3434
Quoted 10 lines
block.c +3
-0assert (child->perm &BLK_PERM_RESIZE ); +/* if bs->drv == NULL, bs is closed, so there's nothing to do here */ if (!drv) {error_setg (errp,"No medium inserted" );return -ENOMEDIUM ; }if (!drv->bdrv_truncate) { +if (bs->file && drv->is_filter) { +return bdrv_truncate (bs->file, prealloc, offset, errp);
...ouch - you got it wrong. s/prealloc, offset/offset, prealloc/
include/block/block_int.h struct BlockDriver { · line 95
Quoted 12 lines
include/block/block_int.h +6
-1+++ b/include/block/block_int.h@@ -87,7 +87,11 @@ struct BlockDriver {const char *format_name;int instance_size; -/* set to true if the BlockDriver is a block filter */ +/* set to true if the BlockDriver is a block filter. Block filters pass +* certain callbacks that refer to data (see block.c) to their bs->file if +* the driver doesn't implement them. Drivers that do not wish to forward +* must implement them and return -ENOTSUP. +*/ bool is_filter;
I'm okay with the new comment.
With the swapped parameter order fixed (the maintainer can probably do that),
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
Re: [Qemu-devel] [PATCH v5 1/4] block: pass bdrv_* methods to bs->file by default in block filters
Quoted 57 lines · Eric Blake
Quoted 9 lines · Manos Pitsidianakis
The following functions fail if
bs->drvis a filter and does not implement them:
bdrv_probe_blocksizesbdrv_probe_geometrybdrv_truncatebdrv_has_zero_initbdrv_get_infoQuoted 7 lines
Instead, the call should be passed to
bs->fileif it exists, to allow filter drivers to support those methods without implementing them. This commit makesdrv->is_filter = trueimply that these callbacks will be forwarded tobs->fileby default, so disabling support for these functions must be done explicitly.Reviewed-by Eric Blake <[email protected]>I would have dropped R-b on the grounds that you added comments to '
is_filter' (good! but non-trivial), to make sure it gets a grammar/wording review of the new content. Furthermore, your patch had a semantic change due to the rebase, and looking at that change...Quoted 6 lines
Reviewed-by Stefan Hajnoczi <[email protected]>Signed-off-by Manos Pitsidianakis <[email protected]>---
block.c +0
-0@@ -3420,11 +3424,15 @@ int bdrv_truncate(BdrvChild *child, int64_t offset, PreallocMode prealloc,block.c int bdrv_truncate(BdrvChild *child, int64_t offset, PreallocMode prealloc, · line 3434
Quoted 10 lines
block.c +3
-0assert (child->perm &BLK_PERM_RESIZE ); +/* if bs->drv == NULL, bs is closed, so there's nothing to do here */ if (!drv) {error_setg (errp,"No medium inserted" );return -ENOMEDIUM ; }if (!drv->bdrv_truncate) { +if (bs->file && drv->is_filter) { +return bdrv_truncate (bs->file, prealloc, offset, errp);...ouch - you got it wrong. s/prealloc, offset/offset, prealloc/
include/block/block_int.h struct BlockDriver { · line 95
Quoted 12 lines
include/block/block_int.h +6
-1+++ b/include/block/block_int.h@@ -87,7 +87,11 @@ struct BlockDriver {const char *format_name;int instance_size; -/* set to true if the BlockDriver is a block filter */ +/* set to true if the BlockDriver is a block filter. Block filters pass +* certain callbacks that refer to data (see block.c) to their bs->file if +* the driver doesn't implement them. Drivers that do not wish to forward +* must implement them and return -ENOTSUP. +*/ bool is_filter;I'm okay with the new comment.
Quoted 3 lines
With the swapped parameter order fixed (the maintainer can probably do that),
Reviewed-by Eric Blake <[email protected]>
Oh, this compiled correctly because PreallocMode is an enum. Sorry for the inconvenience, that was rushed. I will send a new version to be typical.
PATCH v5 2/4 block: remove unused bdrv_media_changed
In reply to Manos PitsidianakisThis function is not used anywhere, so remove it.
Markus Armbruster adds:
The i82078 floppy device model used to call bdrv_media_changed() to implement its media change bit when backed by a host floppy. This went away in 21fcf36 "fdc: simplify media change handling". Probably broke host floppy media change. Host floppy pass-through was dropped in commit f709623. bdrv_media_changed() has never been used for anything else. Remove it.
(Source is Message-ID: <[email protected]>)
--- block.c | 14 -------------- block/raw-format.c | 6 ------ include/block/block.h | 1 - include/block/block_int.h | 1 - 4 files changed, 22 deletions(-)
block.c +0 -14
@@ -4200,20 +4200,6 @@ bool bdrv_is_inserted(BlockDriverState *bs) }/** -* Return whether the media changed since the last call to this -* function, or -ENOTSUP if we don't know. Most drivers don't know. -*/ -int bdrv_media_changed (BlockDriverState *bs) -{ -BlockDriver *drv = bs->drv; - -if (drv && drv->bdrv_media_changed) { -return drv->bdrv_media_changed (bs); - } -return -ENOTSUP ; -} - -/** * If eject_flag is TRUE, eject the media. Otherwise, close the tray */ void bdrv_eject (BlockDriverState *bs,bool eject_flag)
block/raw-format.c +0 -6
@@ -372,11 +372,6 @@ static int raw_truncate(BlockDriverState *bs, int64_t offset,return bdrv_truncate (bs->file, offset, prealloc, errp); } -static int raw_media_changed (BlockDriverState *bs) -{ -return bdrv_media_changed (bs->file->bs); -} -static void raw_eject (BlockDriverState *bs,bool eject_flag) {bdrv_eject (bs->file->bs, eject_flag);
@@ -510,7 +505,6 @@ BlockDriver bdrv_raw = { .bdrv_refresh_limits = &raw_refresh_limits, .bdrv_probe_blocksizes = &raw_probe_blocksizes, .bdrv_probe_geometry = &raw_probe_geometry, - .bdrv_media_changed = &raw_media_changed, .bdrv_eject = &raw_eject, .bdrv_lock_medium = &raw_lock_medium, .bdrv_co_ioctl = &raw_co_ioctl,
include/block/block.h +0 -1
@@ -441,7 +441,6 @@ int bdrv_can_set_read_only(BlockDriverState *bs, bool read_only, Error **errp);int bdrv_set_read_only (BlockDriverState *bs,bool read_only,Error **errp);bool bdrv_is_sg (BlockDriverState *bs);bool bdrv_is_inserted (BlockDriverState *bs); -int bdrv_media_changed (BlockDriverState *bs);void bdrv_lock_medium (BlockDriverState *bs,bool locked);void bdrv_eject (BlockDriverState *bs,bool eject_flag);const char *bdrv_get_format_name (BlockDriverState *bs);
include/block/block_int.h +0 -1
@@ -249,7 +249,6 @@ struct BlockDriver {/* removable device specific */ bool (*bdrv_is_inserted)(BlockDriverState *bs); -int (*bdrv_media_changed)(BlockDriverState *bs);void (*bdrv_eject)(BlockDriverState *bs,bool eject_flag);void (*bdrv_lock_medium)(BlockDriverState *bs,bool locked);
Signature · 1 line
2.11.0
PATCH v5 3/4 block: remove bdrv_truncate callback in blkdebug
In reply to Manos PitsidianakisNow that bdrv_truncate is passed to bs->file by default, remove the callback from block/blkdebug.c and set is_filter to true.
--- block/blkdebug.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-)
block/blkdebug.c +1 -7
@@ -821,12 +821,6 @@ static int64_t blkdebug_getlength(BlockDriverState *bs)return bdrv_getlength (bs->file->bs); } -static int blkdebug_truncate (BlockDriverState *bs,int64_t offset, -PreallocMode prealloc,Error **errp) -{ -return bdrv_truncate (bs->file, offset, prealloc, errp); -} -static void blkdebug_refresh_filename (BlockDriverState *bs,QDict *options) {BDRVBlkdebugState *s = bs->opaque;
@@ -909,6 +903,7 @@ static BlockDriver bdrv_blkdebug = { .format_name = "blkdebug", .protocol_name = "blkdebug", .instance_size = sizeof(BDRVBlkdebugState), + .is_filter = true, .bdrv_parse_filename = blkdebug_parse_filename, .bdrv_file_open = blkdebug_open,
@@ -917,7 +912,6 @@ static BlockDriver bdrv_blkdebug = { .bdrv_child_perm = bdrv_filter_default_perms, .bdrv_getlength = blkdebug_getlength, - .bdrv_truncate = blkdebug_truncate, .bdrv_refresh_filename = blkdebug_refresh_filename, .bdrv_refresh_limits = blkdebug_refresh_limits,
Signature · 1 line
2.11.0
PATCH v5 4/4 block: add default implementations for bdrv_co_get_block_status()
In reply to Manos Pitsidianakisbdrv_co_get_block_status_from_file() and bdrv_co_get_block_status_from_backing() set *file to bs->file and bs->backing respectively, so that bdrv_co_get_block_status() can recurse to them. Future block drivers won't have to duplicate code to implement this.
--- block/blkdebug.c | 12 +----------- block/commit.c | 12 +----------- block/io.c | 26 ++++++++++++++++++++++++++ block/mirror.c | 12 +----------- include/block/block_int.h | 18 ++++++++++++++++++ 5 files changed, 47 insertions(+), 33 deletions(-)
block/blkdebug.c +1 -11
@@ -641,16 +641,6 @@ static int coroutine_fn blkdebug_co_pdiscard(BlockDriverState *bs,return bdrv_co_pdiscard (bs->file->bs, offset, bytes); } -static int64_t coroutine_fn blkdebug_co_get_block_status( -BlockDriverState *bs,int64_t sector_num,int nb_sectors,int *pnum, -BlockDriverState **file) -{ - *pnum = nb_sectors; - *file = bs->file->bs; -return BDRV_BLOCK_RAW |BDRV_BLOCK_OFFSET_VALID | - (sector_num <<BDRV_SECTOR_BITS ); -} -static void blkdebug_close (BlockDriverState *bs) {BDRVBlkdebugState *s = bs->opaque;
@@ -920,7 +910,7 @@ static BlockDriver bdrv_blkdebug = { .bdrv_co_flush_to_disk = blkdebug_co_flush, .bdrv_co_pwrite_zeroes = blkdebug_co_pwrite_zeroes, .bdrv_co_pdiscard = blkdebug_co_pdiscard, - .bdrv_co_get_block_status = blkdebug_co_get_block_status, + .bdrv_co_get_block_status = bdrv_co_get_block_status_from_file, .bdrv_debug_event = blkdebug_debug_event, .bdrv_debug_breakpoint = blkdebug_debug_breakpoint,
block/commit.c +1 -11
@@ -242,16 +242,6 @@ static int coroutine_fn bdrv_commit_top_preadv(BlockDriverState *bs,return bdrv_co_preadv (bs->backing, offset, bytes, qiov, flags); } -static int64_t coroutine_fn bdrv_commit_top_get_block_status( -BlockDriverState *bs,int64_t sector_num,int nb_sectors,int *pnum, -BlockDriverState **file) -{ - *pnum = nb_sectors; - *file = bs->backing->bs; -return BDRV_BLOCK_RAW |BDRV_BLOCK_OFFSET_VALID | - (sector_num <<BDRV_SECTOR_BITS ); -} -static void bdrv_commit_top_refresh_filename (BlockDriverState *bs,QDict *opts) {bdrv_refresh_filename (bs->backing->bs);
@@ -277,7 +267,7 @@ static void bdrv_commit_top_child_perm(BlockDriverState *bs, BdrvChild *c,static BlockDriver bdrv_commit_top = { .format_name ="commit_top" , .bdrv_co_preadv = bdrv_commit_top_preadv, - .bdrv_co_get_block_status = bdrv_commit_top_get_block_status, + .bdrv_co_get_block_status = bdrv_co_get_block_status_from_backing, .bdrv_refresh_filename = bdrv_commit_top_refresh_filename, .bdrv_close = bdrv_commit_top_close, .bdrv_child_perm = bdrv_commit_top_child_perm,
block/io.c +26 -0
@@ -1690,6 +1690,32 @@ typedef struct BdrvCoGetBlockStatusData {bool done; } BdrvCoGetBlockStatusData; +int64_t coroutine_fn bdrv_co_get_block_status_from_file(BlockDriverState *bs, +int64_t sector_num, +int nb_sectors, +int *pnum, +BlockDriverState **file) +{ +assert (bs->file && bs->file->bs); + *pnum = nb_sectors; + *file = bs->file->bs; +return BDRV_BLOCK_RAW |BDRV_BLOCK_OFFSET_VALID | + (sector_num <<BDRV_SECTOR_BITS ); +} + +int64_t coroutine_fn bdrv_co_get_block_status_from_backing(BlockDriverState *bs, +int64_t sector_num, +int nb_sectors, +int *pnum, +BlockDriverState **file) +{ +assert (bs->backing && bs->backing->bs); + *pnum = nb_sectors; + *file = bs->backing->bs; +return BDRV_BLOCK_RAW |BDRV_BLOCK_OFFSET_VALID | + (sector_num <<BDRV_SECTOR_BITS ); +} +/* * Returns the allocation status of the specified sectors. * Drivers not implementing the functionality are assumed to not support
block/mirror.c +1 -11
@@ -1059,16 +1059,6 @@ static int coroutine_fn bdrv_mirror_top_flush(BlockDriverState *bs)return bdrv_co_flush (bs->backing->bs); } -static int64_t coroutine_fn bdrv_mirror_top_get_block_status( -BlockDriverState *bs,int64_t sector_num,int nb_sectors,int *pnum, -BlockDriverState **file) -{ - *pnum = nb_sectors; - *file = bs->backing->bs; -return BDRV_BLOCK_RAW |BDRV_BLOCK_OFFSET_VALID | - (sector_num <<BDRV_SECTOR_BITS ); -} - static int coroutine_fn bdrv_mirror_top_pwrite_zeroes(BlockDriverState *bs,int64_t offset,int bytes,BdrvRequestFlags flags) {
@@ -1115,7 +1105,7 @@ static BlockDriver bdrv_mirror_top = { .bdrv_co_pwrite_zeroes = bdrv_mirror_top_pwrite_zeroes, .bdrv_co_pdiscard = bdrv_mirror_top_pdiscard, .bdrv_co_flush = bdrv_mirror_top_flush, - .bdrv_co_get_block_status = bdrv_mirror_top_get_block_status, + .bdrv_co_get_block_status = bdrv_co_get_block_status_from_backing, .bdrv_refresh_filename = bdrv_mirror_top_refresh_filename, .bdrv_close = bdrv_mirror_top_close, .bdrv_child_perm = bdrv_mirror_top_child_perm,
include/block/block_int.h +18 -0
@@ -964,6 +964,24 @@ void bdrv_format_default_perms(BlockDriverState *bs, BdrvChild *c, uint64_t perm, uint64_t shared, uint64_t *nperm, uint64_t *nshared); +/* +* Default implementation for drivers to pass bdrv_co_get_block_status() to +* their file. +*/ +int64_t coroutine_fn bdrv_co_get_block_status_from_file(BlockDriverState *bs, +int64_t sector_num, +int nb_sectors, +int *pnum, +BlockDriverState **file); +/* +* Default implementation for drivers to pass bdrv_co_get_block_status() to +* their backing file. +*/ +int64_t coroutine_fn bdrv_co_get_block_status_from_backing(BlockDriverState *bs, +int64_t sector_num, +int nb_sectors, +int *pnum, +BlockDriverState **file);const char *bdrv_get_parent_name (const BlockDriverState *bs);void blk_dev_change_media_cb (BlockBackend *blk,bool load,Error **errp);bool blk_dev_has_removable_media (BlockBackend *blk);
Signature · 1 line
2.11.0
Quoted 6 lines · Manos Pitsidianakis
This series makes implementing some of the bdrv_* callbacks easier for block filters by passing requests to
bs->fileifbs->drvdoesn't implement it instead of failing, and adding defaultbdrv_co_get_block_status()implementations.This is based against Kevin Wolf's block branch, commit
da4bd74d2450ab72a7c26bbabb10c6a287dd043e
Not quite the case, since...
Quoted 4 lines
v5:
rebase againstced1484322of https://github.com/XanClic/qemu.git block to fix apply conflicts
...you rebased here instead ;) But thanks for doing that!
Signature · 3 lines
Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org