From 1c95f7e1aff8417ff6e6cc23bc2d04fbcf79d37e Mon Sep 17 00:00:00 2001 From: Max Reitz Date: Mon, 26 Oct 2015 21:39:05 +0100 Subject: block: Add blk_remove_bs() This function removes the BlockDriverState associated with the given BlockBackend from that BB and sets the BDS pointer in the BB to NULL. Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- block/block-backend.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'block/block-backend.c') diff --git a/block/block-backend.c b/block/block-backend.c index 19fdaaec1a..878c448855 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -333,6 +333,18 @@ void blk_hide_on_behalf_of_hmp_drive_del(BlockBackend *blk) } } +/* + * Disassociates the currently associated BlockDriverState from @blk. + */ +void blk_remove_bs(BlockBackend *blk) +{ + blk_update_root_state(blk); + + blk->bs->blk = NULL; + bdrv_unref(blk->bs); + blk->bs = NULL; +} + /* * Associates a new BlockDriverState with @blk. */ -- cgit v1.2.3 From 38cb18f5b71428fb8a3e3759ac8fa21ac70cb1b5 Mon Sep 17 00:00:00 2001 From: Max Reitz Date: Mon, 26 Oct 2015 21:39:07 +0100 Subject: block: Add functions for inheriting a BBRS In order to open a BDS which inherits a BB's root state, blk_get_open_flags_from_root_state() is used to inquire the flags to be passed to bdrv_open(), and blk_apply_root_state() is used to apply the remaining state after the BDS has been opened. Signed-off-by: Max Reitz Signed-off-by: Kevin Wolf --- block/block-backend.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'block/block-backend.c') diff --git a/block/block-backend.c b/block/block-backend.c index 878c448855..7d495395de 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -1239,6 +1239,33 @@ void blk_update_root_state(BlockBackend *blk) } } +/* + * Applies the information in the root state to the given BlockDriverState. This + * does not include the flags which have to be specified for bdrv_open(), use + * blk_get_open_flags_from_root_state() to inquire them. + */ +void blk_apply_root_state(BlockBackend *blk, BlockDriverState *bs) +{ + bs->detect_zeroes = blk->root_state.detect_zeroes; + if (blk->root_state.throttle_group) { + bdrv_io_limits_enable(bs, blk->root_state.throttle_group); + } +} + +/* + * Returns the flags to be used for bdrv_open() of a BlockDriverState which is + * supposed to inherit the root state. + */ +int blk_get_open_flags_from_root_state(BlockBackend *blk) +{ + int bs_flags; + + bs_flags = blk->root_state.read_only ? 0 : BDRV_O_RDWR; + bs_flags |= blk->root_state.open_flags & ~BDRV_O_RDWR; + + return bs_flags; +} + BlockBackendRootState *blk_get_root_state(BlockBackend *blk) { return &blk->root_state; -- cgit v1.2.3 From f1f57066573e832438cd87600310589fa9cee202 Mon Sep 17 00:00:00 2001 From: Max Reitz Date: Mon, 26 Oct 2015 21:39:14 +0100 Subject: block: Inquire tray state before tray-moved events blk_dev_change_media_cb() is called for all potential tray movements; however, it is possible to request closing the tray but nothing actually happening (on a floppy disk drive without a medium). Thus, the actual tray status should be inquired before sending a tray-moved event (and an event should be sent whenever the status changed). Checking @load is now superfluous; it was necessary because it was possible to change a medium without having explicitly opened the tray and closed it again (or it might have been possible, at least). This is no longer possible, though. Signed-off-by: Max Reitz Reviewed-by: Eric Blake Reviewed-by: Kevin Wolf Signed-off-by: Kevin Wolf --- block/block-backend.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) (limited to 'block/block-backend.c') diff --git a/block/block-backend.c b/block/block-backend.c index 7d495395de..1ac69824ca 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -429,18 +429,15 @@ void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops, void blk_dev_change_media_cb(BlockBackend *blk, bool load) { if (blk->dev_ops && blk->dev_ops->change_media_cb) { - bool tray_was_closed = !blk_dev_is_tray_open(blk); + bool tray_was_open, tray_is_open; + tray_was_open = blk_dev_is_tray_open(blk); blk->dev_ops->change_media_cb(blk->dev_opaque, load); - if (tray_was_closed) { - /* tray open */ - qapi_event_send_device_tray_moved(blk_name(blk), - true, &error_abort); - } - if (load) { - /* tray close */ - qapi_event_send_device_tray_moved(blk_name(blk), - false, &error_abort); + tray_is_open = blk_dev_is_tray_open(blk); + + if (tray_was_open != tray_is_open) { + qapi_event_send_device_tray_moved(blk_name(blk), tray_is_open, + &error_abort); } } } -- cgit v1.2.3 From f636ae85f3db8ffb987c79715869dba1b8217e8a Mon Sep 17 00:00:00 2001 From: Alberto Garcia Date: Mon, 2 Nov 2015 16:51:54 +0200 Subject: block: Add blk_get_refcnt() This function returns the reference count of a given BlockBackend. For convenience, it returns 0 if the BlockBackend pointer is NULL. Signed-off-by: Alberto Garcia Reviewed-by: Max Reitz Message-id: dfdd8a17dbe3288842840636d2cfe5bb895abcb0.1446475331.git.berto@igalia.com Signed-off-by: Max Reitz --- block/block-backend.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'block/block-backend.c') diff --git a/block/block-backend.c b/block/block-backend.c index 1ac69824ca..6f9309fef4 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -189,6 +189,11 @@ static void drive_info_del(DriveInfo *dinfo) g_free(dinfo); } +int blk_get_refcnt(BlockBackend *blk) +{ + return blk ? blk->refcnt : 0; +} + /* * Increment @blk's reference count. * @blk must not be null. -- cgit v1.2.3