blob: caebfce066056bac1c7e0adb581392f1ed9eb545 [file] [log] [blame]
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -04001// SPDX-License-Identifier: GPL-2.0
2/*
3 * NVMe Over Fabrics Target File I/O commands implementation.
4 * Copyright (c) 2017-2018 Western Digital Corporation or its
5 * affiliates.
6 */
7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8#include <linux/uio.h>
9#include <linux/falloc.h>
10#include <linux/file.h>
11#include "nvmet.h"
12
13#define NVMET_MAX_MPOOL_BVEC 16
14#define NVMET_MIN_MPOOL_OBJ 16
15
16void nvmet_file_ns_disable(struct nvmet_ns *ns)
17{
18 if (ns->file) {
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -040019 if (ns->buffered_io)
20 flush_workqueue(buffered_io_wq);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040021 mempool_destroy(ns->bvec_pool);
22 ns->bvec_pool = NULL;
23 kmem_cache_destroy(ns->bvec_cache);
24 ns->bvec_cache = NULL;
25 fput(ns->file);
26 ns->file = NULL;
27 }
28}
29
30int nvmet_file_ns_enable(struct nvmet_ns *ns)
31{
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -040032 int flags = O_RDWR | O_LARGEFILE;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040033 struct kstat stat;
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -040034 int ret;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040035
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -040036 if (!ns->buffered_io)
37 flags |= O_DIRECT;
38
39 ns->file = filp_open(ns->device_path, flags, 0);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040040 if (IS_ERR(ns->file)) {
41 pr_err("failed to open file %s: (%ld)\n",
Wei Yongjun81cf54e02018-05-31 01:55:58 +000042 ns->device_path, PTR_ERR(ns->file));
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040043 return PTR_ERR(ns->file);
44 }
45
46 ret = vfs_getattr(&ns->file->f_path,
47 &stat, STATX_SIZE, AT_STATX_FORCE_SYNC);
48 if (ret)
49 goto err;
50
51 ns->size = stat.size;
Sagi Grimberg525ec492019-04-24 11:43:23 -070052 /*
53 * i_blkbits can be greater than the universally accepted upper bound,
54 * so make sure we export a sane namespace lba_shift.
55 */
56 ns->blksize_shift = min_t(u8,
57 file_inode(ns->file)->i_blkbits, 12);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040058
59 ns->bvec_cache = kmem_cache_create("nvmet-bvec",
60 NVMET_MAX_MPOOL_BVEC * sizeof(struct bio_vec),
61 0, SLAB_HWCACHE_ALIGN, NULL);
Wei Yongjun1367bc82018-05-31 11:41:30 +000062 if (!ns->bvec_cache) {
63 ret = -ENOMEM;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040064 goto err;
Wei Yongjun1367bc82018-05-31 11:41:30 +000065 }
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040066
67 ns->bvec_pool = mempool_create(NVMET_MIN_MPOOL_OBJ, mempool_alloc_slab,
68 mempool_free_slab, ns->bvec_cache);
69
Wei Yongjun1367bc82018-05-31 11:41:30 +000070 if (!ns->bvec_pool) {
71 ret = -ENOMEM;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040072 goto err;
Wei Yongjun1367bc82018-05-31 11:41:30 +000073 }
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040074
75 return ret;
76err:
77 ns->size = 0;
78 ns->blksize_shift = 0;
79 nvmet_file_ns_disable(ns);
80 return ret;
81}
82
Ming Lei02db9952019-03-27 17:07:22 +080083static void nvmet_file_init_bvec(struct bio_vec *bv, struct scatterlist *sg)
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040084{
Ming Lei02db9952019-03-27 17:07:22 +080085 bv->bv_page = sg_page(sg);
86 bv->bv_offset = sg->offset;
87 bv->bv_len = sg->length;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040088}
89
90static ssize_t nvmet_file_submit_bvec(struct nvmet_req *req, loff_t pos,
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -050091 unsigned long nr_segs, size_t count, int ki_flags)
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040092{
93 struct kiocb *iocb = &req->f.iocb;
94 ssize_t (*call_iter)(struct kiocb *iocb, struct iov_iter *iter);
95 struct iov_iter iter;
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -050096 int rw;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040097
98 if (req->cmd->rw.opcode == nvme_cmd_write) {
99 if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500100 ki_flags |= IOCB_DSYNC;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400101 call_iter = req->ns->file->f_op->write_iter;
102 rw = WRITE;
103 } else {
104 call_iter = req->ns->file->f_op->read_iter;
105 rw = READ;
106 }
107
David Howellsaa563d72018-10-20 00:57:56 +0100108 iov_iter_bvec(&iter, rw, req->f.bvec, nr_segs, count);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400109
110 iocb->ki_pos = pos;
111 iocb->ki_filp = req->ns->file;
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -0400112 iocb->ki_flags = ki_flags | iocb_flags(req->ns->file);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400113
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500114 return call_iter(iocb, &iter);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400115}
116
117static void nvmet_file_io_done(struct kiocb *iocb, long ret, long ret2)
118{
119 struct nvmet_req *req = container_of(iocb, struct nvmet_req, f.iocb);
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800120 u16 status = NVME_SC_SUCCESS;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400121
122 if (req->f.bvec != req->inline_bvec) {
123 if (likely(req->f.mpool_alloc == false))
124 kfree(req->f.bvec);
125 else
126 mempool_free(req->f.bvec, req->ns->bvec_pool);
127 }
128
Christoph Hellwige9061c32019-10-23 10:35:44 -0600129 if (unlikely(ret != req->transfer_len))
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800130 status = errno_to_nvme_status(req, ret);
131 nvmet_req_complete(req, status);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400132}
133
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500134static bool nvmet_file_execute_io(struct nvmet_req *req, int ki_flags)
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400135{
Ming Lei02db9952019-03-27 17:07:22 +0800136 ssize_t nr_bvec = req->sg_cnt;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400137 unsigned long bv_cnt = 0;
138 bool is_sync = false;
139 size_t len = 0, total_len = 0;
140 ssize_t ret = 0;
141 loff_t pos;
Ming Lei02db9952019-03-27 17:07:22 +0800142 int i;
143 struct scatterlist *sg;
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500144
145 if (req->f.mpool_alloc && nr_bvec > NVMET_MAX_MPOOL_BVEC)
146 is_sync = true;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400147
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300148 pos = le64_to_cpu(req->cmd->rw.slba) << req->ns->blksize_shift;
Christoph Hellwige9061c32019-10-23 10:35:44 -0600149 if (unlikely(pos + req->transfer_len > req->ns->size)) {
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800150 nvmet_req_complete(req, errno_to_nvme_status(req, -ENOSPC));
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500151 return true;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400152 }
153
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400154 memset(&req->f.iocb, 0, sizeof(struct kiocb));
Ming Lei02db9952019-03-27 17:07:22 +0800155 for_each_sg(req->sg, sg, req->sg_cnt, i) {
156 nvmet_file_init_bvec(&req->f.bvec[bv_cnt], sg);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400157 len += req->f.bvec[bv_cnt].bv_len;
158 total_len += req->f.bvec[bv_cnt].bv_len;
159 bv_cnt++;
160
161 WARN_ON_ONCE((nr_bvec - 1) < 0);
162
163 if (unlikely(is_sync) &&
164 (nr_bvec - 1 == 0 || bv_cnt == NVMET_MAX_MPOOL_BVEC)) {
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500165 ret = nvmet_file_submit_bvec(req, pos, bv_cnt, len, 0);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400166 if (ret < 0)
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500167 goto complete;
168
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400169 pos += len;
170 bv_cnt = 0;
171 len = 0;
172 }
173 nr_bvec--;
174 }
175
Christoph Hellwige9061c32019-10-23 10:35:44 -0600176 if (WARN_ON_ONCE(total_len != req->transfer_len)) {
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400177 ret = -EIO;
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500178 goto complete;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400179 }
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500180
181 if (unlikely(is_sync)) {
182 ret = total_len;
183 goto complete;
184 }
185
186 /*
187 * A NULL ki_complete ask for synchronous execution, which we want
188 * for the IOCB_NOWAIT case.
189 */
190 if (!(ki_flags & IOCB_NOWAIT))
191 req->f.iocb.ki_complete = nvmet_file_io_done;
192
193 ret = nvmet_file_submit_bvec(req, pos, bv_cnt, total_len, ki_flags);
194
195 switch (ret) {
196 case -EIOCBQUEUED:
197 return true;
198 case -EAGAIN:
199 if (WARN_ON_ONCE(!(ki_flags & IOCB_NOWAIT)))
200 goto complete;
201 return false;
202 case -EOPNOTSUPP:
203 /*
204 * For file systems returning error -EOPNOTSUPP, handle
205 * IOCB_NOWAIT error case separately and retry without
206 * IOCB_NOWAIT.
207 */
208 if ((ki_flags & IOCB_NOWAIT))
209 return false;
210 break;
211 }
212
213complete:
214 nvmet_file_io_done(&req->f.iocb, ret, 0);
215 return true;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400216}
217
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -0400218static void nvmet_file_buffered_io_work(struct work_struct *w)
219{
220 struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
221
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500222 nvmet_file_execute_io(req, 0);
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -0400223}
224
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500225static void nvmet_file_submit_buffered_io(struct nvmet_req *req)
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -0400226{
227 INIT_WORK(&req->f.work, nvmet_file_buffered_io_work);
228 queue_work(buffered_io_wq, &req->f.work);
229}
230
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500231static void nvmet_file_execute_rw(struct nvmet_req *req)
232{
Ming Lei02db9952019-03-27 17:07:22 +0800233 ssize_t nr_bvec = req->sg_cnt;
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500234
Christoph Hellwige9061c32019-10-23 10:35:44 -0600235 if (!nvmet_check_data_len(req, nvmet_rw_len(req)))
236 return;
237
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500238 if (!req->sg_cnt || !nr_bvec) {
239 nvmet_req_complete(req, 0);
240 return;
241 }
242
243 if (nr_bvec > NVMET_MAX_INLINE_BIOVEC)
244 req->f.bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec),
245 GFP_KERNEL);
246 else
247 req->f.bvec = req->inline_bvec;
248
249 if (unlikely(!req->f.bvec)) {
250 /* fallback under memory pressure */
251 req->f.bvec = mempool_alloc(req->ns->bvec_pool, GFP_KERNEL);
252 req->f.mpool_alloc = true;
253 } else
254 req->f.mpool_alloc = false;
255
256 if (req->ns->buffered_io) {
257 if (likely(!req->f.mpool_alloc) &&
258 nvmet_file_execute_io(req, IOCB_NOWAIT))
259 return;
260 nvmet_file_submit_buffered_io(req);
261 } else
262 nvmet_file_execute_io(req, 0);
263}
264
Chaitanya Kulkarnidedf0be2018-08-07 23:01:07 -0700265u16 nvmet_file_flush(struct nvmet_req *req)
266{
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800267 return errno_to_nvme_status(req, vfs_fsync(req->ns->file, 1));
Chaitanya Kulkarnidedf0be2018-08-07 23:01:07 -0700268}
269
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400270static void nvmet_file_flush_work(struct work_struct *w)
271{
272 struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400273
Chaitanya Kulkarnidedf0be2018-08-07 23:01:07 -0700274 nvmet_req_complete(req, nvmet_file_flush(req));
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400275}
276
277static void nvmet_file_execute_flush(struct nvmet_req *req)
278{
Christoph Hellwige9061c32019-10-23 10:35:44 -0600279 if (!nvmet_check_data_len(req, 0))
280 return;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400281 INIT_WORK(&req->f.work, nvmet_file_flush_work);
282 schedule_work(&req->f.work);
283}
284
285static void nvmet_file_execute_discard(struct nvmet_req *req)
286{
287 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
288 struct nvme_dsm_range range;
Sagi Grimberg1b72b712018-07-11 12:43:16 +0300289 loff_t offset, len;
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800290 u16 status = 0;
291 int ret;
Sagi Grimberg1b72b712018-07-11 12:43:16 +0300292 int i;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400293
294 for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) {
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800295 status = nvmet_copy_from_sgl(req, i * sizeof(range), &range,
Sagi Grimberg1b72b712018-07-11 12:43:16 +0300296 sizeof(range));
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800297 if (status)
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400298 break;
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300299
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400300 offset = le64_to_cpu(range.slba) << req->ns->blksize_shift;
Bart Van Assche8eacd1b2018-10-08 14:28:52 -0700301 len = le32_to_cpu(range.nlb);
302 len <<= req->ns->blksize_shift;
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300303 if (offset + len > req->ns->size) {
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800304 req->error_slba = le64_to_cpu(range.slba);
305 status = errno_to_nvme_status(req, -ENOSPC);
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300306 break;
307 }
308
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800309 ret = vfs_fallocate(req->ns->file, mode, offset, len);
Christoph Hellwig005c6742019-03-13 18:55:09 +0100310 if (ret && ret != -EOPNOTSUPP) {
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800311 req->error_slba = le64_to_cpu(range.slba);
312 status = errno_to_nvme_status(req, ret);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400313 break;
Sagi Grimberg1b72b712018-07-11 12:43:16 +0300314 }
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400315 }
316
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800317 nvmet_req_complete(req, status);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400318}
319
320static void nvmet_file_dsm_work(struct work_struct *w)
321{
322 struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
323
324 switch (le32_to_cpu(req->cmd->dsm.attributes)) {
325 case NVME_DSMGMT_AD:
326 nvmet_file_execute_discard(req);
327 return;
328 case NVME_DSMGMT_IDR:
329 case NVME_DSMGMT_IDW:
330 default:
331 /* Not supported yet */
332 nvmet_req_complete(req, 0);
333 return;
334 }
335}
336
337static void nvmet_file_execute_dsm(struct nvmet_req *req)
338{
Christoph Hellwige9061c32019-10-23 10:35:44 -0600339 if (!nvmet_check_data_len(req, nvmet_dsm_len(req)))
340 return;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400341 INIT_WORK(&req->f.work, nvmet_file_dsm_work);
342 schedule_work(&req->f.work);
343}
344
345static void nvmet_file_write_zeroes_work(struct work_struct *w)
346{
347 struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
348 struct nvme_write_zeroes_cmd *write_zeroes = &req->cmd->write_zeroes;
349 int mode = FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE;
350 loff_t offset;
351 loff_t len;
352 int ret;
353
354 offset = le64_to_cpu(write_zeroes->slba) << req->ns->blksize_shift;
355 len = (((sector_t)le16_to_cpu(write_zeroes->length) + 1) <<
356 req->ns->blksize_shift);
357
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300358 if (unlikely(offset + len > req->ns->size)) {
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800359 nvmet_req_complete(req, errno_to_nvme_status(req, -ENOSPC));
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300360 return;
361 }
362
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400363 ret = vfs_fallocate(req->ns->file, mode, offset, len);
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800364 nvmet_req_complete(req, ret < 0 ? errno_to_nvme_status(req, ret) : 0);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400365}
366
367static void nvmet_file_execute_write_zeroes(struct nvmet_req *req)
368{
Christoph Hellwige9061c32019-10-23 10:35:44 -0600369 if (!nvmet_check_data_len(req, 0))
370 return;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400371 INIT_WORK(&req->f.work, nvmet_file_write_zeroes_work);
372 schedule_work(&req->f.work);
373}
374
375u16 nvmet_file_parse_io_cmd(struct nvmet_req *req)
376{
377 struct nvme_command *cmd = req->cmd;
378
379 switch (cmd->common.opcode) {
380 case nvme_cmd_read:
381 case nvme_cmd_write:
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500382 req->execute = nvmet_file_execute_rw;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400383 return 0;
384 case nvme_cmd_flush:
385 req->execute = nvmet_file_execute_flush;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400386 return 0;
387 case nvme_cmd_dsm:
388 req->execute = nvmet_file_execute_dsm;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400389 return 0;
390 case nvme_cmd_write_zeroes:
391 req->execute = nvmet_file_execute_write_zeroes;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400392 return 0;
393 default:
394 pr_err("unhandled cmd for file ns %d on qid %d\n",
395 cmd->common.opcode, req->sq->qid);
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800396 req->error_loc = offsetof(struct nvme_common_command, opcode);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400397 return NVME_SC_INVALID_OPCODE | NVME_SC_DNR;
398 }
399}