blockdev: (Nearly) free clean-up work
v4 cover blockdev: (Nearly) free clean-up work
After a lot has been restructed in the block layer in the past, we can now reap at least one of the fruits: Make bdrv_open() return a BDS!
v4:
- Rebased
- Patch 4:
- Fixed the comment above blk_new_open() (we are no longer calling
blk_new_with_bs())
- Dropped the blk_new_with_bs() declaration in sysemu/block-backend.h
- Patch 9: Added
git-backport-diff against v3:
Key:
[----] : patches are identical
[####] : number of functional differences between upstream/downstream patch [down] : patch is downstream-only
The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively
001/9:[----] [--] 'block: Drop useless bdrv_new() call' 002/9:[----] [--] 'block: Let bdrv_open_inherit() return the snapshot' 003/9:[----] [--] 'tests: Drop BDS from test-throttle.c' 004/9:[0003] [FC] 'block: Drop blk_new_with_bs()' 005/9:[----] [--] 'block: Drop bdrv_new_root()' 006/9:[----] [-C] 'block: Make bdrv_open() return a BDS' 007/9:[----] [--] 'block: Assert !bs->refcnt in bdrv_close()' 008/9:[----] [--] 'block: Drop bdrv_parent_cb_...() from bdrv_close()' 009/9:[down] 'block: Drop errp parameter from blk_new()'
Max Reitz (9):
block: Drop useless bdrv_new() call
block: Let bdrv_open_inherit() return the snapshot
tests: Drop BDS from test-throttle.c
block: Drop blk_new_with_bs()
block: Drop bdrv_new_root()
block: Make bdrv_open() return a BDS
block: Assert !bs->refcnt in bdrv_close()
block: Drop bdrv_parent_cb_...() from bdrv_close()
block: Drop errp parameter from blk_new()
block.c | 146 +++++++++++++++++------------------------ block/block-backend.c | 39 +++-------- block/vvfat.c | 8 +-- blockdev.c | 44 +++++-------- include/block/block.h | 5 +- include/sysemu/block-backend.h | 3 +- tests/test-throttle.c | 6 +- 7 files changed, 93 insertions(+), 158 deletions(-)
Signature · 1 line
2.8.2
PATCH v4 1/9 block: Drop useless bdrv_new() call
bdrv_append_temp_snapshot() uses bdrv_new() to create an empty BDS before invoking bdrv_open() on that BDS. This is probably a relict from when it used to do some modifications on that empty BDS, but now that is unnecessary, so we can just set bs_snapshot to NULL and let bdrv_open() do the rest.
--- block.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
block.c +1 -2
@@ -1470,8 +1470,7 @@ static int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags,qdict_put (snapshot_options,"driver" ,qstring_from_str ("qcow2" )); - bs_snapshot =bdrv_new (); - + bs_snapshot =NULL ; ret =bdrv_open (&bs_snapshot,NULL ,NULL , snapshot_options, flags, &local_err); snapshot_options =NULL ;
Signature · 1 line
2.8.2
Re: [Qemu-devel] [PATCH v4 1/9] block: Drop useless bdrv_new() call
Quoted 2 lines · Max Reitz
bdrv_append_temp_snapshot()usesbdrv_new()to create an empty BDS before invokingbdrv_open()on that BDS. This is probably a relict from
s/relict/relic/
Quoted 11 lines
when it used to do some modifications on that empty BDS, but now that is unnecessary, so we can just set
bs_snapshotto NULL and letbdrv_open()do the rest.Signed-off-by Max Reitz <[email protected]>Reviewed-by Alberto Garcia <[email protected]>Reviewed-by Kevin Wolf <[email protected]>--- block.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
Signature · 2 lines
Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
PATCH v4 2/9 block: Let bdrv_open_inherit() return the snapshot
In reply to Max ReitzIf bdrv_open_inherit() creates a snapshot BDS and *pbs is NULL, that snapshot BDS should be returned instead of the BDS under it.
This has worked so far because (nearly) all users of BDRV_O_SNAPSHOT use blk_new_open() to create the BDS tree. bdrv_append() (which is called by bdrv_append_temp_snapshot()) redirects pointers from parents (i.e. the BB in this case) to the newly appended child (i.e. the overlay), therefore, while bdrv_open_inherit() did not return the root BDS, the BB still pointed to it.
The only instance where BDRV_O_SNAPSHOT is used but blk_new_open() is not is in blockdev_init() if no BDS tree is created, and instead blk_new() is used and the flags are stored in the BB root state. However, qmp_blockdev_change_medium() filters the BDRV_O_SNAPSHOT flag before invoking bdrv_open(), so it will not have any effect.
In any case, it would be nicer if bdrv_open_inherit() could just always return the root of the BDS tree that has been created.
To this end, bdrv_append_temp_snapshot() now returns the snapshot BDS instead of just appending it on top of the snapshotted BDS. Also, it calls bdrv_ref() before bdrv_append() (which bdrv_open_inherit() has to undo if not returning the overlay).
--- block.c | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-)
block.c +40 -7
@@ -1422,8 +1422,10 @@ done:return c; } -static int bdrv_append_temp_snapshot (BlockDriverState *bs,int flags, -QDict *snapshot_options,Error **errp) +static BlockDriverState *bdrv_append_temp_snapshot (BlockDriverState *bs, +int flags, +QDict *snapshot_options, +Error **errp) {/* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ char *tmp_filename =g_malloc0 (PATH_MAX +1 );
@@ -1439,7 +1441,6 @@ static int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags,/* Get the required size from the image */ total_size =bdrv_getlength (bs);if (total_size <0 ) { - ret = total_size;error_setg_errno (errp, -total_size,"Could not get image size" ); goto out; }
@@ -1479,12 +1480,19 @@ static int bdrv_append_temp_snapshot(BlockDriverState *bs, int flags, goto out; } +/* bdrv_append() consumes a strong reference to bs_snapshot (i.e. it will +* call bdrv_unref() on it), so in order to be able to return one, we have +* to increase bs_snapshot's refcount here */ +bdrv_ref (bs_snapshot);bdrv_append (bs_snapshot, bs); +g_free (tmp_filename); +return bs_snapshot; + out:QDECREF (snapshot_options);g_free (tmp_filename); -return ret; +return NULL ; }/*
@@ -1704,17 +1712,42 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename, }QDECREF (options); - *pbs = bs;/* For snapshot=on, create a temporary qcow2 overlay. bs points to the * temporary snapshot afterwards. */ if (snapshot_flags) { - ret =bdrv_append_temp_snapshot (bs, snapshot_flags, snapshot_options, - &local_err); +BlockDriverState *snapshot_bs; + snapshot_bs =bdrv_append_temp_snapshot (bs, snapshot_flags, + snapshot_options, &local_err); snapshot_options =NULL ;if (local_err) { + ret = -EINVAL ; goto close_and_fail; } +if (!*pbs) { +/* We are not going to return bs but the overlay on top of it +* (snapshot_bs); thus, we have to drop the strong reference to bs +* (which we obtained by calling bdrv_new()). bs will not be +* deleted, though, because the overlay still has a reference to it. +*/ +bdrv_unref (bs); + bs = snapshot_bs; + }else { +/* We are not going to return snapshot_bs, so we have to drop the +* strong reference to it (which was returned by +* bdrv_append_temp_snapshot()). snapshot_bs will not be deleted, +* though, because bdrv_append_temp_snapshot() made all parental +* references to bs (*pbs) point to snapshot_bs. +* In fact, if *pbs was not NULL, we are not going to return any new +* BDS. But we do not need to decrement bs's refcount here as is +* done above, because with a non-NULL *pbs this function never even +* had a strong reference to bs. */ +bdrv_unref (snapshot_bs); + } + } + +if (!*pbs) { + *pbs = bs; }return 0 ;
Signature · 1 line
2.8.2
PATCH v4 3/9 tests: Drop BDS from test-throttle.c
In reply to Max ReitzNow that throttling has been moved to the BlockBackend level, we do not need to create a BDS along with the BB in the I/O throttling test.
--- tests/test-throttle.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
tests/test-throttle.c +3 -3
@@ -578,9 +578,9 @@ static void test_groups(void)BlockBackend *blk1, *blk2, *blk3;BlockBackendPublic *blkp1, *blkp2, *blkp3; - blk1 =blk_new_with_bs (&error_abort); - blk2 =blk_new_with_bs (&error_abort); - blk3 =blk_new_with_bs (&error_abort); + blk1 =blk_new (&error_abort); + blk2 =blk_new (&error_abort); + blk3 =blk_new (&error_abort); blkp1 =blk_get_public (blk1); blkp2 =blk_get_public (blk2);
Signature · 1 line
2.8.2
PATCH v4 4/9 block: Drop blk_new_with_bs()
In reply to Max ReitzIts only caller is blk_new_open(), so we can just inline it there.
The bdrv_new_root() call is dropped in the process because we can just let bdrv_open() create the BDS.
--- block/block-backend.c | 30 +++++++----------------------- include/sysemu/block-backend.h | 1 - 2 files changed, 7 insertions(+), 24 deletions(-)
block/block-backend.c +7 -23
@@ -136,27 +136,7 @@ BlockBackend *blk_new(Error **errp) }/* -* Create a new BlockBackend with a new BlockDriverState attached. -* Otherwise just like blk_new(), which see. -*/ -BlockBackend *blk_new_with_bs (Error **errp) -{ -BlockBackend *blk; -BlockDriverState *bs; - - blk =blk_new (errp); -if (!blk) { -return NULL ; - } - - bs =bdrv_new_root (); - blk->root =bdrv_root_attach_child (bs,"root" , &child_root); - blk->root->opaque = blk; -return blk; -} - -/* -* Calls blk_new_with_bs() and then calls bdrv_open() on the BlockDriverState. +* Creates a new BlockBackend, opens a new BlockDriverState, and connects both. * * Just as with bdrv_open(), after having called this function the reference to * @options belongs to the block layer (even on failure).
@@ -171,21 +151,25 @@ BlockBackend *blk_new_open(const char *filename, const char *reference, QDict *options, int flags, Error **errp) { BlockBackend *blk; +BlockDriverState *bs;int ret; - blk =blk_new_with_bs (errp); + blk =blk_new (errp);if (!blk) {QDECREF (options);return NULL ; } - ret =bdrv_open (&blk->root->bs, filename, reference, options, flags, errp); + bs =NULL ; + ret =bdrv_open (&bs, filename, reference, options, flags, errp);if (ret <0 ) {blk_unref (blk);return NULL ; }blk_set_enable_write_cache (blk, true); + blk->root =bdrv_root_attach_child (bs,"root" , &child_root); + blk->root->opaque = blk;return blk; }
include/sysemu/block-backend.h +0 -1
@@ -79,7 +79,6 @@ typedef struct BlockBackendPublic { } BlockBackendPublic;BlockBackend *blk_new (Error **errp); -BlockBackend *blk_new_with_bs (Error **errp);BlockBackend *blk_new_open (const char *filename,const char *reference,QDict *options,int flags,Error **errp);int blk_get_refcnt (BlockBackend *blk);
Signature · 1 line
2.8.2
PATCH v4 5/9 block: Drop bdrv_new_root()
In reply to Max ReitzIt is unused now, so we may just as well drop it.
--- block.c | 5 ----- include/block/block.h | 1 - 2 files changed, 6 deletions(-)
block.c +0 -5
@@ -220,11 +220,6 @@ void bdrv_register(BlockDriver *bdrv)QLIST_INSERT_HEAD (&bdrv_drivers, bdrv, list); } -BlockDriverState *bdrv_new_root (void ) -{ -return bdrv_new (); -} -BlockDriverState *bdrv_new (void ) {BlockDriverState *bs;
include/block/block.h +0 -1
@@ -198,7 +198,6 @@ BlockDriver *bdrv_find_format(const char *format_name);int bdrv_create (BlockDriver *drv,const char * filename,QemuOpts *opts,Error **errp);int bdrv_create_file (const char *filename,QemuOpts *opts,Error **errp); -BlockDriverState *bdrv_new_root (void );BlockDriverState *bdrv_new (void );void bdrv_append (BlockDriverState *bs_new,BlockDriverState *bs_top); void bdrv_replace_in_backing_chain(BlockDriverState *old,
Signature · 1 line
2.8.2
PATCH v4 6/9 block: Make bdrv_open() return a BDS
In reply to Max ReitzThere are no callers to bdrv_open() or bdrv_open_inherit() left that pass a pointer to a non-NULL BDS pointer as the first argument of these functions, so we can finally drop that parameter and just make them return the new BDS.
Generally, the following pattern is applied:
bs = NULL;
ret = bdrv_open(&bs, ..., &local_err);
if (ret < 0) {
error_propagate(errp, local_err);
...
}by
bs = bdrv_open(..., errp);
if (!bs) {
ret = -EINVAL;
...
}Of course, there are only a few instances where the pattern is really pure.
--- block.c | 138 ++++++++++++++++---------------------------------- block/block-backend.c | 6 +-- block/vvfat.c | 8 +-- blockdev.c | 38 +++++--------- include/block/block.h | 4 +- 5 files changed, 66 insertions(+), 128 deletions(-)
block.c +44 -94
@@ -64,10 +64,12 @@ static QTAILQ_HEAD(, BlockDriverState) all_bdrv_states = static QLIST_HEAD(, BlockDriver) bdrv_drivers =QLIST_HEAD_INITIALIZER (bdrv_drivers); -static int bdrv_open_inherit (BlockDriverState **pbs,const char *filename, -const char *reference,QDict *options,int flags, -BlockDriverState *parent, -const BdrvChildRole *child_role,Error **errp); +static BlockDriverState *bdrv_open_inherit (const char *filename, +const char *reference, +QDict *options,int flags, +BlockDriverState *parent, +const BdrvChildRole *child_role, +Error **errp);/* If non-zero, use only whitelisted block drivers */ static int use_bdrv_whitelist;
@@ -1336,14 +1338,13 @@ int bdrv_open_backing_file(BlockDriverState *bs, QDict *parent_options,qdict_put (options,"driver" ,qstring_from_str (bs->backing_format)); } - backing_hd =NULL ; - ret =bdrv_open_inherit (&backing_hd, - *backing_filename ? backing_filename :NULL , - reference, options,0 , bs, &child_backing, - errp); -if (ret <0 ) { + backing_hd =bdrv_open_inherit (*backing_filename ? backing_filename :NULL , + reference, options,0 , bs, &child_backing, + errp); +if (!backing_hd) { bs->open_flags |=BDRV_O_NO_BACKING ;error_prepend (errp,"Could not open backing file: " ); + ret = -EINVAL ; goto free_exit; }
@@ -1383,7 +1384,6 @@ BdrvChild *bdrv_open_child(const char *filename,BdrvChild *c =NULL ;BlockDriverState *bs;QDict *image_options; -int ret;char *bdref_key_dot;const char *reference;
@@ -1403,10 +1403,9 @@ BdrvChild *bdrv_open_child(const char *filename, goto done; } - bs =NULL ; - ret =bdrv_open_inherit (&bs, filename, reference, image_options,0 , - parent, child_role, errp); -if (ret <0 ) { + bs =bdrv_open_inherit (filename, reference, image_options,0 , + parent, child_role, errp); +if (!bs) { goto done; }
@@ -1427,7 +1426,6 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,int64_t total_size;QemuOpts *opts =NULL ;BlockDriverState *bs_snapshot; -Error *local_err =NULL ;int ret;/* if snapshot, we create a temporary backing file and open it
@@ -1466,12 +1464,10 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,qdict_put (snapshot_options,"driver" ,qstring_from_str ("qcow2" )); - bs_snapshot =NULL ; - ret =bdrv_open (&bs_snapshot,NULL ,NULL , snapshot_options, - flags, &local_err); + bs_snapshot =bdrv_open (NULL ,NULL , snapshot_options, flags, errp); snapshot_options =NULL ; -if (ret <0 ) { -error_propagate (errp, local_err); +if (!bs_snapshot) { + ret = -EINVAL ; goto out; }
@@ -1505,10 +1501,12 @@ out:* should be opened. If specified, neither options nor a filename may be given, * nor can an existing BDS be reused (that is, *pbs has to be NULL). */ -static int bdrv_open_inherit (BlockDriverState **pbs,const char *filename, -const char *reference,QDict *options,int flags, -BlockDriverState *parent, -const BdrvChildRole *child_role,Error **errp) +static BlockDriverState *bdrv_open_inherit (const char *filename, +const char *reference, +QDict *options,int flags, +BlockDriverState *parent, +const BdrvChildRole *child_role, +Error **errp) {int ret;BdrvChild *file =NULL ;
@@ -1520,7 +1518,6 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename,QDict *snapshot_options =NULL ;int snapshot_flags =0 ; -assert (pbs);assert (!child_role || !flags);assert (!child_role == !parent);
@@ -1528,33 +1525,22 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename,bool options_non_empty = options ?qdict_size (options) : false;QDECREF (options); -if (*pbs) { -error_setg (errp,"Cannot reuse an existing BDS when referencing " -"another block device" ); -return -EINVAL ; - } -if (filename || options_non_empty) {error_setg (errp,"Cannot reference an existing block device with " "additional options or a new filename" ); -return -EINVAL ; +return NULL ; } bs =bdrv_lookup_bs (reference, reference, errp);if (!bs) { -return -ENODEV ; +return NULL ; }bdrv_ref (bs); - *pbs = bs; -return 0 ; +return bs; } -if (*pbs) { - bs = *pbs; - }else { - bs =bdrv_new (); - } + bs =bdrv_new ();/* NULL means an empty set of options */ if (options ==NULL ) {
@@ -1564,7 +1550,6 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename,/* json: syntax counts as explicit options, as if in the QDict */ parse_json_protocol (options, &filename, &local_err);if (local_err) { - ret = -EINVAL ; goto fail; }
@@ -1591,7 +1576,6 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename, drv =bdrv_find_format (drvname);if (!drv) {error_setg (errp,"Unknown driver: '%s'" , drvname); - ret = -EINVAL ; goto fail; } }
@@ -1621,7 +1605,6 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename, file =bdrv_open_child (filename, options,"file" , bs, &child_file, true, &local_err);if (local_err) { - ret = -EINVAL ; goto fail; } }
@@ -1648,7 +1631,6 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename,qdict_put (options,"driver" ,qstring_from_str (drv->format_name)); }else if (!drv) {error_setg (errp,"Must specify either driver or file" ); - ret = -EINVAL ; goto fail; }
@@ -1691,7 +1673,6 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename, drv->format_name, entry->key); } - ret = -EINVAL ; goto close_and_fail; }
@@ -1702,7 +1683,6 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename, && !runstate_check(RUN_STATE_PAUSED)) { /* HACK */error_setg (errp,"Guest must be stopped for opening of encrypted image" ); - ret = -EBUSY ; goto close_and_fail; }
@@ -1716,36 +1696,17 @@ static int bdrv_open_inherit(BlockDriverState **pbs, const char *filename, snapshot_options, &local_err); snapshot_options =NULL ;if (local_err) { - ret = -EINVAL ; goto close_and_fail; } -if (!*pbs) { -/* We are not going to return bs but the overlay on top of it -* (snapshot_bs); thus, we have to drop the strong reference to bs -* (which we obtained by calling bdrv_new()). bs will not be -* deleted, though, because the overlay still has a reference to it. -*/ -bdrv_unref (bs); - bs = snapshot_bs; - }else { -/* We are not going to return snapshot_bs, so we have to drop the -* strong reference to it (which was returned by -* bdrv_append_temp_snapshot()). snapshot_bs will not be deleted, -* though, because bdrv_append_temp_snapshot() made all parental -* references to bs (*pbs) point to snapshot_bs. -* In fact, if *pbs was not NULL, we are not going to return any new -* BDS. But we do not need to decrement bs's refcount here as is -* done above, because with a non-NULL *pbs this function never even -* had a strong reference to bs. */ -bdrv_unref (snapshot_bs); - } - } - -if (!*pbs) { - *pbs = bs; +/* We are not going to return bs but the overlay on top of it +* (snapshot_bs); thus, we have to drop the strong reference to bs +* (which we obtained by calling bdrv_new()). bs will not be deleted, +* though, because the overlay still has a reference to it. */ +bdrv_unref (bs); + bs = snapshot_bs; } -return 0 ; +return bs; fail:if (file !=NULL ) {
@@ -1756,36 +1717,26 @@ fail:QDECREF (bs->options);QDECREF (options); bs->options =NULL ; -if (!*pbs) { -/* If *pbs is NULL, a new BDS has been created in this function and -needs to be freed now. Otherwise, it does not need to be closed, -since it has not really been opened yet. */ -bdrv_unref (bs); - } +bdrv_unref (bs);if (local_err) {error_propagate (errp, local_err); } -return ret; +return NULL ; close_and_fail: -/* See fail path, but now the BDS has to be always closed */ -if (*pbs) { -bdrv_close (bs); - }else { -bdrv_unref (bs); - } +bdrv_unref (bs);QDECREF (snapshot_options);QDECREF (options);if (local_err) {error_propagate (errp, local_err); } -return ret; +return NULL ; } -int bdrv_open (BlockDriverState **pbs,const char *filename, -const char *reference,QDict *options,int flags,Error **errp) +BlockDriverState *bdrv_open (const char *filename,const char *reference, +QDict *options,int flags,Error **errp) { -return bdrv_open_inherit (pbs, filename, reference, options, flags,NULL , +return bdrv_open_inherit (filename, reference, options, flags,NULL ,NULL , errp); }
@@ -3574,11 +3525,10 @@ void bdrv_img_create(const char *filename, const char *fmt, qstring_from_str(backing_fmt)); } - bs =NULL ; - ret =bdrv_open (&bs, full_backing,NULL , backing_options, - back_flags, &local_err); + bs =bdrv_open (full_backing,NULL , backing_options, back_flags, + &local_err);g_free (full_backing); -if (ret <0 ) { +if (!bs) { goto out; } size =bdrv_getlength (bs);
block/block-backend.c +2 -4
@@ -152,7 +152,6 @@ BlockBackend *blk_new_open(const char *filename, const char *reference, {BlockBackend *blk;BlockDriverState *bs; -int ret; blk =blk_new (errp);if (!blk) {
@@ -160,9 +159,8 @@ BlockBackend *blk_new_open(const char *filename, const char *reference,return NULL ; } - bs =NULL ; - ret =bdrv_open (&bs, filename, reference, options, flags, errp); -if (ret <0 ) { + bs =bdrv_open (filename, reference, options, flags, errp); +if (!bs) {blk_unref (blk);return NULL ; }
block/vvfat.c +4 -4
@@ -2997,12 +2997,12 @@ static int enable_write_target(BDRVVVFATState *s, Error **errp) goto err; } - s->qcow =NULL ; options =qdict_new ();qdict_put (options,"driver" ,qstring_from_str ("qcow" )); - ret =bdrv_open (&s->qcow, s->qcow_filename,NULL , options, -BDRV_O_RDWR |BDRV_O_NO_FLUSH , errp); -if (ret <0 ) { + s->qcow =bdrv_open (s->qcow_filename,NULL , options, +BDRV_O_RDWR |BDRV_O_NO_FLUSH , errp); +if (!s->qcow) { + ret = -EINVAL ; goto err; }
blockdev.c +14 -24
@@ -657,7 +657,6 @@ static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp)QemuOpts *opts;Error *local_error =NULL ;BlockdevDetectZeroesOptions detect_zeroes; -int ret;int bdrv_flags =0 ; opts =qemu_opts_create (&qemu_root_bds_opts,NULL ,1 , errp);
@@ -688,9 +687,8 @@ static BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp) bdrv_flags |=BDRV_O_INACTIVE ; } - bs =NULL ; - ret =bdrv_open (&bs,NULL ,NULL , bs_opts, bdrv_flags, errp); -if (ret <0 ) { + bs =bdrv_open (NULL ,NULL , bs_opts, bdrv_flags, errp); +if (!bs) { goto fail_no_bs_opts; }
@@ -1643,7 +1641,7 @@ typedef struct ExternalSnapshotState {static void external_snapshot_prepare (BlkActionState *common,Error **errp) { -int flags =0 , ret; +int flags =0 ;QDict *options =NULL ;Error *local_err =NULL ;/* Device and node name of the image to generate the snapshot from */
@@ -1768,11 +1766,10 @@ static void external_snapshot_prepare(BlkActionState *common, flags |=BDRV_O_NO_BACKING ; } -assert (state->new_bs ==NULL ); - ret =bdrv_open (&state->new_bs, new_image_file, snapshot_ref, options, - flags, errp); + state->new_bs =bdrv_open (new_image_file, snapshot_ref, options, flags, + errp);/* We will manually add the backing_hd field to the bs later */ -if (ret !=0 ) { +if (!state->new_bs) {return ; }
@@ -2511,7 +2508,7 @@ void qmp_blockdev_change_medium(const char *device, const char *filename, {BlockBackend *blk;BlockDriverState *medium_bs =NULL ; -int bdrv_flags, ret; +int bdrv_flags;QDict *options =NULL ;Error *err =NULL ;
@@ -2555,9 +2552,8 @@ void qmp_blockdev_change_medium(const char *device, const char *filename,qdict_put (options,"driver" ,qstring_from_str (format)); } -assert (!medium_bs); - ret =bdrv_open (&medium_bs, filename,NULL , options, bdrv_flags, errp); -if (ret <0 ) { + medium_bs =bdrv_open (filename,NULL , options, bdrv_flags, errp); +if (!medium_bs) { goto fail; }
@@ -3170,7 +3166,6 @@ static void do_drive_backup(const char *device, const char *target,Error *local_err =NULL ;int flags;int64_t size; -int ret;if (!has_speed) { speed =0 ;
@@ -3254,10 +3249,8 @@ static void do_drive_backup(const char *device, const char *target,qdict_put (options,"driver" ,qstring_from_str (format)); } - target_bs =NULL ; - ret =bdrv_open (&target_bs, target,NULL , options, flags, &local_err); -if (ret <0 ) { -error_propagate (errp, local_err); + target_bs =bdrv_open (target,NULL , options, flags, errp); +if (!target_bs) { goto out; }
@@ -3482,7 +3475,6 @@ void qmp_drive_mirror(const char *device, const char *target,QDict *options =NULL ;int flags;int64_t size; -int ret; blk =blk_by_name (device);if (!blk) {
@@ -3591,11 +3583,9 @@ void qmp_drive_mirror(const char *device, const char *target,/* Mirroring takes care of copy-on-write using the source's backing * file. */ - target_bs =NULL ; - ret =bdrv_open (&target_bs, target,NULL , options, - flags |BDRV_O_NO_BACKING , &local_err); -if (ret <0 ) { -error_propagate (errp, local_err); + target_bs =bdrv_open (target,NULL , options, flags |BDRV_O_NO_BACKING , + errp); +if (!target_bs) { goto out; }
include/block/block.h +2 -2
@@ -213,8 +213,8 @@ BdrvChild *bdrv_open_child(const char *filename,void bdrv_set_backing_hd (BlockDriverState *bs,BlockDriverState *backing_hd);int bdrv_open_backing_file (BlockDriverState *bs,QDict *parent_options,const char *bdref_key,Error **errp); -int bdrv_open (BlockDriverState **pbs,const char *filename, -const char *reference,QDict *options,int flags,Error **errp); +BlockDriverState *bdrv_open (const char *filename,const char *reference, +QDict *options,int flags,Error **errp);BlockReopenQueue *bdrv_reopen_queue (BlockReopenQueue *bs_queue,BlockDriverState *bs,QDict *options,int flags);
Signature · 1 line
2.8.2
PATCH v4 7/9 block: Assert !bs->refcnt in bdrv_close()
In reply to Max ReitzThe only caller of bdrv_close() left is bdrv_delete(). We may as well assert that, in a way (there are some things in bdrv_close() that make more sense under that assumption, such as the call to bdrv_release_all_dirty_bitmaps() which in turn assumes that no frozen bitmaps are attached to the BDS).
In addition, being called only in bdrv_delete() means that we can drop bdrv_close()'s forward declaration at the top of block.c.
--- block.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
block.c +1 -2
@@ -74,8 +74,6 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,/* If non-zero, use only whitelisted block drivers */ static int use_bdrv_whitelist; -static void bdrv_close (BlockDriverState *bs); -#ifdef _WIN32static int is_windows_drive_prefix (const char *filename) {
@@ -2110,6 +2108,7 @@ static void bdrv_close(BlockDriverState *bs)BdrvAioNotifier *ban, *ban_next;assert (!bs->job); +assert (!bs->refcnt);bdrv_drained_begin (bs);/* complete I/O */ bdrv_flush (bs);
Signature · 1 line
2.8.2
PATCH v4 8/9 block: Drop bdrv_parent_cb_...() from bdrv_close()
In reply to Max Reitzbdrv_close() now asserts that the BDS's refcount is 0, therefore it cannot have any parents and the bdrv_parent_cb_change_media() call is a no-op.
--- block.c | 2 -- 1 file changed, 2 deletions(-)
block.c +0 -2
@@ -2117,8 +2117,6 @@ static void bdrv_close(BlockDriverState *bs)bdrv_release_named_dirty_bitmaps (bs);assert (QLIST_EMPTY (&bs->dirty_bitmaps)); -bdrv_parent_cb_change_media (bs, false); -if (bs->drv) {BdrvChild *child, *next;
Signature · 1 line
2.8.2
PATCH v4 9/9 block: Drop errp parameter from blk_new()
In reply to Max Reitzblk_new() cannot fail so its Error ** parameter has become superfluous.
--- block/block-backend.c | 9 ++------- blockdev.c | 6 +----- include/sysemu/block-backend.h | 2 +- tests/test-throttle.c | 6 +++--- 4 files changed, 7 insertions(+), 16 deletions(-)
block/block-backend.c +2 -7
@@ -119,7 +119,7 @@ static const BdrvChildRole child_root = {* Store an error through @errp on failure, unless it's null. * Return the new BlockBackend on success, null on failure. */ -BlockBackend *blk_new (Error **errp) +BlockBackend *blk_new (void ) {BlockBackend *blk;
@@ -153,12 +153,7 @@ BlockBackend *blk_new_open(const char *filename, const char *reference,BlockBackend *blk;BlockDriverState *bs; - blk =blk_new (errp); -if (!blk) { -QDECREF (options); -return NULL ; - } - + blk =blk_new (); bs =bdrv_open (filename, reference, options, flags, errp);if (!bs) {blk_unref (blk);
blockdev.c +1 -5
@@ -567,11 +567,7 @@ static BlockBackend *blockdev_init(const char *file, QDict *bs_opts,if ((!file || !*file) && !qdict_size (bs_opts)) {BlockBackendRootState *blk_rs; - blk =blk_new (errp); -if (!blk) { - goto early_err; - } - + blk =blk_new (); blk_rs =blk_get_root_state (blk); blk_rs->open_flags = bdrv_flags; blk_rs->read_only = !(bdrv_flags &BDRV_O_RDWR );
include/sysemu/block-backend.h +1 -1
@@ -78,7 +78,7 @@ typedef struct BlockBackendPublic {QLIST_ENTRY (BlockBackendPublic) round_robin; } BlockBackendPublic; -BlockBackend *blk_new (Error **errp); +BlockBackend *blk_new (void );BlockBackend *blk_new_open (const char *filename,const char *reference,QDict *options,int flags,Error **errp);int blk_get_refcnt (BlockBackend *blk);
tests/test-throttle.c +3 -3
@@ -578,9 +578,9 @@ static void test_groups(void)BlockBackend *blk1, *blk2, *blk3;BlockBackendPublic *blkp1, *blkp2, *blkp3; - blk1 =blk_new (&error_abort); - blk2 =blk_new (&error_abort); - blk3 =blk_new (&error_abort); + blk1 =blk_new (); + blk2 =blk_new (); + blk3 =blk_new (); blkp1 =blk_get_public (blk1); blkp2 =blk_get_public (blk2);
Signature · 1 line
2.8.2
Kevin Wolf
AppliedAm 17.05.2016 um 16:41 hat Max Reitz geschrieben:
Quoted 2 lines
After a lot has been restructed in the block layer in the past, we can now reap at least one of the fruits: Make
bdrv_open()return a BDS!
Thanks, applied to the block branch.
Kevin