mirror of
https://github.com/AuxXxilium/linux_dsm_epyc7002.git
synced 2024-12-24 11:46:54 +07:00
005c674f70
NVMe DSM is a pure hint, so if the underlying device / file system
does not support discard-like operations we should not fail the
operation but rather return success.
Fixes: 3b031d1599
("nvmet: add error log support for bdev backend")
Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed by: Chaitanya Kulkarni <chaitanya.kulkarni@wdc.com>
Tested-by: Sagi Grimberg <sagi@grimberg.me>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
306 lines
7.4 KiB
C
306 lines
7.4 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* NVMe I/O command implementation.
|
|
* Copyright (c) 2015-2016 HGST, a Western Digital Company.
|
|
*/
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
#include <linux/blkdev.h>
|
|
#include <linux/module.h>
|
|
#include "nvmet.h"
|
|
|
|
int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
|
|
{
|
|
int ret;
|
|
|
|
ns->bdev = blkdev_get_by_path(ns->device_path,
|
|
FMODE_READ | FMODE_WRITE, NULL);
|
|
if (IS_ERR(ns->bdev)) {
|
|
ret = PTR_ERR(ns->bdev);
|
|
if (ret != -ENOTBLK) {
|
|
pr_err("failed to open block device %s: (%ld)\n",
|
|
ns->device_path, PTR_ERR(ns->bdev));
|
|
}
|
|
ns->bdev = NULL;
|
|
return ret;
|
|
}
|
|
ns->size = i_size_read(ns->bdev->bd_inode);
|
|
ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
|
|
return 0;
|
|
}
|
|
|
|
void nvmet_bdev_ns_disable(struct nvmet_ns *ns)
|
|
{
|
|
if (ns->bdev) {
|
|
blkdev_put(ns->bdev, FMODE_WRITE | FMODE_READ);
|
|
ns->bdev = NULL;
|
|
}
|
|
}
|
|
|
|
static u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts)
|
|
{
|
|
u16 status = NVME_SC_SUCCESS;
|
|
|
|
if (likely(blk_sts == BLK_STS_OK))
|
|
return status;
|
|
/*
|
|
* Right now there exists M : 1 mapping between block layer error
|
|
* to the NVMe status code (see nvme_error_status()). For consistency,
|
|
* when we reverse map we use most appropriate NVMe Status code from
|
|
* the group of the NVMe staus codes used in the nvme_error_status().
|
|
*/
|
|
switch (blk_sts) {
|
|
case BLK_STS_NOSPC:
|
|
status = NVME_SC_CAP_EXCEEDED | NVME_SC_DNR;
|
|
req->error_loc = offsetof(struct nvme_rw_command, length);
|
|
break;
|
|
case BLK_STS_TARGET:
|
|
status = NVME_SC_LBA_RANGE | NVME_SC_DNR;
|
|
req->error_loc = offsetof(struct nvme_rw_command, slba);
|
|
break;
|
|
case BLK_STS_NOTSUPP:
|
|
req->error_loc = offsetof(struct nvme_common_command, opcode);
|
|
switch (req->cmd->common.opcode) {
|
|
case nvme_cmd_dsm:
|
|
case nvme_cmd_write_zeroes:
|
|
status = NVME_SC_ONCS_NOT_SUPPORTED | NVME_SC_DNR;
|
|
break;
|
|
default:
|
|
status = NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
|
|
}
|
|
break;
|
|
case BLK_STS_MEDIUM:
|
|
status = NVME_SC_ACCESS_DENIED;
|
|
req->error_loc = offsetof(struct nvme_rw_command, nsid);
|
|
break;
|
|
case BLK_STS_IOERR:
|
|
/* fallthru */
|
|
default:
|
|
status = NVME_SC_INTERNAL | NVME_SC_DNR;
|
|
req->error_loc = offsetof(struct nvme_common_command, opcode);
|
|
}
|
|
|
|
switch (req->cmd->common.opcode) {
|
|
case nvme_cmd_read:
|
|
case nvme_cmd_write:
|
|
req->error_slba = le64_to_cpu(req->cmd->rw.slba);
|
|
break;
|
|
case nvme_cmd_write_zeroes:
|
|
req->error_slba =
|
|
le64_to_cpu(req->cmd->write_zeroes.slba);
|
|
break;
|
|
default:
|
|
req->error_slba = 0;
|
|
}
|
|
return status;
|
|
}
|
|
|
|
static void nvmet_bio_done(struct bio *bio)
|
|
{
|
|
struct nvmet_req *req = bio->bi_private;
|
|
|
|
nvmet_req_complete(req, blk_to_nvme_status(req, bio->bi_status));
|
|
if (bio != &req->b.inline_bio)
|
|
bio_put(bio);
|
|
}
|
|
|
|
static void nvmet_bdev_execute_rw(struct nvmet_req *req)
|
|
{
|
|
int sg_cnt = req->sg_cnt;
|
|
struct bio *bio;
|
|
struct scatterlist *sg;
|
|
sector_t sector;
|
|
int op, op_flags = 0, i;
|
|
|
|
if (!req->sg_cnt) {
|
|
nvmet_req_complete(req, 0);
|
|
return;
|
|
}
|
|
|
|
if (req->cmd->rw.opcode == nvme_cmd_write) {
|
|
op = REQ_OP_WRITE;
|
|
op_flags = REQ_SYNC | REQ_IDLE;
|
|
if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
|
|
op_flags |= REQ_FUA;
|
|
} else {
|
|
op = REQ_OP_READ;
|
|
}
|
|
|
|
if (is_pci_p2pdma_page(sg_page(req->sg)))
|
|
op_flags |= REQ_NOMERGE;
|
|
|
|
sector = le64_to_cpu(req->cmd->rw.slba);
|
|
sector <<= (req->ns->blksize_shift - 9);
|
|
|
|
if (req->data_len <= NVMET_MAX_INLINE_DATA_LEN) {
|
|
bio = &req->b.inline_bio;
|
|
bio_init(bio, req->inline_bvec, ARRAY_SIZE(req->inline_bvec));
|
|
} else {
|
|
bio = bio_alloc(GFP_KERNEL, min(sg_cnt, BIO_MAX_PAGES));
|
|
}
|
|
bio_set_dev(bio, req->ns->bdev);
|
|
bio->bi_iter.bi_sector = sector;
|
|
bio->bi_private = req;
|
|
bio->bi_end_io = nvmet_bio_done;
|
|
bio_set_op_attrs(bio, op, op_flags);
|
|
|
|
for_each_sg(req->sg, sg, req->sg_cnt, i) {
|
|
while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
|
|
!= sg->length) {
|
|
struct bio *prev = bio;
|
|
|
|
bio = bio_alloc(GFP_KERNEL, min(sg_cnt, BIO_MAX_PAGES));
|
|
bio_set_dev(bio, req->ns->bdev);
|
|
bio->bi_iter.bi_sector = sector;
|
|
bio_set_op_attrs(bio, op, op_flags);
|
|
|
|
bio_chain(bio, prev);
|
|
submit_bio(prev);
|
|
}
|
|
|
|
sector += sg->length >> 9;
|
|
sg_cnt--;
|
|
}
|
|
|
|
submit_bio(bio);
|
|
}
|
|
|
|
static void nvmet_bdev_execute_flush(struct nvmet_req *req)
|
|
{
|
|
struct bio *bio = &req->b.inline_bio;
|
|
|
|
bio_init(bio, req->inline_bvec, ARRAY_SIZE(req->inline_bvec));
|
|
bio_set_dev(bio, req->ns->bdev);
|
|
bio->bi_private = req;
|
|
bio->bi_end_io = nvmet_bio_done;
|
|
bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
|
|
|
|
submit_bio(bio);
|
|
}
|
|
|
|
u16 nvmet_bdev_flush(struct nvmet_req *req)
|
|
{
|
|
if (blkdev_issue_flush(req->ns->bdev, GFP_KERNEL, NULL))
|
|
return NVME_SC_INTERNAL | NVME_SC_DNR;
|
|
return 0;
|
|
}
|
|
|
|
static u16 nvmet_bdev_discard_range(struct nvmet_req *req,
|
|
struct nvme_dsm_range *range, struct bio **bio)
|
|
{
|
|
struct nvmet_ns *ns = req->ns;
|
|
int ret;
|
|
|
|
ret = __blkdev_issue_discard(ns->bdev,
|
|
le64_to_cpu(range->slba) << (ns->blksize_shift - 9),
|
|
le32_to_cpu(range->nlb) << (ns->blksize_shift - 9),
|
|
GFP_KERNEL, 0, bio);
|
|
if (ret && ret != -EOPNOTSUPP) {
|
|
req->error_slba = le64_to_cpu(range->slba);
|
|
return blk_to_nvme_status(req, errno_to_blk_status(ret));
|
|
}
|
|
return NVME_SC_SUCCESS;
|
|
}
|
|
|
|
static void nvmet_bdev_execute_discard(struct nvmet_req *req)
|
|
{
|
|
struct nvme_dsm_range range;
|
|
struct bio *bio = NULL;
|
|
int i;
|
|
u16 status;
|
|
|
|
for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) {
|
|
status = nvmet_copy_from_sgl(req, i * sizeof(range), &range,
|
|
sizeof(range));
|
|
if (status)
|
|
break;
|
|
|
|
status = nvmet_bdev_discard_range(req, &range, &bio);
|
|
if (status)
|
|
break;
|
|
}
|
|
|
|
if (bio) {
|
|
bio->bi_private = req;
|
|
bio->bi_end_io = nvmet_bio_done;
|
|
if (status) {
|
|
bio->bi_status = BLK_STS_IOERR;
|
|
bio_endio(bio);
|
|
} else {
|
|
submit_bio(bio);
|
|
}
|
|
} else {
|
|
nvmet_req_complete(req, status);
|
|
}
|
|
}
|
|
|
|
static void nvmet_bdev_execute_dsm(struct nvmet_req *req)
|
|
{
|
|
switch (le32_to_cpu(req->cmd->dsm.attributes)) {
|
|
case NVME_DSMGMT_AD:
|
|
nvmet_bdev_execute_discard(req);
|
|
return;
|
|
case NVME_DSMGMT_IDR:
|
|
case NVME_DSMGMT_IDW:
|
|
default:
|
|
/* Not supported yet */
|
|
nvmet_req_complete(req, 0);
|
|
return;
|
|
}
|
|
}
|
|
|
|
static void nvmet_bdev_execute_write_zeroes(struct nvmet_req *req)
|
|
{
|
|
struct nvme_write_zeroes_cmd *write_zeroes = &req->cmd->write_zeroes;
|
|
struct bio *bio = NULL;
|
|
u16 status = NVME_SC_SUCCESS;
|
|
sector_t sector;
|
|
sector_t nr_sector;
|
|
int ret;
|
|
|
|
sector = le64_to_cpu(write_zeroes->slba) <<
|
|
(req->ns->blksize_shift - 9);
|
|
nr_sector = (((sector_t)le16_to_cpu(write_zeroes->length) + 1) <<
|
|
(req->ns->blksize_shift - 9));
|
|
|
|
ret = __blkdev_issue_zeroout(req->ns->bdev, sector, nr_sector,
|
|
GFP_KERNEL, &bio, 0);
|
|
status = blk_to_nvme_status(req, errno_to_blk_status(ret));
|
|
if (bio) {
|
|
bio->bi_private = req;
|
|
bio->bi_end_io = nvmet_bio_done;
|
|
submit_bio(bio);
|
|
} else {
|
|
nvmet_req_complete(req, status);
|
|
}
|
|
}
|
|
|
|
u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req)
|
|
{
|
|
struct nvme_command *cmd = req->cmd;
|
|
|
|
switch (cmd->common.opcode) {
|
|
case nvme_cmd_read:
|
|
case nvme_cmd_write:
|
|
req->execute = nvmet_bdev_execute_rw;
|
|
req->data_len = nvmet_rw_len(req);
|
|
return 0;
|
|
case nvme_cmd_flush:
|
|
req->execute = nvmet_bdev_execute_flush;
|
|
req->data_len = 0;
|
|
return 0;
|
|
case nvme_cmd_dsm:
|
|
req->execute = nvmet_bdev_execute_dsm;
|
|
req->data_len = (le32_to_cpu(cmd->dsm.nr) + 1) *
|
|
sizeof(struct nvme_dsm_range);
|
|
return 0;
|
|
case nvme_cmd_write_zeroes:
|
|
req->execute = nvmet_bdev_execute_write_zeroes;
|
|
return 0;
|
|
default:
|
|
pr_err("unhandled cmd %d on qid %d\n", cmd->common.opcode,
|
|
req->sq->qid);
|
|
req->error_loc = offsetof(struct nvme_common_command, opcode);
|
|
return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
|
|
}
|
|
}
|