Commit b6b8a333 authored by Paolo Bonzini's avatar Paolo Bonzini Committed by Stefan Hajnoczi
Browse files

block: introduce bdrv_get_block_status API



For now, bdrv_get_block_status is just another name for bdrv_is_allocated.
The next patches will add more flags.

This also touches all block drivers with a mostly mechanical rename.  The
sole exception is cow; because it calls cow_co_is_allocated from the read
code, we keep that function and make cow_co_get_block_status a wrapper.

Reviewed-by: default avatarEric Blake <eblake@redhat.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
Signed-off-by: default avatarStefan Hajnoczi <stefanha@redhat.com>
parent 11212d8f
Loading
Loading
Loading
Loading
+26 −20
Original line number Diff line number Diff line
@@ -3044,15 +3044,15 @@ int bdrv_has_zero_init(BlockDriverState *bs)
    return 0;
}

typedef struct BdrvCoIsAllocatedData {
typedef struct BdrvCoGetBlockStatusData {
    BlockDriverState *bs;
    BlockDriverState *base;
    int64_t sector_num;
    int nb_sectors;
    int *pnum;
    int ret;
    int64_t ret;
    bool done;
} BdrvCoIsAllocatedData;
} BdrvCoGetBlockStatusData;

/*
 * Returns true iff the specified sector is present in the disk image. Drivers
@@ -3069,7 +3069,7 @@ typedef struct BdrvCoIsAllocatedData {
 * 'nb_sectors' is the max value 'pnum' should be set to.  If nb_sectors goes
 * beyond the end of the disk image it will be clamped.
 */
static int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs,
static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs,
                                                     int64_t sector_num,
                                                     int nb_sectors, int *pnum)
{
@@ -3091,35 +3091,35 @@ static int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs,
        nb_sectors = n;
    }

    if (!bs->drv->bdrv_co_is_allocated) {
    if (!bs->drv->bdrv_co_get_block_status) {
        *pnum = nb_sectors;
        return 1;
    }

    return bs->drv->bdrv_co_is_allocated(bs, sector_num, nb_sectors, pnum);
    return bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum);
}

/* Coroutine wrapper for bdrv_is_allocated() */
static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque)
/* Coroutine wrapper for bdrv_get_block_status() */
static void coroutine_fn bdrv_get_block_status_co_entry(void *opaque)
{
    BdrvCoIsAllocatedData *data = opaque;
    BdrvCoGetBlockStatusData *data = opaque;
    BlockDriverState *bs = data->bs;

    data->ret = bdrv_co_is_allocated(bs, data->sector_num, data->nb_sectors,
    data->ret = bdrv_co_get_block_status(bs, data->sector_num, data->nb_sectors,
                                         data->pnum);
    data->done = true;
}

/*
 * Synchronous wrapper around bdrv_co_is_allocated().
 * Synchronous wrapper around bdrv_co_get_block_status().
 *
 * See bdrv_co_is_allocated() for details.
 * See bdrv_co_get_block_status() for details.
 */
int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
                      int *pnum)
