perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / nvme / target / io-cmd-bdev.c
blobc1ec3475a1407a1f0a52ff4d12f214fbe7c8df12
1 /*
2 * NVMe I/O command implementation.
3 * Copyright (c) 2015-2016 HGST, a Western Digital Company.
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.
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.
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 #include <linux/blkdev.h>
16 #include <linux/module.h>
17 #include "nvmet.h"
19 int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
21 int ret;
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));
31 ns->bdev = NULL;
32 return ret;
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;
39 void nvmet_bdev_ns_disable(struct nvmet_ns *ns)
41 if (ns->bdev) {
42 blkdev_put(ns->bdev, FMODE_WRITE | FMODE_READ);
43 ns->bdev = NULL;
47 static void nvmet_bio_done(struct bio *bio)
49 struct nvmet_req *req = bio->bi_private;
51 nvmet_req_complete(req,
52 bio->bi_status ? NVME_SC_INTERNAL | NVME_SC_DNR : 0);
54 if (bio != &req->b.inline_bio)
55 bio_put(bio);
58 static void nvmet_bdev_execute_rw(struct nvmet_req *req)
60 int sg_cnt = req->sg_cnt;
61 struct bio *bio;
62 struct scatterlist *sg;
63 sector_t sector;
64 blk_qc_t cookie;
65 int op, op_flags = 0, i;
67 if (!req->sg_cnt) {
68 nvmet_req_complete(req, 0);
69 return;
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;
81 if (is_pci_p2pdma_page(sg_page(req->sg)))
82 op_flags |= REQ_NOMERGE;
84 sector = le64_to_cpu(req->cmd->rw.slba);
85 sector <<= (req->ns->blksize_shift - 9);
87 if (req->data_len <= NVMET_MAX_INLINE_DATA_LEN) {
88 bio = &req->b.inline_bio;
89 bio_init(bio, req->inline_bvec, ARRAY_SIZE(req->inline_bvec));
90 } else {
91 bio = bio_alloc(GFP_KERNEL, min(sg_cnt, BIO_MAX_PAGES));
93 bio_set_dev(bio, req->ns->bdev);
94 bio->bi_iter.bi_sector = sector;
95 bio->bi_private = req;
96 bio->bi_end_io = nvmet_bio_done;
97 bio_set_op_attrs(bio, op, op_flags);
99 for_each_sg(req->sg, sg, req->sg_cnt, i) {
100 while (bio_add_page(bio, sg_page(sg), sg->length, sg->offset)
101 != sg->length) {
102 struct bio *prev = bio;
104 bio = bio_alloc(GFP_KERNEL, min(sg_cnt, BIO_MAX_PAGES));
105 bio_set_dev(bio, req->ns->bdev);
106 bio->bi_iter.bi_sector = sector;
107 bio_set_op_attrs(bio, op, op_flags);
109 bio_chain(bio, prev);
110 submit_bio(prev);
113 sector += sg->length >> 9;
114 sg_cnt--;
117 cookie = submit_bio(bio);
119 blk_poll(bdev_get_queue(req->ns->bdev), cookie);
122 static void nvmet_bdev_execute_flush(struct nvmet_req *req)
124 struct bio *bio = &req->b.inline_bio;
126 bio_init(bio, req->inline_bvec, ARRAY_SIZE(req->inline_bvec));
127 bio_set_dev(bio, req->ns->bdev);
128 bio->bi_private = req;
129 bio->bi_end_io = nvmet_bio_done;
130 bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
132 submit_bio(bio);
135 u16 nvmet_bdev_flush(struct nvmet_req *req)
137 if (blkdev_issue_flush(req->ns->bdev, GFP_KERNEL, NULL))
138 return NVME_SC_INTERNAL | NVME_SC_DNR;
139 return 0;
142 static u16 nvmet_bdev_discard_range(struct nvmet_ns *ns,
143 struct nvme_dsm_range *range, struct bio **bio)
145 int ret;
147 ret = __blkdev_issue_discard(ns->bdev,
148 le64_to_cpu(range->slba) << (ns->blksize_shift - 9),
149 le32_to_cpu(range->nlb) << (ns->blksize_shift - 9),
150 GFP_KERNEL, 0, bio);
151 if (ret && ret != -EOPNOTSUPP)
152 return NVME_SC_INTERNAL | NVME_SC_DNR;
153 return 0;
156 static void nvmet_bdev_execute_discard(struct nvmet_req *req)
158 struct nvme_dsm_range range;
159 struct bio *bio = NULL;
160 int i;
161 u16 status;
163 for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) {
164 status = nvmet_copy_from_sgl(req, i * sizeof(range), &range,
165 sizeof(range));
166 if (status)
167 break;
169 status = nvmet_bdev_discard_range(req->ns, &range, &bio);
170 if (status)
171 break;
174 if (bio) {
175 bio->bi_private = req;
176 bio->bi_end_io = nvmet_bio_done;
177 if (status) {
178 bio->bi_status = BLK_STS_IOERR;
179 bio_endio(bio);
180 } else {
181 submit_bio(bio);
183 } else {
184 nvmet_req_complete(req, status);
188 static void nvmet_bdev_execute_dsm(struct nvmet_req *req)
190 switch (le32_to_cpu(req->cmd->dsm.attributes)) {
191 case NVME_DSMGMT_AD:
192 nvmet_bdev_execute_discard(req);
193 return;
194 case NVME_DSMGMT_IDR:
195 case NVME_DSMGMT_IDW:
196 default:
197 /* Not supported yet */
198 nvmet_req_complete(req, 0);
199 return;
203 static void nvmet_bdev_execute_write_zeroes(struct nvmet_req *req)
205 struct nvme_write_zeroes_cmd *write_zeroes = &req->cmd->write_zeroes;
206 struct bio *bio = NULL;
207 u16 status = NVME_SC_SUCCESS;
208 sector_t sector;
209 sector_t nr_sector;
211 sector = le64_to_cpu(write_zeroes->slba) <<
212 (req->ns->blksize_shift - 9);
213 nr_sector = (((sector_t)le16_to_cpu(write_zeroes->length) + 1) <<
214 (req->ns->blksize_shift - 9));
216 if (__blkdev_issue_zeroout(req->ns->bdev, sector, nr_sector,
217 GFP_KERNEL, &bio, 0))
218 status = NVME_SC_INTERNAL | NVME_SC_DNR;
220 if (bio) {
221 bio->bi_private = req;
222 bio->bi_end_io = nvmet_bio_done;
223 submit_bio(bio);
224 } else {
225 nvmet_req_complete(req, status);
229 u16 nvmet_bdev_parse_io_cmd(struct nvmet_req *req)
231 struct nvme_command *cmd = req->cmd;
233 switch (cmd->common.opcode) {
234 case nvme_cmd_read:
235 case nvme_cmd_write:
236 req->execute = nvmet_bdev_execute_rw;
237 req->data_len = nvmet_rw_len(req);
238 return 0;
239 case nvme_cmd_flush:
240 req->execute = nvmet_bdev_execute_flush;
241 req->data_len = 0;
242 return 0;
243 case nvme_cmd_dsm:
244 req->execute = nvmet_bdev_execute_dsm;
245 req->data_len = (le32_to_cpu(cmd->dsm.nr) + 1) *
246 sizeof(struct nvme_dsm_range);
247 return 0;
248 case nvme_cmd_write_zeroes:
249 req->execute = nvmet_bdev_execute_write_zeroes;
250 return 0;
251 default:
252 pr_err("unhandled cmd %d on qid %d\n", cmd->common.opcode,
253 req->sq->qid);
254 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;