From 0c51a893b643bc9393c685b47b9cea1e6831565f Mon Sep 17 00:00:00 2001 From: Eric Blake Date: Fri, 15 Jul 2016 17:22:51 -0600 Subject: [PATCH] block: Convert bdrv_discard() to byte-based Another step towards byte-based interfaces everywhere. Replace the sector-based bdrv_discard() with a new byte-based bdrv_pdiscard(), which silently ignores any unaligned head or tail. Signed-off-by: Eric Blake Reviewed-by: Stefan Hajnoczi Message-id: 1468624988-423-3-git-send-email-eblake@redhat.com Signed-off-by: Stefan Hajnoczi --- block/block-backend.c | 3 ++- block/io.c | 19 +++++++++---------- block/qcow2-refcount.c | 4 +--- include/block/block.h | 2 +- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/block/block-backend.c b/block/block-backend.c index d982cf9d29..83b6407ea6 100644 --- a/block/block-backend.c +++ b/block/block-backend.c @@ -1512,7 +1512,8 @@ int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors) return ret; } - return bdrv_discard(blk_bs(blk), sector_num, nb_sectors); + return bdrv_pdiscard(blk_bs(blk), sector_num << BDRV_SECTOR_BITS, + nb_sectors << BDRV_SECTOR_BITS); } int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf, diff --git a/block/io.c b/block/io.c index 14d448d50c..8d413058be 100644 --- a/block/io.c +++ b/block/io.c @@ -2391,16 +2391,15 @@ int bdrv_flush(BlockDriverState *bs) typedef struct DiscardCo { BlockDriverState *bs; - int64_t sector_num; - int nb_sectors; + int64_t offset; + int count; int ret; } DiscardCo; -static void coroutine_fn bdrv_discard_co_entry(void *opaque) +static void coroutine_fn bdrv_pdiscard_co_entry(void *opaque) { DiscardCo *rwco = opaque; - rwco->ret = bdrv_co_pdiscard(rwco->bs, rwco->sector_num << BDRV_SECTOR_BITS, - rwco->nb_sectors << BDRV_SECTOR_BITS); + rwco->ret = bdrv_co_pdiscard(rwco->bs, rwco->offset, rwco->count); } int coroutine_fn bdrv_co_pdiscard(BlockDriverState *bs, int64_t offset, @@ -2495,23 +2494,23 @@ out: return ret; } -int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) +int bdrv_pdiscard(BlockDriverState *bs, int64_t offset, int count) { Coroutine *co; DiscardCo rwco = { .bs = bs, - .sector_num = sector_num, - .nb_sectors = nb_sectors, + .offset = offset, + .count = count, .ret = NOT_DONE, }; if (qemu_in_coroutine()) { /* Fast-path if already in coroutine context */ - bdrv_discard_co_entry(&rwco); + bdrv_pdiscard_co_entry(&rwco); } else { AioContext *aio_context = bdrv_get_aio_context(bs); - co = qemu_coroutine_create(bdrv_discard_co_entry, &rwco); + co = qemu_coroutine_create(bdrv_pdiscard_co_entry, &rwco); qemu_coroutine_enter(co); while (rwco.ret == NOT_DONE) { aio_poll(aio_context, true); diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 49b6ce6bfd..cbfb3fe064 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -615,9 +615,7 @@ void qcow2_process_discards(BlockDriverState *bs, int ret) /* Discard is optional, ignore the return value */ if (ret >= 0) { - bdrv_discard(bs->file->bs, - d->offset >> BDRV_SECTOR_BITS, - d->bytes >> BDRV_SECTOR_BITS); + bdrv_pdiscard(bs->file->bs, d->offset, d->bytes); } g_free(d); diff --git a/include/block/block.h b/include/block/block.h index 4f5cebf15f..94cabbb685 100644 --- a/include/block/block.h +++ b/include/block/block.h @@ -341,7 +341,7 @@ void bdrv_drain(BlockDriverState *bs); void coroutine_fn bdrv_co_drain(BlockDriverState *bs); void bdrv_drain_all(void); -int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors); +int bdrv_pdiscard(BlockDriverState *bs, int64_t offset, int count); int bdrv_co_pdiscard(BlockDriverState *bs, int64_t offset, int count); int bdrv_has_zero_init_1(BlockDriverState *bs); int bdrv_has_zero_init(BlockDriverState *bs); -- 2.39.2