blob: 715d4376c997975a743bedc908218b23325a7653 [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
Anthony Iliopoulose8cd1ff2020-04-19 16:48:50 -070016int nvmet_file_ns_revalidate(struct nvmet_ns *ns)
17{
18 struct kstat stat;
19 int ret;
20
21 ret = vfs_getattr(&ns->file->f_path, &stat, STATX_SIZE,
22 AT_STATX_FORCE_SYNC);
23 if (!ret)
24 ns->size = stat.size;
25 return ret;
26}
27
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040028void nvmet_file_ns_disable(struct nvmet_ns *ns)
29{
30 if (ns->file) {
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -040031 if (ns->buffered_io)
32 flush_workqueue(buffered_io_wq);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040033 mempool_destroy(ns->bvec_pool);
34 ns->bvec_pool = NULL;
35 kmem_cache_destroy(ns->bvec_cache);
36 ns->bvec_cache = NULL;
37 fput(ns->file);
38 ns->file = NULL;
39 }
40}
41
42int nvmet_file_ns_enable(struct nvmet_ns *ns)
43{
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -040044 int flags = O_RDWR | O_LARGEFILE;
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -040045 int ret;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040046
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -040047 if (!ns->buffered_io)
48 flags |= O_DIRECT;
49
50 ns->file = filp_open(ns->device_path, flags, 0);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040051 if (IS_ERR(ns->file)) {
52 pr_err("failed to open file %s: (%ld)\n",
Wei Yongjun81cf54e02018-05-31 01:55:58 +000053 ns->device_path, PTR_ERR(ns->file));
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040054 return PTR_ERR(ns->file);
55 }
56
Anthony Iliopoulose8cd1ff2020-04-19 16:48:50 -070057 ret = nvmet_file_ns_revalidate(ns);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040058 if (ret)
59 goto err;
60
Sagi Grimberg525ec492019-04-24 11:43:23 -070061 /*
62 * i_blkbits can be greater than the universally accepted upper bound,
63 * so make sure we export a sane namespace lba_shift.
64 */
65 ns->blksize_shift = min_t(u8,
66 file_inode(ns->file)->i_blkbits, 12);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040067
68 ns->bvec_cache = kmem_cache_create("nvmet-bvec",
69 NVMET_MAX_MPOOL_BVEC * sizeof(struct bio_vec),
70 0, SLAB_HWCACHE_ALIGN, NULL);
Wei Yongjun1367bc82018-05-31 11:41:30 +000071 if (!ns->bvec_cache) {
72 ret = -ENOMEM;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040073 goto err;
Wei Yongjun1367bc82018-05-31 11:41:30 +000074 }
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040075
76 ns->bvec_pool = mempool_create(NVMET_MIN_MPOOL_OBJ, mempool_alloc_slab,
77 mempool_free_slab, ns->bvec_cache);
78
Wei Yongjun1367bc82018-05-31 11:41:30 +000079 if (!ns->bvec_pool) {
80 ret = -ENOMEM;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040081 goto err;
Wei Yongjun1367bc82018-05-31 11:41:30 +000082 }
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040083
84 return ret;
85err:
86 ns->size = 0;
87 ns->blksize_shift = 0;
88 nvmet_file_ns_disable(ns);
89 return ret;
90}
91
Ming Lei02db9952019-03-27 17:07:22 +080092static void nvmet_file_init_bvec(struct bio_vec *bv, struct scatterlist *sg)
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040093{
Ming Lei02db9952019-03-27 17:07:22 +080094 bv->bv_page = sg_page(sg);
95 bv->bv_offset = sg->offset;
96 bv->bv_len = sg->length;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040097}
98
99static ssize_t nvmet_file_submit_bvec(struct nvmet_req *req, loff_t pos,
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500100 unsigned long nr_segs, size_t count, int ki_flags)
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400101{
102 struct kiocb *iocb = &req->f.iocb;
103 ssize_t (*call_iter)(struct kiocb *iocb, struct iov_iter *iter);
104 struct iov_iter iter;
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500105 int rw;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400106
107 if (req->cmd->rw.opcode == nvme_cmd_write) {
108 if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500109 ki_flags |= IOCB_DSYNC;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400110 call_iter = req->ns->file->f_op->write_iter;
111 rw = WRITE;
112 } else {
113 call_iter = req->ns->file->f_op->read_iter;
114 rw = READ;
115 }
116
David Howellsaa563d72018-10-20 00:57:56 +0100117 iov_iter_bvec(&iter, rw, req->f.bvec, nr_segs, count);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400118
119 iocb->ki_pos = pos;
120 iocb->ki_filp = req->ns->file;
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -0400121 iocb->ki_flags = ki_flags | iocb_flags(req->ns->file);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400122
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500123 return call_iter(iocb, &iter);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400124}
125
126static void nvmet_file_io_done(struct kiocb *iocb, long ret, long ret2)
127{
128 struct nvmet_req *req = container_of(iocb, struct nvmet_req, f.iocb);
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800129 u16 status = NVME_SC_SUCCESS;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400130
131 if (req->f.bvec != req->inline_bvec) {
132 if (likely(req->f.mpool_alloc == false))
133 kfree(req->f.bvec);
134 else
135 mempool_free(req->f.bvec, req->ns->bvec_pool);
136 }
137
Christoph Hellwige9061c32019-10-23 10:35:44 -0600138 if (unlikely(ret != req->transfer_len))
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800139 status = errno_to_nvme_status(req, ret);
140 nvmet_req_complete(req, status);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400141}
142
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500143static bool nvmet_file_execute_io(struct nvmet_req *req, int ki_flags)
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400144{
Ming Lei02db9952019-03-27 17:07:22 +0800145 ssize_t nr_bvec = req->sg_cnt;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400146 unsigned long bv_cnt = 0;
147 bool is_sync = false;
148 size_t len = 0, total_len = 0;
149 ssize_t ret = 0;
150 loff_t pos;
Ming Lei02db9952019-03-27 17:07:22 +0800151 int i;
152 struct scatterlist *sg;
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500153
154 if (req->f.mpool_alloc && nr_bvec > NVMET_MAX_MPOOL_BVEC)
155 is_sync = true;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400156
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300157 pos = le64_to_cpu(req->cmd->rw.slba) << req->ns->blksize_shift;
Christoph Hellwige9061c32019-10-23 10:35:44 -0600158 if (unlikely(pos + req->transfer_len > req->ns->size)) {
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800159 nvmet_req_complete(req, errno_to_nvme_status(req, -ENOSPC));
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500160 return true;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400161 }
162
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400163 memset(&req->f.iocb, 0, sizeof(struct kiocb));
Ming Lei02db9952019-03-27 17:07:22 +0800164 for_each_sg(req->sg, sg, req->sg_cnt, i) {
165 nvmet_file_init_bvec(&req->f.bvec[bv_cnt], sg);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400166 len += req->f.bvec[bv_cnt].bv_len;
167 total_len += req->f.bvec[bv_cnt].bv_len;
168 bv_cnt++;
169
170 WARN_ON_ONCE((nr_bvec - 1) < 0);
171
172 if (unlikely(is_sync) &&
173 (nr_bvec - 1 == 0 || bv_cnt == NVMET_MAX_MPOOL_BVEC)) {
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500174 ret = nvmet_file_submit_bvec(req, pos, bv_cnt, len, 0);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400175 if (ret < 0)
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500176 goto complete;
177
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400178 pos += len;
179 bv_cnt = 0;
180 len = 0;
181 }
182 nr_bvec--;
183 }
184
Christoph Hellwige9061c32019-10-23 10:35:44 -0600185 if (WARN_ON_ONCE(total_len != req->transfer_len)) {
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400186 ret = -EIO;
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500187 goto complete;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400188 }
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500189
190 if (unlikely(is_sync)) {
191 ret = total_len;
192 goto complete;
193 }
194
195 /*
196 * A NULL ki_complete ask for synchronous execution, which we want
197 * for the IOCB_NOWAIT case.
198 */
199 if (!(ki_flags & IOCB_NOWAIT))
200 req->f.iocb.ki_complete = nvmet_file_io_done;
201
202 ret = nvmet_file_submit_bvec(req, pos, bv_cnt, total_len, ki_flags);
203
204 switch (ret) {
205 case -EIOCBQUEUED:
206 return true;
207 case -EAGAIN:
208 if (WARN_ON_ONCE(!(ki_flags & IOCB_NOWAIT)))
209 goto complete;
210 return false;
211 case -EOPNOTSUPP:
212 /*
213 * For file systems returning error -EOPNOTSUPP, handle
214 * IOCB_NOWAIT error case separately and retry without
215 * IOCB_NOWAIT.
216 */
217 if ((ki_flags & IOCB_NOWAIT))
218 return false;
219 break;
220 }
221
222complete:
223 nvmet_file_io_done(&req->f.iocb, ret, 0);
224 return true;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400225}
226
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -0400227static void nvmet_file_buffered_io_work(struct work_struct *w)
228{
229 struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
230
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500231 nvmet_file_execute_io(req, 0);
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -0400232}
233
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500234static void nvmet_file_submit_buffered_io(struct nvmet_req *req)
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -0400235{
236 INIT_WORK(&req->f.work, nvmet_file_buffered_io_work);
237 queue_work(buffered_io_wq, &req->f.work);
238}
239
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500240static void nvmet_file_execute_rw(struct nvmet_req *req)
241{
Ming Lei02db9952019-03-27 17:07:22 +0800242 ssize_t nr_bvec = req->sg_cnt;
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500243
Israel Rukshin136cc1f2020-05-19 17:05:59 +0300244 if (!nvmet_check_transfer_len(req, nvmet_rw_data_len(req)))
Christoph Hellwige9061c32019-10-23 10:35:44 -0600245 return;
246
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500247 if (!req->sg_cnt || !nr_bvec) {
248 nvmet_req_complete(req, 0);
249 return;
250 }
251
252 if (nr_bvec > NVMET_MAX_INLINE_BIOVEC)
253 req->f.bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec),
254 GFP_KERNEL);
255 else
256 req->f.bvec = req->inline_bvec;
257
258 if (unlikely(!req->f.bvec)) {
259 /* fallback under memory pressure */
260 req->f.bvec = mempool_alloc(req->ns->bvec_pool, GFP_KERNEL);
261 req->f.mpool_alloc = true;
262 } else
263 req->f.mpool_alloc = false;
264
265 if (req->ns->buffered_io) {
266 if (likely(!req->f.mpool_alloc) &&
267 nvmet_file_execute_io(req, IOCB_NOWAIT))
268 return;
269 nvmet_file_submit_buffered_io(req);
270 } else
271 nvmet_file_execute_io(req, 0);
272}
273
Chaitanya Kulkarnidedf0be2018-08-07 23:01:07 -0700274u16 nvmet_file_flush(struct nvmet_req *req)
275{
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800276 return errno_to_nvme_status(req, vfs_fsync(req->ns->file, 1));
Chaitanya Kulkarnidedf0be2018-08-07 23:01:07 -0700277}
278
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400279static void nvmet_file_flush_work(struct work_struct *w)
280{
281 struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400282
Chaitanya Kulkarnidedf0be2018-08-07 23:01:07 -0700283 nvmet_req_complete(req, nvmet_file_flush(req));
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400284}
285
286static void nvmet_file_execute_flush(struct nvmet_req *req)
287{
Israel Rukshin136cc1f2020-05-19 17:05:59 +0300288 if (!nvmet_check_transfer_len(req, 0))
Christoph Hellwige9061c32019-10-23 10:35:44 -0600289 return;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400290 INIT_WORK(&req->f.work, nvmet_file_flush_work);
291 schedule_work(&req->f.work);
292}
293
294static void nvmet_file_execute_discard(struct nvmet_req *req)
295{
296 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
297 struct nvme_dsm_range range;
Sagi Grimberg1b72b712018-07-11 12:43:16 +0300298 loff_t offset, len;
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800299 u16 status = 0;
300 int ret;
Sagi Grimberg1b72b712018-07-11 12:43:16 +0300301 int i;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400302
303 for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) {
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800304 status = nvmet_copy_from_sgl(req, i * sizeof(range), &range,
Sagi Grimberg1b72b712018-07-11 12:43:16 +0300305 sizeof(range));
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800306 if (status)
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400307 break;
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300308
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400309 offset = le64_to_cpu(range.slba) << req->ns->blksize_shift;
Bart Van Assche8eacd1b2018-10-08 14:28:52 -0700310 len = le32_to_cpu(range.nlb);
311 len <<= req->ns->blksize_shift;
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300312 if (offset + len > req->ns->size) {
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800313 req->error_slba = le64_to_cpu(range.slba);
314 status = errno_to_nvme_status(req, -ENOSPC);
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300315 break;
316 }
317
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800318 ret = vfs_fallocate(req->ns->file, mode, offset, len);
Christoph Hellwig005c6742019-03-13 18:55:09 +0100319 if (ret && ret != -EOPNOTSUPP) {
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800320 req->error_slba = le64_to_cpu(range.slba);
321 status = errno_to_nvme_status(req, ret);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400322 break;
Sagi Grimberg1b72b712018-07-11 12:43:16 +0300323 }
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400324 }
325
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800326 nvmet_req_complete(req, status);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400327}
328
329static void nvmet_file_dsm_work(struct work_struct *w)
330{
331 struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
332
333 switch (le32_to_cpu(req->cmd->dsm.attributes)) {
334 case NVME_DSMGMT_AD:
335 nvmet_file_execute_discard(req);
336 return;
337 case NVME_DSMGMT_IDR:
338 case NVME_DSMGMT_IDW:
339 default:
340 /* Not supported yet */
341 nvmet_req_complete(req, 0);
342 return;
343 }
344}
345
346static void nvmet_file_execute_dsm(struct nvmet_req *req)
347{
Sagi Grimbergb716e682020-01-26 23:23:28 -0800348 if (!nvmet_check_data_len_lte(req, nvmet_dsm_len(req)))
Christoph Hellwige9061c32019-10-23 10:35:44 -0600349 return;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400350 INIT_WORK(&req->f.work, nvmet_file_dsm_work);
351 schedule_work(&req->f.work);
352}
353
354static void nvmet_file_write_zeroes_work(struct work_struct *w)
355{
356 struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
357 struct nvme_write_zeroes_cmd *write_zeroes = &req->cmd->write_zeroes;
358 int mode = FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE;
359 loff_t offset;
360 loff_t len;
361 int ret;
362
363 offset = le64_to_cpu(write_zeroes->slba) << req->ns->blksize_shift;
364 len = (((sector_t)le16_to_cpu(write_zeroes->length) + 1) <<
365 req->ns->blksize_shift);
366
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300367 if (unlikely(offset + len > req->ns->size)) {
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800368 nvmet_req_complete(req, errno_to_nvme_status(req, -ENOSPC));
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300369 return;
370 }
371
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400372 ret = vfs_fallocate(req->ns->file, mode, offset, len);
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800373 nvmet_req_complete(req, ret < 0 ? errno_to_nvme_status(req, ret) : 0);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400374}
375
376static void nvmet_file_execute_write_zeroes(struct nvmet_req *req)
377{
Israel Rukshin136cc1f2020-05-19 17:05:59 +0300378 if (!nvmet_check_transfer_len(req, 0))
Christoph Hellwige9061c32019-10-23 10:35:44 -0600379 return;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400380 INIT_WORK(&req->f.work, nvmet_file_write_zeroes_work);
381 schedule_work(&req->f.work);
382}
383
384u16 nvmet_file_parse_io_cmd(struct nvmet_req *req)
385{
386 struct nvme_command *cmd = req->cmd;
387
388 switch (cmd->common.opcode) {
389 case nvme_cmd_read:
390 case nvme_cmd_write:
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500391 req->execute = nvmet_file_execute_rw;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400392 return 0;
393 case nvme_cmd_flush:
394 req->execute = nvmet_file_execute_flush;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400395 return 0;
396 case nvme_cmd_dsm:
397 req->execute = nvmet_file_execute_dsm;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400398 return 0;
399 case nvme_cmd_write_zeroes:
400 req->execute = nvmet_file_execute_write_zeroes;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400401 return 0;
402 default:
Chaitanya Kulkarni1c2c7612021-02-09 21:47:57 -0800403 return nvmet_report_invalid_opcode(req);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400404 }
405}