Skip to content

Commit

Permalink
block-backend: ignore inserted state in blk_co_nb_sectors
Browse files Browse the repository at this point in the history
All callers of blk_co_nb_sectors (and blk_nb_sectors) are able to
handle a non-inserted CD-ROM as a zero-length file, they do not need
to raise an error.

Not using blk_co_is_available() aligns the function with
blk_co_get_geometry(), which becomes a simple wrapper for
blk_co_nb_sectors().  It will also make it possible to skip the creation
of a coroutine in the (common) case where bs->bl.has_variable_length
is false.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Message-Id: <20230407153303.391121-8-pbonzini@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Reviewed-by: Kevin Wolf <kwolf@redhat.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
  • Loading branch information
bonzini authored and kevmw committed Apr 11, 2023
1 parent e5203a3 commit 9ed98ca
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions block/block-backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -1615,33 +1615,26 @@ int64_t coroutine_fn blk_co_getlength(BlockBackend *blk)
return bdrv_co_getlength(blk_bs(blk));
}

/* return 0 as number of sectors if no device present or error */
void coroutine_fn blk_co_get_geometry(BlockBackend *blk,
uint64_t *nb_sectors_ptr)
int64_t coroutine_fn blk_co_nb_sectors(BlockBackend *blk)
{
BlockDriverState *bs = blk_bs(blk);

IO_CODE();
GRAPH_RDLOCK_GUARD();

if (!bs) {
*nb_sectors_ptr = 0;
return -ENOMEDIUM;
} else {
int64_t nb_sectors = bdrv_co_nb_sectors(bs);
*nb_sectors_ptr = nb_sectors < 0 ? 0 : nb_sectors;
return bdrv_co_nb_sectors(bs);
}
}

int64_t coroutine_fn blk_co_nb_sectors(BlockBackend *blk)
/* return 0 as number of sectors if no device present or error */
void coroutine_fn blk_co_get_geometry(BlockBackend *blk,
uint64_t *nb_sectors_ptr)
{
IO_CODE();
GRAPH_RDLOCK_GUARD();

if (!blk_co_is_available(blk)) {
return -ENOMEDIUM;
}

return bdrv_co_nb_sectors(blk_bs(blk));
int64_t ret = blk_co_nb_sectors(blk);
*nb_sectors_ptr = ret < 0 ? 0 : ret;
}

BlockAIOCB *blk_aio_preadv(BlockBackend *blk, int64_t offset,
Expand Down

0 comments on commit 9ed98ca

Please sign in to comment.