[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PULL 16/48] raw-posix: implement write_zeroes with MAY_UNM
From: |
Stefan Hajnoczi |
Subject: |
[Qemu-devel] [PULL 16/48] raw-posix: implement write_zeroes with MAY_UNMAP for files |
Date: |
Fri, 6 Dec 2013 17:36:15 +0100 |
From: Paolo Bonzini <address@hidden>
Writing zeroes to a file can be done by punching a hole if
MAY_UNMAP is set.
Note that in this case ENOTSUP is not ignored, but makes
the block layer fall back to the generic implementation.
Signed-off-by: Paolo Bonzini <address@hidden>
Signed-off-by: Stefan Hajnoczi <address@hidden>
---
block/raw-posix.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++--
trace-events | 1 +
2 files changed, 65 insertions(+), 2 deletions(-)
diff --git a/block/raw-posix.c b/block/raw-posix.c
index cfa3162..7f3f47d 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -139,9 +139,10 @@ typedef struct BDRVRawState {
void *aio_ctx;
#endif
#ifdef CONFIG_XFS
- bool is_xfs : 1;
+ bool is_xfs:1;
#endif
- bool has_discard : 1;
+ bool has_discard:1;
+ bool discard_zeroes:1;
} BDRVRawState;
typedef struct BDRVRawReopenState {
@@ -283,6 +284,7 @@ static int raw_open_common(BlockDriverState *bs, QDict
*options,
Error *local_err = NULL;
const char *filename;
int fd, ret;
+ struct stat st;
opts = qemu_opts_create_nofail(&raw_runtime_opts);
qemu_opts_absorb_qdict(opts, options, &local_err);
@@ -324,6 +326,15 @@ static int raw_open_common(BlockDriverState *bs, QDict
*options,
#endif
s->has_discard = true;
+
+ if (fstat(s->fd, &st) < 0) {
+ error_setg_errno(errp, errno, "Could not stat file");
+ goto fail;
+ }
+ if (S_ISREG(st.st_mode)) {
+ s->discard_zeroes = true;
+ }
+
#ifdef CONFIG_XFS
if (platform_test_xfs_fd(s->fd)) {
s->is_xfs = true;
@@ -787,6 +798,29 @@ static int aio_worker(void *arg)
return ret;
}
+static int paio_submit_co(BlockDriverState *bs, int fd,
+ int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
+ int type)
+{
+ RawPosixAIOData *acb = g_slice_new(RawPosixAIOData);
+ ThreadPool *pool;
+
+ acb->bs = bs;
+ acb->aio_type = type;
+ acb->aio_fildes = fd;
+
+ if (qiov) {
+ acb->aio_iov = qiov->iov;
+ acb->aio_niov = qiov->niov;
+ }
+ acb->aio_nbytes = nb_sectors * 512;
+ acb->aio_offset = sector_num * 512;
+
+ trace_paio_submit_co(sector_num, nb_sectors, type);
+ pool = aio_get_thread_pool(bdrv_get_aio_context(bs));
+ return thread_pool_submit_co(pool, aio_worker, acb);
+}
+
static BlockDriverAIOCB *paio_submit(BlockDriverState *bs, int fd,
int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
BlockDriverCompletionFunc *cb, void *opaque, int type)
@@ -1199,6 +1233,31 @@ static coroutine_fn BlockDriverAIOCB
*raw_aio_discard(BlockDriverState *bs,
cb, opaque, QEMU_AIO_DISCARD);
}
+static int coroutine_fn raw_co_write_zeroes(
+ BlockDriverState *bs, int64_t sector_num,
+ int nb_sectors, BdrvRequestFlags flags)
+{
+ BDRVRawState *s = bs->opaque;
+
+ if (!(flags & BDRV_REQ_MAY_UNMAP)) {
+ return -ENOTSUP;
+ }
+ if (!s->discard_zeroes) {
+ return -ENOTSUP;
+ }
+ return paio_submit_co(bs, s->fd, sector_num, NULL, nb_sectors,
+ QEMU_AIO_DISCARD);
+}
+
+static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
+{
+ BDRVRawState *s = bs->opaque;
+
+ bdi->unallocated_blocks_are_zero = s->discard_zeroes;
+ bdi->can_write_zeroes_with_unmap = s->discard_zeroes;
+ return 0;
+}
+
static QEMUOptionParameter raw_create_options[] = {
{
.name = BLOCK_OPT_SIZE,
@@ -1222,6 +1281,7 @@ static BlockDriver bdrv_file = {
.bdrv_create = raw_create,
.bdrv_has_zero_init = bdrv_has_zero_init_1,
.bdrv_co_get_block_status = raw_co_get_block_status,
+ .bdrv_co_write_zeroes = raw_co_write_zeroes,
.bdrv_aio_readv = raw_aio_readv,
.bdrv_aio_writev = raw_aio_writev,
@@ -1230,6 +1290,7 @@ static BlockDriver bdrv_file = {
.bdrv_truncate = raw_truncate,
.bdrv_getlength = raw_getlength,
+ .bdrv_get_info = raw_get_info,
.bdrv_get_allocated_file_size
= raw_get_allocated_file_size,
@@ -1585,6 +1646,7 @@ static BlockDriver bdrv_host_device = {
.bdrv_truncate = raw_truncate,
.bdrv_getlength = raw_getlength,
+ .bdrv_get_info = raw_get_info,
.bdrv_get_allocated_file_size
= raw_get_allocated_file_size,
diff --git a/trace-events b/trace-events
index d318d6f..e32d00c 100644
--- a/trace-events
+++ b/trace-events
@@ -128,6 +128,7 @@ thread_pool_cancel(void *req, void *opaque) "req %p opaque
%p"
# block/raw-win32.c
# block/raw-posix.c
+paio_submit_co(int64_t sector_num, int nb_sectors, int type) "sector_num
%"PRId64" nb_sectors %d type %d"
paio_submit(void *acb, void *opaque, int64_t sector_num, int nb_sectors, int
type) "acb %p opaque %p sector_num %"PRId64" nb_sectors %d type %d"
# ioport.c
--
1.8.4.2
- [Qemu-devel] [PULL 06/48] block: add flags argument to bdrv_co_write_zeroes tracepoint, (continued)
- [Qemu-devel] [PULL 06/48] block: add flags argument to bdrv_co_write_zeroes tracepoint, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 07/48] block: add bdrv_aio_write_zeroes, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 08/48] block: handle ENOTSUP from discard in generic code, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 09/48] block: make bdrv_co_do_write_zeroes stricter in producing aligned requests, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 10/48] vpc, vhdx: add get_info, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 11/48] block drivers: add discard/write_zeroes properties to bdrv_get_info implementation, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 12/48] block drivers: expose requirement for write same alignment from formats, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 15/48] block/iscsi: check WRITE SAME support differently depending on MAY_UNMAP, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 13/48] block/iscsi: remove .bdrv_has_zero_init, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 14/48] block/iscsi: updated copyright, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 16/48] raw-posix: implement write_zeroes with MAY_UNMAP for files,
Stefan Hajnoczi <=
- [Qemu-devel] [PULL 17/48] raw-posix: implement write_zeroes with MAY_UNMAP for block devices, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 18/48] raw-posix: add support for write_zeroes on XFS and block devices, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 19/48] qemu-iotests: 033 is fast, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 20/48] scsi-disk: catch write protection errors in UNMAP, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 21/48] scsi-disk: reject ANCHOR=1 for UNMAP and WRITE SAME commands, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 22/48] scsi-disk: correctly implement WRITE SAME, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 23/48] block: Close backing file early in bdrv_img_create, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 24/48] qcow2: Zero-initialise first cluster for new images, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 25/48] qemu-iotests: Add "-c <cache-mode>" option, Stefan Hajnoczi, 2013/12/06
- [Qemu-devel] [PULL 26/48] qemu-iotests: Honour cache mode in iotests.py, Stefan Hajnoczi, 2013/12/06