linux/drivers/nvme/target/io-cmd-bdev.c
<<
>>
Prefs
   1/*
   2 * NVMe I/O command implementation.
   3 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
   4 *
   5 * This program is free software; you can redistribute it and/or modify it
   6 * under the terms and conditions of the GNU General Public License,
   7 * version 2, as published by the Free Software Foundation.
   8 *
   9 * This program is distributed in the hope it will be useful, but WITHOUT
  10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  12 * more details.
  13 */
  14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15#include <linux/blkdev.h>
  16#include <linux/module.h>
  17#include "nvmet.h"
  18
  19int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
  20{
  21        int ret;
  22
  23        ns->bdev = blkdev_get_by_path(ns->device_path,
  24                        FMODE_READ | FMODE_WRITE, NULL);
  25        if (IS_ERR(ns->bdev)) {
  26                ret = PTR_ERR(ns->bdev);
  27                if (ret != -ENOTBLK) {
  28                        pr_err("failed to open block device %s: (%ld)\n",
  29                                        ns->device_path, PTR_ERR(ns->bdev));
  30                }
  31                ns->bdev = NULL;
  32                return ret;
  33        }
  34        ns->size = i_size_read(ns->bdev->bd_inode);
  35        ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
  36        return 0;
  37}
  38
  39void nvmet_bdev_ns_disable(struct nvmet_ns *ns)
  40{
  41        if (ns->bdev) {
  42                blkdev_put(ns->bdev, FMODE_WRITE | FMODE_READ);
  43                ns->bdev = NULL;
  44        }
  45}
  46
  47static void nvmet_bio_done(struct bio *bio)
  48{
  49        struct nvmet_req *req = bio->bi_private;
  50
  51        nvmet_req_complete(req,
  52                bio->bi_status ? NVME_SC_INTERNAL | NVME_SC_DNR : 0);
  53
  54        if (bio != &req->b.inline_bio)
  55                bio_put(bio);
  56}
  57
  58static void nvmet_bdev_execute_rw(struct nvmet_req *req)
  59{
  60        int sg_cnt = req->sg_cnt;
  61        struct bio *bio = &req->b.inline_bio;
  62        struct scatterlist *sg;
  63        sector_t sector;
  64        blk_qc_t cookie;
  65        int op, op_flags = 0, i;
  66
  67        if (!req->sg_cnt) {
  68                nvmet_req_complete(req, 0);
  69                return;
  70        }
  71
  72        if (req->cmd->rw.opcode == nvme_cmd_write) {
  73                op = REQ_OP_WRITE;
  74                op_flags = REQ_SYNC | REQ_IDLE;
  75                if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
  76                        op_flags |= REQ_FUA;
  77        } else {
  78                op = REQ_OP_READ;
  79        }
  80
  81        if (is_pci_p2pdma_page(sg_page(req->sg)))
  82                op_flags |= REQ_NOMERGE;
  83
  84        sector = le64_to_cpu(req->cmd->rw.slba);
  85        sector <<= (req->ns->blksize_shift - 9);
  86
  87        bio_init(bio, req->inline_bvec, ARRAY_SIZE(req->inline_bvec));
  88        bio_set_dev(bio, req->ns->bdev);
  89        bio->bi_iter.bi_sector = sector;
  90        bio->bi_private = req;
  91        bio->bi_end_io = nvmet_bio_done;
  92        bio_set_op_attrs(bio, op, op_flags);
  93
  94        for_each_sg(req->sg, sg, req->sg_cnt, i) {
  95                while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
  96                                != sg->length) {
  97                        struct bio *prev = bio;
  98
  99                        bio = bio_alloc(GFP_KERNEL, min(sg_cnt, BIO_MAX_PAGES));
 100                        bio_set_dev(bio, req->ns->bdev);
 101                        bio->bi_iter.bi_sector = sector;
 102                        bio_set_op_attrs(bio, op, op_flags);
 103
 104                        bio_chain(bio, prev);
 105                        submit_bio(prev);
 106                }
 107
 108                sector += sg->length >> 9;
 109                sg_cnt--;
 110        }
 111
 112        cookie = submit_bio(bio);
 113}
 114
 115static void nvmet_bdev_execute_flush(struct nvmet_req *req)
 116{
 117        struct bio *bio = &req->b.inline_bio;
 118
 119        bio_init(bio, req->inline_bvec, ARRAY_SIZE(req->inline_bvec));
 120        bio_set_dev(bio, req->ns->bdev);
 121        bio->bi_private = req;
 122        bio->bi_end_io = nvmet_bio_done;
 123        bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
 124
 125        submit_bio(bio);
 126}
 127
 128static u16 nvmet_bdev_discard_range(struct nvmet_ns *ns,
 129                struct nvme_dsm_range *range, struct bio **bio)
 130{
 131        int ret;
 132
 133        ret = __blkdev_issue_discard(ns->bdev,
 134                        le64_to_cpu(range->slba) << (ns->blksize_shift - 9),
 135                        le32_to_cpu(range->nlb) << (ns->blksize_shift - 9),
 136                        GFP_KERNEL, 0, bio);
 137        if (ret && ret != -EOPNOTSUPP)
 138                return NVME_SC_INTERNAL | NVME_SC_DNR;
 139        return 0;
 140}
 141
 142static void nvmet_bdev_execute_discard(struct nvmet_req *req)
 143{
 144        struct nvme_dsm_range range;
 145        struct bio *bio = NULL;
 146        int i;
 147        u16 status;
 148
 149        for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) {
 150                status = nvmet_copy_from_sgl(req, i * sizeof(range), &range,
 151                                sizeof(range));
 152                if (status)
 153                        break;
 154
 155                status = nvmet_bdev_discard_range(req->ns, &range, &bio);
 156                if (status)
 157                        break;
 158        }
 159
 160        if (bio) {
 161                bio->bi_private = req;
 162                bio->bi_end_io = nvmet_bio_done;
 163                if (status) {
 164                        bio->bi_status = BLK_STS_IOERR;
 165                        bio_endio(bio);
 166                } else {
 167                        submit_bio(bio);
 168                }
 169        } else {
 170                nvmet_req_complete(req, status);
 171        }
 172}
 173
 174static void nvmet_bdev_execute_dsm(struct nvmet_req *req)
 175{
 176        switch (le32_to_cpu(req->cmd->dsm.attributes)) {
 177        case NVME_DSMGMT_AD:
 178                nvmet_bdev_execute_discard(req);
 179                return;
 180        case NVME_DSMGMT_IDR:
 181        case NVME_DSMGMT_IDW:
 182        default:
 183                /* Not supported yet */
 184                nvmet_req_complete(req, 0);
 185                return;
 186        }
 187}
 188
 189static void nvmet_bdev_execute_write_zeroes(struct nvmet_req *req)
 190{
 191        struct nvme_write_zeroes_cmd *write_zeroes = &req->cmd->write_zeroes;
 192        struct bio *bio = NULL;
 193        u16 status = NVME_SC_SUCCESS;
 194        sector_t sector;
 195        sector_t nr_sector;
 196
 197        sector = le64_to_cpu(write_zeroes->slba) <<
 198                (req->ns->blksize_shift - 9);
 199        nr_sector = (((sector_t)le16_to_cpu(write_zeroes->length) + 1) <<
 200                (req->ns->blksize_shift - 9));
 201
 202        if (__blkdev_issue_zeroout(req->ns->bdev, sector, nr_sector,
 203                                GFP_KERNEL, &bio, 0))
 204                status = NVME_SC_INTERNAL | NVME_SC_DNR;
 205
 206        if (bio) {
 207                bio->bi_private = req;
 208                bio->bi_end_io = nvmet_bio_done;
 209                submit_bio(bio);
 210        } else {
 211                nvmet_req_complete(req, status);
 212        }
 213}
 214
 215u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req)
 216{
 217        struct nvme_command *cmd = req->cmd;
 218
 219        switch (cmd->common.opcode) {
 220        case nvme_cmd_read:
 221        case nvme_cmd_write:
 222                req->execute = nvmet_bdev_execute_rw;
 223                req->data_len = nvmet_rw_len(req);
 224                return 0;
 225        case nvme_cmd_flush:
 226                req->execute = nvmet_bdev_execute_flush;
 227                req->data_len = 0;
 228                return 0;
 229        case nvme_cmd_dsm:
 230                req->execute = nvmet_bdev_execute_dsm;
 231                req->data_len = (le32_to_cpu(cmd->dsm.nr) + 1) *
 232                        sizeof(struct nvme_dsm_range);
 233                return 0;
 234        case nvme_cmd_write_zeroes:
 235                req->execute = nvmet_bdev_execute_write_zeroes;
 236                return 0;
 237        default:
 238                pr_err("unhandled cmd %d on qid %d\n", cmd->common.opcode,
 239                       req->sq->qid);
 240                return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
 241        }
 242}
 243