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 <eblake@redhat.com>
Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
Message-id: 1468624988-423-3-git-send-email-eblake@redhat.com
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
This commit is contained in:
Eric Blake 2016-07-15 17:22:51 -06:00 committed by Stefan Hajnoczi
parent 9f1963b3f7
commit 0c51a893b6
4 changed files with 13 additions and 15 deletions

View File

@ -1512,7 +1512,8 @@ int blk_discard(BlockBackend *blk, int64_t sector_num, int nb_sectors)
return ret; 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, int blk_save_vmstate(BlockBackend *blk, const uint8_t *buf,

View File

@ -2391,16 +2391,15 @@ int bdrv_flush(BlockDriverState *bs)
typedef struct DiscardCo { typedef struct DiscardCo {
BlockDriverState *bs; BlockDriverState *bs;
int64_t sector_num; int64_t offset;
int nb_sectors; int count;
int ret; int ret;
} DiscardCo; } DiscardCo;
static void coroutine_fn bdrv_discard_co_entry(void *opaque) static void coroutine_fn bdrv_pdiscard_co_entry(void *opaque)
{ {
DiscardCo *rwco = opaque; DiscardCo *rwco = opaque;
rwco->ret = bdrv_co_pdiscard(rwco->bs, rwco->sector_num << BDRV_SECTOR_BITS, rwco->ret = bdrv_co_pdiscard(rwco->bs, rwco->offset, rwco->count);
rwco->nb_sectors << BDRV_SECTOR_BITS);
} }
int coroutine_fn bdrv_co_pdiscard(BlockDriverState *bs, int64_t offset, int coroutine_fn bdrv_co_pdiscard(BlockDriverState *bs, int64_t offset,
@ -2495,23 +2494,23 @@ out:
return ret; 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; Coroutine *co;
DiscardCo rwco = { DiscardCo rwco = {
.bs = bs, .bs = bs,
.sector_num = sector_num, .offset = offset,
.nb_sectors = nb_sectors, .count = count,
.ret = NOT_DONE, .ret = NOT_DONE,
}; };
if (qemu_in_coroutine()) { if (qemu_in_coroutine()) {
/* Fast-path if already in coroutine context */ /* Fast-path if already in coroutine context */
bdrv_discard_co_entry(&rwco); bdrv_pdiscard_co_entry(&rwco);
} else { } else {
AioContext *aio_context = bdrv_get_aio_context(bs); 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); qemu_coroutine_enter(co);
while (rwco.ret == NOT_DONE) { while (rwco.ret == NOT_DONE) {
aio_poll(aio_context, true); aio_poll(aio_context, true);

View File

@ -615,9 +615,7 @@ void qcow2_process_discards(BlockDriverState *bs, int ret)
/* Discard is optional, ignore the return value */ /* Discard is optional, ignore the return value */
if (ret >= 0) { if (ret >= 0) {
bdrv_discard(bs->file->bs, bdrv_pdiscard(bs->file->bs, d->offset, d->bytes);
d->offset >> BDRV_SECTOR_BITS,
d->bytes >> BDRV_SECTOR_BITS);
} }
g_free(d); g_free(d);

View File

@ -341,7 +341,7 @@ void bdrv_drain(BlockDriverState *bs);
void coroutine_fn bdrv_co_drain(BlockDriverState *bs); void coroutine_fn bdrv_co_drain(BlockDriverState *bs);
void bdrv_drain_all(void); 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_co_pdiscard(BlockDriverState *bs, int64_t offset, int count);
int bdrv_has_zero_init_1(BlockDriverState *bs); int bdrv_has_zero_init_1(BlockDriverState *bs);
int bdrv_has_zero_init(BlockDriverState *bs); int bdrv_has_zero_init(BlockDriverState *bs);