int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num,
                              int nb_sectors, int *pnum)
{
    Coroutine *co;
    BdrvCoIsAllocatedData data = {
    BdrvCoGetBlockStatusData data = {
        .bs = bs,
        .sector_num = sector_num,
        .nb_sectors = nb_sectors,
@@ -3129,9 +3129,9 @@ int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,

    if (qemu_in_coroutine()) {
        /* Fast-path if already in coroutine context */
        bdrv_is_allocated_co_entry(&data);
        bdrv_get_block_status_co_entry(&data);
    } else {
        co = qemu_coroutine_create(bdrv_is_allocated_co_entry);
        co = qemu_coroutine_create(bdrv_get_block_status_co_entry);
        qemu_coroutine_enter(co, &data);
        while (!data.done) {
            qemu_aio_wait();
@@ -3140,6 +3140,12 @@ int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
    return data.ret;
}

int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num,
                                   int nb_sectors, int *pnum)
{
    return bdrv_get_block_status(bs, sector_num, nb_sectors, pnum);
}

/*
 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
 *
+7 −1
Original line number Diff line number Diff line
@@ -188,6 +188,12 @@ static int coroutine_fn cow_co_is_allocated(BlockDriverState *bs,
    return changed;
}

static int64_t coroutine_fn cow_co_get_block_status(BlockDriverState *bs,
        int64_t sector_num, int nb_sectors, int *num_same)
{
    return cow_co_is_allocated(bs, sector_num, nb_sectors, num_same);
}

static int cow_update_bitmap(BlockDriverState *bs, int64_t sector_num,
        int nb_sectors)
{
@@ -371,7 +377,7 @@ static BlockDriver bdrv_cow = {

    .bdrv_read              = cow_co_read,
    .bdrv_write             = cow_co_write,
    .bdrv_co_is_allocated   = cow_co_is_allocated,
    .bdrv_co_get_block_status   = cow_co_get_block_status,

    .create_options = cow_create_options,
};
+2 −2
Original line number Diff line number Diff line
@@ -395,7 +395,7 @@ static uint64_t get_cluster_offset(BlockDriverState *bs,
    return cluster_offset;
}

static int coroutine_fn qcow_co_is_allocated(BlockDriverState *bs,
static int64_t coroutine_fn qcow_co_get_block_status(BlockDriverState *bs,
        int64_t sector_num, int nb_sectors, int *pnum)
{
    BDRVQcowState *s = bs->opaque;
@@ -896,7 +896,7 @@ static BlockDriver bdrv_qcow = {

    .bdrv_co_readv          = qcow_co_readv,
    .bdrv_co_writev         = qcow_co_writev,
    .bdrv_co_is_allocated   = qcow_co_is_allocated,
    .bdrv_co_get_block_status   = qcow_co_get_block_status,

    .bdrv_set_key           = qcow_set_key,
    .bdrv_make_empty        = qcow_make_empty,
+2 −2
Original line number Diff line number Diff line
@@ -688,7 +688,7 @@ static int qcow2_reopen_prepare(BDRVReopenState *state,
    return 0;
}

static int coroutine_fn qcow2_co_is_allocated(BlockDriverState *bs,
static int64_t coroutine_fn qcow2_co_get_block_status(BlockDriverState *bs,
        int64_t sector_num, int nb_sectors, int *pnum)
{
    BDRVQcowState *s = bs->opaque;
@@ -1866,7 +1866,7 @@ static BlockDriver bdrv_qcow2 = {
    .bdrv_reopen_prepare  = qcow2_reopen_prepare,
    .bdrv_create        = qcow2_create,
    .bdrv_has_zero_init = bdrv_has_zero_init_1,
    .bdrv_co_is_allocated = qcow2_co_is_allocated,
    .bdrv_co_get_block_status = qcow2_co_get_block_status,
    .bdrv_set_key       = qcow2_set_key,
    .bdrv_make_empty    = qcow2_make_empty,

+2 −2
Original line number Diff line number Diff line
@@ -667,7 +667,7 @@ static void qed_is_allocated_cb(void *opaque, int ret, uint64_t offset, size_t l
    }
}

static int coroutine_fn bdrv_qed_co_is_allocated(BlockDriverState *bs,
static int64_t coroutine_fn bdrv_qed_co_get_block_status(BlockDriverState *bs,
                                                 int64_t sector_num,
                                                 int nb_sectors, int *pnum)
{
@@ -1575,7 +1575,7 @@ static BlockDriver bdrv_qed = {
    .bdrv_reopen_prepare      = bdrv_qed_reopen_prepare,
    .bdrv_create              = bdrv_qed_create,
    .bdrv_has_zero_init       = bdrv_has_zero_init_1,
    .bdrv_co_is_allocated     = bdrv_qed_co_is_allocated,
    .bdrv_co_get_block_status = bdrv_qed_co_get_block_status,
    .bdrv_make_empty          = bdrv_qed_make_empty,
    .bdrv_aio_readv           = bdrv_qed_aio_readv,
    .bdrv_aio_writev          = bdrv_qed_aio_writev,
Loading