block: emulate .bdrv_flush() using .bdrv_aio_flush()
PATCH 1/1 block: emulate .bdrv_flush() using .bdrv_aio_flush()
Block drivers typically have two copies of the flush operation: a synchronous .bdrv_flush() and an asynchronous .bdrv_aio_flush(). This patch applies the same emulation that we already do for .bdrv_read()/.bdrv_write() to .bdrv_flush(). Now block drivers only need to provide either .bdrv_aio_flush() or, in the case of legacy drivers, .bdrv_flush().
--- block.c | 31 +++++++++++++++++++++++++++---- block/blkdebug.c | 6 ------ block/blkverify.c | 9 --------- block/qcow.c | 6 ------ block/qcow2.c | 19 ------------------- block/qed.c | 6 ------ block/raw-posix.c | 11 ----------- 7 files changed, 27 insertions(+), 61 deletions(-)
block.c +27 -4
@@ -59,6 +59,7 @@ static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sectors);static int bdrv_write_em (BlockDriverState *bs,int64_t sector_num,const uint8_t *buf,int nb_sectors); +static int bdrv_flush_em (BlockDriverState *bs);static BlockDriverAIOCB *bdrv_co_aio_readv_em (BlockDriverState *bs,int64_t sector_num,QEMUIOVector *qiov,int nb_sectors,BlockDriverCompletionFunc *cb,void *opaque);
@@ -205,8 +206,11 @@ void bdrv_register(BlockDriver *bdrv) } } -if (!bdrv->bdrv_aio_flush) +if (!bdrv->bdrv_aio_flush) { bdrv->bdrv_aio_flush = bdrv_aio_flush_em; + }else if (!bdrv->bdrv_flush) { + bdrv->bdrv_flush = bdrv_flush_em; + }QLIST_INSERT_HEAD (&bdrv_drivers, bdrv, list); }
@@ -2848,7 +2852,7 @@ static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,/**************************************************************/ /* sync block device emulation */ -static void bdrv_rw_em_cb (void *opaque,int ret) +static void bdrv_em_cb (void *opaque,int ret) { *(int *)opaque = ret; }
@@ -2868,7 +2872,7 @@ static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, iov.iov_len = nb_sectors *BDRV_SECTOR_SIZE ;qemu_iovec_init_external (&qiov, &iov,1 ); acb =bdrv_aio_readv (bs, sector_num, &qiov, nb_sectors, - bdrv_rw_em_cb, &async_ret); + bdrv_em_cb, &async_ret);if (acb ==NULL ) { async_ret =-1 ; goto fail;
@@ -2896,7 +2900,26 @@ static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, iov.iov_len = nb_sectors *BDRV_SECTOR_SIZE ;qemu_iovec_init_external (&qiov, &iov,1 ); acb =bdrv_aio_writev (bs, sector_num, &qiov, nb_sectors, - bdrv_rw_em_cb, &async_ret); + bdrv_em_cb, &async_ret); +if (acb ==NULL ) { + async_ret =-1 ; + goto fail; + } +while (async_ret ==NOT_DONE ) { +qemu_aio_wait (); + } + +fail: +return async_ret; +} + +static int bdrv_flush_em (BlockDriverState *bs) +{ +int async_ret; +BlockDriverAIOCB *acb; + + async_ret =NOT_DONE ; + acb =bdrv_aio_flush (bs, bdrv_em_cb, &async_ret);if (acb ==NULL ) { async_ret =-1 ; goto fail;
block/blkdebug.c +0 -6
@@ -397,11 +397,6 @@ static void blkdebug_close(BlockDriverState *bs) } } -static int blkdebug_flush (BlockDriverState *bs) -{ -return bdrv_flush (bs->file); -} -static BlockDriverAIOCB *blkdebug_aio_flush (BlockDriverState *bs,BlockDriverCompletionFunc *cb,void *opaque) {
@@ -454,7 +449,6 @@ static BlockDriver bdrv_blkdebug = { .bdrv_file_open = blkdebug_open, .bdrv_close = blkdebug_close, - .bdrv_flush = blkdebug_flush, .bdrv_aio_readv = blkdebug_aio_readv, .bdrv_aio_writev = blkdebug_aio_writev,
block/blkverify.c +0 -9
@@ -116,14 +116,6 @@ static void blkverify_close(BlockDriverState *bs) s->test_file =NULL ; } -static int blkverify_flush (BlockDriverState *bs) -{ -BDRVBlkverifyState *s = bs->opaque; - -/* Only flush test file, the raw file is not important */ -return bdrv_flush (s->test_file); -} -static int64_t blkverify_getlength (BlockDriverState *bs) {BDRVBlkverifyState *s = bs->opaque;
@@ -368,7 +360,6 @@ static BlockDriver bdrv_blkverify = { .bdrv_file_open = blkverify_open, .bdrv_close = blkverify_close, - .bdrv_flush = blkverify_flush, .bdrv_aio_readv = blkverify_aio_readv, .bdrv_aio_writev = blkverify_aio_writev,
block/qcow.c +0 -6
@@ -781,11 +781,6 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,return 0 ; } -static int qcow_flush (BlockDriverState *bs) -{ -return bdrv_flush (bs->file); -} -static BlockDriverAIOCB *qcow_aio_flush (BlockDriverState *bs,BlockDriverCompletionFunc *cb,void *opaque) {
@@ -826,7 +821,6 @@ static BlockDriver bdrv_qcow = { .bdrv_open = qcow_open, .bdrv_close = qcow_close, .bdrv_create = qcow_create, - .bdrv_flush = qcow_flush, .bdrv_is_allocated = qcow_is_allocated, .bdrv_set_key = qcow_set_key, .bdrv_make_empty = qcow_make_empty,
block/qcow2.c +0 -19
@@ -1092,24 +1092,6 @@ static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,return 0 ; } -static int qcow2_flush (BlockDriverState *bs) -{ -BDRVQcowState *s = bs->opaque; -int ret; - - ret =qcow2_cache_flush (bs, s->l2_table_cache); -if (ret <0 ) { -return ret; - } - - ret =qcow2_cache_flush (bs, s->refcount_block_cache); -if (ret <0 ) { -return ret; - } - -return bdrv_flush (bs->file); -} -static BlockDriverAIOCB *qcow2_aio_flush (BlockDriverState *bs,BlockDriverCompletionFunc *cb,void *opaque)
@@ -1242,7 +1224,6 @@ static BlockDriver bdrv_qcow2 = { .bdrv_open = qcow2_open, .bdrv_close = qcow2_close, .bdrv_create = qcow2_create, - .bdrv_flush = qcow2_flush, .bdrv_is_allocated = qcow2_is_allocated, .bdrv_set_key = qcow2_set_key, .bdrv_make_empty = qcow2_make_empty,
block/qed.c +0 -6
@@ -533,11 +533,6 @@ static void bdrv_qed_close(BlockDriverState *bs)qemu_vfree (s->l1_table); } -static int bdrv_qed_flush (BlockDriverState *bs) -{ -return bdrv_flush (bs->file); -} -static int qed_create (const char *filename,uint32_t cluster_size,uint64_t image_size,uint32_t table_size,const char *backing_file,const char *backing_fmt)
@@ -1479,7 +1474,6 @@ static BlockDriver bdrv_qed = { .bdrv_open = bdrv_qed_open, .bdrv_close = bdrv_qed_close, .bdrv_create = bdrv_qed_create, - .bdrv_flush = bdrv_qed_flush, .bdrv_is_allocated = bdrv_qed_is_allocated, .bdrv_make_empty = bdrv_qed_make_empty, .bdrv_aio_readv = bdrv_qed_aio_readv,
block/raw-posix.c +0 -11
@@ -836,12 +836,6 @@ static int raw_create(const char *filename, QEMUOptionParameter *options)return result; } -static int raw_flush (BlockDriverState *bs) -{ -BDRVRawState *s = bs->opaque; -return qemu_fdatasync (s->fd); -} -#ifdef CONFIG_XFS static int xfs_discard (BDRVRawState *s,int64_t sector_num,int nb_sectors) {
@@ -893,7 +887,6 @@ static BlockDriver bdrv_file = { .bdrv_write = raw_write, .bdrv_close = raw_close, .bdrv_create = raw_create, - .bdrv_flush = raw_flush, .bdrv_discard = raw_discard, .bdrv_aio_readv = raw_aio_readv,
@@ -1163,7 +1156,6 @@ static BlockDriver bdrv_host_device = { .bdrv_create = hdev_create, .create_options = raw_create_options, .bdrv_has_zero_init = hdev_has_zero_init, - .bdrv_flush = raw_flush, .bdrv_aio_readv = raw_aio_readv, .bdrv_aio_writev = raw_aio_writev,
@@ -1284,7 +1276,6 @@ static BlockDriver bdrv_host_floppy = { .bdrv_create = hdev_create, .create_options = raw_create_options, .bdrv_has_zero_init = hdev_has_zero_init, - .bdrv_flush = raw_flush, .bdrv_aio_readv = raw_aio_readv, .bdrv_aio_writev = raw_aio_writev,
@@ -1385,7 +1376,6 @@ static BlockDriver bdrv_host_cdrom = { .bdrv_create = hdev_create, .create_options = raw_create_options, .bdrv_has_zero_init = hdev_has_zero_init, - .bdrv_flush = raw_flush, .bdrv_aio_readv = raw_aio_readv, .bdrv_aio_writev = raw_aio_writev,
@@ -1506,7 +1496,6 @@ static BlockDriver bdrv_host_cdrom = { .bdrv_create = hdev_create, .create_options = raw_create_options, .bdrv_has_zero_init = hdev_has_zero_init, - .bdrv_flush = raw_flush, .bdrv_aio_readv = raw_aio_readv, .bdrv_aio_writev = raw_aio_writev,
Signature · 1 line
1.7.5.4
Quoted 6 lines · Stefan Hajnoczi
Block drivers typically have two copies of the flush operation: a synchronous .bdrv_flush() and an asynchronous .bdrv_aio_flush(). This patch applies the same emulation that we already do for .bdrv_read()/.bdrv_write() to .bdrv_flush(). Now block drivers only need to provide either .bdrv_aio_flush() or, in the case of legacy drivers, .bdrv_flush().
I had the same bug in my nbd improvements series, so thanks. After Kevin merges this and parts 1-7 (or 1-9 even) of that series I'll resend.
Paolo
Quoted 275 lines · Stefan Hajnoczi
Block drivers typically have two copies of the flush operation: a synchronous .bdrv_flush() and an asynchronous .bdrv_aio_flush(). This patch applies the same emulation that we already do for .bdrv_read()/.bdrv_write() to .bdrv_flush(). Now block drivers only need to provide either .bdrv_aio_flush() or, in the case of legacy drivers, .bdrv_flush().
Signed-off-by Stefan Hajnoczi<[email protected]>--- block.c | 31 +++++++++++++++++++++++++++---- block/blkdebug.c | 6 ------ block/blkverify.c | 9 --------- block/qcow.c | 6 ------ block/qcow2.c | 19 ------------------- block/qed.c | 6 ------ block/raw-posix.c | 11 ----------- 7 files changed, 27 insertions(+), 61 deletions(-)block.c +27
-4@@ -59,6 +59,7 @@ static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sectors);static int bdrv_write_em (BlockDriverState *bs,int64_t sector_num,const uint8_t *buf,int nb_sectors); +static int bdrv_flush_em (BlockDriverState *bs);static BlockDriverAIOCB *bdrv_co_aio_readv_em (BlockDriverState *bs,int64_t sector_num,QEMUIOVector *qiov,int nb_sectors,BlockDriverCompletionFunc *cb,void *opaque);@@ -205,8 +206,11 @@ void bdrv_register(BlockDriver *bdrv) } } -if (!bdrv->bdrv_aio_flush) +if (!bdrv->bdrv_aio_flush) { bdrv->bdrv_aio_flush = bdrv_aio_flush_em; + }else if (!bdrv->bdrv_flush) { + bdrv->bdrv_flush = bdrv_flush_em; + }QLIST_INSERT_HEAD (&bdrv_drivers, bdrv, list); }@@ -2848,7 +2852,7 @@ static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,/**************************************************************/ /* sync block device emulation */ -static void bdrv_rw_em_cb (void *opaque,int ret) +static void bdrv_em_cb (void *opaque,int ret) { *(int *)opaque = ret; }@@ -2868,7 +2872,7 @@ static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, iov.iov_len = nb_sectors *BDRV_SECTOR_SIZE ;qemu_iovec_init_external (&qiov,&iov,1 ); acb =bdrv_aio_readv (bs, sector_num,&qiov, nb_sectors, - bdrv_rw_em_cb,&async_ret); + bdrv_em_cb,&async_ret);if (acb ==NULL ) { async_ret =-1 ; goto fail;@@ -2896,7 +2900,26 @@ static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, iov.iov_len = nb_sectors *BDRV_SECTOR_SIZE ;qemu_iovec_init_external (&qiov,&iov,1 ); acb =bdrv_aio_writev (bs, sector_num,&qiov, nb_sectors, - bdrv_rw_em_cb,&async_ret); + bdrv_em_cb,&async_ret); +if (acb ==NULL ) { + async_ret =-1 ; + goto fail; + } +while (async_ret ==NOT_DONE ) { +qemu_aio_wait (); + } + +fail: +return async_ret; +} + +static int bdrv_flush_em (BlockDriverState *bs) +{ +int async_ret; +BlockDriverAIOCB *acb; + + async_ret =NOT_DONE ; + acb =bdrv_aio_flush (bs, bdrv_em_cb,&async_ret);if (acb ==NULL ) { async_ret =-1 ; goto fail;block/blkdebug.c +0
-6@@ -397,11 +397,6 @@ static void blkdebug_close(BlockDriverState *bs) } } -static int blkdebug_flush (BlockDriverState *bs) -{ -return bdrv_flush (bs->file); -} -static BlockDriverAIOCB *blkdebug_aio_flush (BlockDriverState *bs,BlockDriverCompletionFunc *cb,void *opaque) {@@ -454,7 +449,6 @@ static BlockDriver bdrv_blkdebug = { .bdrv_file_open = blkdebug_open, .bdrv_close = blkdebug_close, - .bdrv_flush = blkdebug_flush, .bdrv_aio_readv = blkdebug_aio_readv, .bdrv_aio_writev = blkdebug_aio_writev,block/blkverify.c +0
-9@@ -116,14 +116,6 @@ static void blkverify_close(BlockDriverState *bs) s->test_file =NULL ; } -static int blkverify_flush (BlockDriverState *bs) -{ -BDRVBlkverifyState *s = bs->opaque; - -/* Only flush test file, the raw file is not important */ -return bdrv_flush (s->test_file); -} -static int64_t blkverify_getlength (BlockDriverState *bs) {BDRVBlkverifyState *s = bs->opaque;@@ -368,7 +360,6 @@ static BlockDriver bdrv_blkverify = { .bdrv_file_open = blkverify_open, .bdrv_close = blkverify_close, - .bdrv_flush = blkverify_flush, .bdrv_aio_readv = blkverify_aio_readv, .bdrv_aio_writev = blkverify_aio_writev,block/qcow.c +0
-6@@ -781,11 +781,6 @@ static int qcow_write_compressed(BlockDriverState *bs, int64_t sector_num,return 0 ; } -static int qcow_flush (BlockDriverState *bs) -{ -return bdrv_flush (bs->file); -} -static BlockDriverAIOCB *qcow_aio_flush (BlockDriverState *bs,BlockDriverCompletionFunc *cb,void *opaque) {@@ -826,7 +821,6 @@ static BlockDriver bdrv_qcow = { .bdrv_open = qcow_open, .bdrv_close = qcow_close, .bdrv_create = qcow_create, - .bdrv_flush = qcow_flush, .bdrv_is_allocated = qcow_is_allocated, .bdrv_set_key = qcow_set_key, .bdrv_make_empty = qcow_make_empty,block/qcow2.c +0
-19@@ -1092,24 +1092,6 @@ static int qcow2_write_compressed(BlockDriverState *bs, int64_t sector_num,return 0 ; } -static int qcow2_flush (BlockDriverState *bs) -{ -BDRVQcowState *s = bs->opaque; -int ret; - - ret =qcow2_cache_flush (bs, s->l2_table_cache); -if (ret<0 ) { -return ret; - } - - ret =qcow2_cache_flush (bs, s->refcount_block_cache); -if (ret<0 ) { -return ret; - } - -return bdrv_flush (bs->file); -} -static BlockDriverAIOCB *qcow2_aio_flush (BlockDriverState *bs,BlockDriverCompletionFunc *cb,void *opaque)@@ -1242,7 +1224,6 @@ static BlockDriver bdrv_qcow2 = { .bdrv_open = qcow2_open, .bdrv_close = qcow2_close, .bdrv_create = qcow2_create, - .bdrv_flush = qcow2_flush, .bdrv_is_allocated = qcow2_is_allocated, .bdrv_set_key = qcow2_set_key, .bdrv_make_empty = qcow2_make_empty,block/qed.c +0
-6@@ -533,11 +533,6 @@ static void bdrv_qed_close(BlockDriverState *bs)qemu_vfree (s->l1_table); } -static int bdrv_qed_flush (BlockDriverState *bs) -{ -return bdrv_flush (bs->file); -} -static int qed_create (const char *filename,uint32_t cluster_size,uint64_t image_size,uint32_t table_size,const char *backing_file,const char *backing_fmt)@@ -1479,7 +1474,6 @@ static BlockDriver bdrv_qed = { .bdrv_open = bdrv_qed_open, .bdrv_close = bdrv_qed_close, .bdrv_create = bdrv_qed_create, - .bdrv_flush = bdrv_qed_flush, .bdrv_is_allocated = bdrv_qed_is_allocated, .bdrv_make_empty = bdrv_qed_make_empty, .bdrv_aio_readv = bdrv_qed_aio_readv,block/raw-posix.c +0
-10@@ -836,12 +836,6 @@ static int raw_create(const char *filename, QEMUOptionParameter *options)return result; } -static int raw_flush (BlockDriverState *bs) -{ -BDRVRawState *s = bs->opaque; -return qemu_fdatasync (s->fd); -} -#ifdef CONFIG_XFS static int xfs_discard (BDRVRawState *s,int64_t sector_num,int nb_sectors) {@@ -893,7 +887,6 @@ static BlockDriver bdrv_file = { .bdrv_write = raw_write, .bdrv_close = raw_close, .bdrv_create = raw_create, - .bdrv_flush = raw_flush, .bdrv_discard = raw_discard, .bdrv_aio_readv = raw_aio_readv,@@ -1163,7 +1156,6 @@ static BlockDriver bdrv_host_device = { .bdrv_create = hdev_create, .create_options = raw_create_options, .bdrv_has_zero_init = hdev_has_zero_init, - .bdrv_flush = raw_flush, .bdrv_aio_readv = raw_aio_readv, .bdrv_aio_writev = raw_aio_writev,@@ -1284,7 +1276,6 @@ static BlockDriver bdrv_host_floppy = { .bdrv_create = hdev_create, .create_options = raw_create_options, .bdrv_has_zero_init = hdev_has_zero_init, - .bdrv_flush = raw_flush, .bdrv_aio_readv = raw_aio_readv, .bdrv_aio_writev = raw_aio_writev,@@ -1385,7 +1376,6 @@ static BlockDriver bdrv_host_cdrom = { .bdrv_create = hdev_create, .create_options = raw_create_options, .bdrv_has_zero_init = hdev_has_zero_init, - .bdrv_flush = raw_flush, .bdrv_aio_readv = raw_aio_readv, .bdrv_aio_writev = raw_aio_writev,
block/raw-posix.c static BlockDriver bdrv_host_cdrom = { · line 1501
Quoted 7 lines
block/raw-posix.c +0
-1@@ -1506,7 +1496,6 @@ static BlockDriver bdrv_host_cdrom = { .bdrv_create = hdev_create, .create_options = raw_create_options, .bdrv_has_zero_init = hdev_has_zero_init, - .bdrv_flush = raw_flush, .bdrv_aio_readv = raw_aio_readv, .bdrv_aio_writev = raw_aio_writev,
Paolo