[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 43/69] block/nvme: add support for discard
From: |
Max Reitz |
Subject: |
[PULL 43/69] block/nvme: add support for discard |
Date: |
Mon, 28 Oct 2019 13:14:35 +0100 |
From: Maxim Levitsky <address@hidden>
Signed-off-by: Maxim Levitsky <address@hidden>
Message-id: address@hidden
Reviewed-by: John Snow <address@hidden>
Signed-off-by: Max Reitz <address@hidden>
---
block/nvme.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++
block/trace-events | 2 ++
2 files changed, 85 insertions(+)
diff --git a/block/nvme.c b/block/nvme.c
index 0ac38c24e6..d41c4bda6e 100644
--- a/block/nvme.c
+++ b/block/nvme.c
@@ -113,6 +113,7 @@ typedef struct {
bool plugged;
bool supports_write_zeroes;
+ bool supports_discard;
CoMutex dma_map_lock;
CoQueue dma_flush_queue;
@@ -465,6 +466,7 @@ static void nvme_identify(BlockDriverState *bs, int
namespace, Error **errp)
oncs = le16_to_cpu(idctrl->oncs);
s->supports_write_zeroes = !!(oncs & NVME_ONCS_WRITE_ZEROS);
+ s->supports_discard = !!(oncs & NVME_ONCS_DSM);
memset(resp, 0, 4096);
@@ -1155,6 +1157,86 @@ static coroutine_fn int
nvme_co_pwrite_zeroes(BlockDriverState *bs,
}
+static int coroutine_fn nvme_co_pdiscard(BlockDriverState *bs,
+ int64_t offset,
+ int bytes)
+{
+ BDRVNVMeState *s = bs->opaque;
+ NVMeQueuePair *ioq = s->queues[1];
+ NVMeRequest *req;
+ NvmeDsmRange *buf;
+ QEMUIOVector local_qiov;
+ int ret;
+
+ NvmeCmd cmd = {
+ .opcode = NVME_CMD_DSM,
+ .nsid = cpu_to_le32(s->nsid),
+ .cdw10 = cpu_to_le32(0), /*number of ranges - 0 based*/
+ .cdw11 = cpu_to_le32(1 << 2), /*deallocate bit*/
+ };
+
+ NVMeCoData data = {
+ .ctx = bdrv_get_aio_context(bs),
+ .ret = -EINPROGRESS,
+ };
+
+ if (!s->supports_discard) {
+ return -ENOTSUP;
+ }
+
+ assert(s->nr_queues > 1);
+
+ buf = qemu_try_blockalign0(bs, s->page_size);
+ if (!buf) {
+ return -ENOMEM;
+ }
+
+ buf->nlb = cpu_to_le32(bytes >> s->blkshift);
+ buf->slba = cpu_to_le64(offset >> s->blkshift);
+ buf->cattr = 0;
+
+ qemu_iovec_init(&local_qiov, 1);
+ qemu_iovec_add(&local_qiov, buf, 4096);
+
+ req = nvme_get_free_req(ioq);
+ assert(req);
+
+ qemu_co_mutex_lock(&s->dma_map_lock);
+ ret = nvme_cmd_map_qiov(bs, &cmd, req, &local_qiov);
+ qemu_co_mutex_unlock(&s->dma_map_lock);
+
+ if (ret) {
+ req->busy = false;
+ goto out;
+ }
+
+ trace_nvme_dsm(s, offset, bytes);
+
+ nvme_submit_command(s, ioq, req, &cmd, nvme_rw_cb, &data);
+
+ data.co = qemu_coroutine_self();
+ while (data.ret == -EINPROGRESS) {
+ qemu_coroutine_yield();
+ }
+
+ qemu_co_mutex_lock(&s->dma_map_lock);
+ ret = nvme_cmd_unmap_qiov(bs, &local_qiov);
+ qemu_co_mutex_unlock(&s->dma_map_lock);
+
+ if (ret) {
+ goto out;
+ }
+
+ ret = data.ret;
+ trace_nvme_dsm_done(s, offset, bytes, ret);
+out:
+ qemu_iovec_destroy(&local_qiov);
+ qemu_vfree(buf);
+ return ret;
+
+}
+
+
static int nvme_reopen_prepare(BDRVReopenState *reopen_state,
BlockReopenQueue *queue, Error **errp)
{
@@ -1261,6 +1343,7 @@ static BlockDriver bdrv_nvme = {
.bdrv_co_pwritev = nvme_co_pwritev,
.bdrv_co_pwrite_zeroes = nvme_co_pwrite_zeroes,
+ .bdrv_co_pdiscard = nvme_co_pdiscard,
.bdrv_co_flush_to_disk = nvme_co_flush,
.bdrv_reopen_prepare = nvme_reopen_prepare,
diff --git a/block/trace-events b/block/trace-events
index 8706eab170..6ba86decca 100644
--- a/block/trace-events
+++ b/block/trace-events
@@ -156,6 +156,8 @@ nvme_write_zeroes(void *s, uint64_t offset, uint64_t bytes,
int flags) "s %p off
nvme_qiov_unaligned(const void *qiov, int n, void *base, size_t size, int
align) "qiov %p n %d base %p size 0x%zx align 0x%x"
nvme_prw_buffered(void *s, uint64_t offset, uint64_t bytes, int niov, int
is_write) "s %p offset %"PRId64" bytes %"PRId64" niov %d is_write %d"
nvme_rw_done(void *s, int is_write, uint64_t offset, uint64_t bytes, int ret)
"s %p is_write %d offset %"PRId64" bytes %"PRId64" ret %d"
+nvme_dsm(void *s, uint64_t offset, uint64_t bytes) "s %p offset %"PRId64"
bytes %"PRId64""
+nvme_dsm_done(void *s, uint64_t offset, uint64_t bytes, int ret) "s %p offset
%"PRId64" bytes %"PRId64" ret %d"
nvme_dma_map_flush(void *s) "s %p"
nvme_free_req_queue_wait(void *q) "q %p"
nvme_cmd_map_qiov(void *s, void *cmd, void *req, void *qiov, int entries) "s
%p cmd %p req %p qiov %p entries %d"
--
2.21.0
- [PULL 35/69] iotests: Drop TEST_DIR filter from _filter_nbd, (continued)
- [PULL 35/69] iotests: Drop TEST_DIR filter from _filter_nbd, Max Reitz, 2019/10/28
- [PULL 34/69] iotests/267: Create socket in $SOCK_DIR, Max Reitz, 2019/10/28
- [PULL 33/69] iotests/240: Create socket in $SOCK_DIR, Max Reitz, 2019/10/28
- [PULL 36/69] block/block-copy: allocate buffer in block_copy_with_bounce_buffer, Max Reitz, 2019/10/28
- [PULL 37/69] block/block-copy: limit copy_range_size to 16 MiB, Max Reitz, 2019/10/28
- [PULL 38/69] block/block-copy: refactor copying, Max Reitz, 2019/10/28
- [PULL 39/69] util: introduce SharedResource, Max Reitz, 2019/10/28
- [PULL 40/69] block/block-copy: add memory limit, Max Reitz, 2019/10/28
- [PULL 41/69] block/block-copy: increase buffered copy request, Max Reitz, 2019/10/28
- [PULL 42/69] block/nvme: add support for write zeros, Max Reitz, 2019/10/28
- [PULL 43/69] block/nvme: add support for discard,
Max Reitz <=
- [PULL 44/69] mirror: Do not dereference invalid pointers, Max Reitz, 2019/10/28
- [PULL 45/69] include: Move endof() up from hw/virtio/virtio.h, Max Reitz, 2019/10/28
- [PULL 46/69] qcow2: Use endof(), Max Reitz, 2019/10/28
- [PULL 49/69] qcow2: Make qcow2_write_snapshots() public, Max Reitz, 2019/10/28
- [PULL 47/69] qcow2: Add Error ** to qcow2_read_snapshots(), Max Reitz, 2019/10/28
- [PULL 51/69] qcow2: Write v3-compliant snapshot list on upgrade, Max Reitz, 2019/10/28
- [PULL 50/69] qcow2: Put qcow2_upgrade() into its own function, Max Reitz, 2019/10/28
- [PULL 53/69] qcow2: Add qcow2_check_fix_snapshot_table(), Max Reitz, 2019/10/28
- [PULL 48/69] qcow2: Keep unknown extra snapshot data, Max Reitz, 2019/10/28
- [PULL 62/69] block/cor: Drop cor_co_truncate(), Max Reitz, 2019/10/28