blob: 7fdbdc496597d5304eb7f6938b5cc12f1d890507 [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)) {
Daniel Wagner85428be2021-05-12 16:50:05 +020052 ret = PTR_ERR(ns->file);
53 pr_err("failed to open file %s: (%d)\n",
54 ns->device_path, ret);
55 ns->file = NULL;
56 return ret;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040057 }
58
Anthony Iliopoulose8cd1ff2020-04-19 16:48:50 -070059 ret = nvmet_file_ns_revalidate(ns);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040060 if (ret)
61 goto err;
62
Sagi Grimberg525ec492019-04-24 11:43:23 -070063 /*
64 * i_blkbits can be greater than the universally accepted upper bound,
65 * so make sure we export a sane namespace lba_shift.
66 */
67 ns->blksize_shift = min_t(u8,
68 file_inode(ns->file)->i_blkbits, 12);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040069
70 ns->bvec_cache = kmem_cache_create("nvmet-bvec",
71 NVMET_MAX_MPOOL_BVEC * sizeof(struct bio_vec),
72 0, SLAB_HWCACHE_ALIGN, NULL);
Wei Yongjun1367bc82018-05-31 11:41:30 +000073 if (!ns->bvec_cache) {
74 ret = -ENOMEM;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040075 goto err;
Wei Yongjun1367bc82018-05-31 11:41:30 +000076 }
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040077
78 ns->bvec_pool = mempool_create(NVMET_MIN_MPOOL_OBJ, mempool_alloc_slab,
79 mempool_free_slab, ns->bvec_cache);
80
Wei Yongjun1367bc82018-05-31 11:41:30 +000081 if (!ns->bvec_pool) {
82 ret = -ENOMEM;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040083 goto err;
Wei Yongjun1367bc82018-05-31 11:41:30 +000084 }
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040085
86 return ret;
87err:
88 ns->size = 0;
89 ns->blksize_shift = 0;
90 nvmet_file_ns_disable(ns);
91 return ret;
92}
93
Ming Lei02db9952019-03-27 17:07:22 +080094static void nvmet_file_init_bvec(struct bio_vec *bv, struct scatterlist *sg)
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040095{
Ming Lei02db9952019-03-27 17:07:22 +080096 bv->bv_page = sg_page(sg);
97 bv->bv_offset = sg->offset;
98 bv->bv_len = sg->length;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -040099}
100
101static ssize_t nvmet_file_submit_bvec(struct nvmet_req *req, loff_t pos,
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500102 unsigned long nr_segs, size_t count, int ki_flags)
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400103{
104 struct kiocb *iocb = &req->f.iocb;
105 ssize_t (*call_iter)(struct kiocb *iocb, struct iov_iter *iter);
106 struct iov_iter iter;
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500107 int rw;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400108
109 if (req->cmd->rw.opcode == nvme_cmd_write) {
110 if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA))
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500111 ki_flags |= IOCB_DSYNC;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400112 call_iter = req->ns->file->f_op->write_iter;
113 rw = WRITE;
114 } else {
115 call_iter = req->ns->file->f_op->read_iter;
116 rw = READ;
117 }
118
David Howellsaa563d72018-10-20 00:57:56 +0100119 iov_iter_bvec(&iter, rw, req->f.bvec, nr_segs, count);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400120
121 iocb->ki_pos = pos;
122 iocb->ki_filp = req->ns->file;
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -0400123 iocb->ki_flags = ki_flags | iocb_flags(req->ns->file);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400124
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500125 return call_iter(iocb, &iter);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400126}
127
128static void nvmet_file_io_done(struct kiocb *iocb, long ret, long ret2)
129{
130 struct nvmet_req *req = container_of(iocb, struct nvmet_req, f.iocb);
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800131 u16 status = NVME_SC_SUCCESS;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400132
133 if (req->f.bvec != req->inline_bvec) {
134 if (likely(req->f.mpool_alloc == false))
135 kfree(req->f.bvec);
136 else
137 mempool_free(req->f.bvec, req->ns->bvec_pool);
138 }
139
Christoph Hellwige9061c32019-10-23 10:35:44 -0600140 if (unlikely(ret != req->transfer_len))
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800141 status = errno_to_nvme_status(req, ret);
142 nvmet_req_complete(req, status);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400143}
144
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500145static bool nvmet_file_execute_io(struct nvmet_req *req, int ki_flags)
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400146{
Ming Lei02db9952019-03-27 17:07:22 +0800147 ssize_t nr_bvec = req->sg_cnt;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400148 unsigned long bv_cnt = 0;
149 bool is_sync = false;
150 size_t len = 0, total_len = 0;
151 ssize_t ret = 0;
152 loff_t pos;
Ming Lei02db9952019-03-27 17:07:22 +0800153 int i;
154 struct scatterlist *sg;
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500155
156 if (req->f.mpool_alloc && nr_bvec > NVMET_MAX_MPOOL_BVEC)
157 is_sync = true;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400158
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300159 pos = le64_to_cpu(req->cmd->rw.slba) << req->ns->blksize_shift;
Christoph Hellwige9061c32019-10-23 10:35:44 -0600160 if (unlikely(pos + req->transfer_len > req->ns->size)) {
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800161 nvmet_req_complete(req, errno_to_nvme_status(req, -ENOSPC));
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500162 return true;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400163 }
164
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400165 memset(&req->f.iocb, 0, sizeof(struct kiocb));
Ming Lei02db9952019-03-27 17:07:22 +0800166 for_each_sg(req->sg, sg, req->sg_cnt, i) {
167 nvmet_file_init_bvec(&req->f.bvec[bv_cnt], sg);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400168 len += req->f.bvec[bv_cnt].bv_len;
169 total_len += req->f.bvec[bv_cnt].bv_len;
170 bv_cnt++;
171
172 WARN_ON_ONCE((nr_bvec - 1) < 0);
173
174 if (unlikely(is_sync) &&
175 (nr_bvec - 1 == 0 || bv_cnt == NVMET_MAX_MPOOL_BVEC)) {
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500176 ret = nvmet_file_submit_bvec(req, pos, bv_cnt, len, 0);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400177 if (ret < 0)
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500178 goto complete;
179
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400180 pos += len;
181 bv_cnt = 0;
182 len = 0;
183 }
184 nr_bvec--;
185 }
186
Christoph Hellwige9061c32019-10-23 10:35:44 -0600187 if (WARN_ON_ONCE(total_len != req->transfer_len)) {
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400188 ret = -EIO;
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500189 goto complete;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400190 }
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500191
192 if (unlikely(is_sync)) {
193 ret = total_len;
194 goto complete;
195 }
196
197 /*
198 * A NULL ki_complete ask for synchronous execution, which we want
199 * for the IOCB_NOWAIT case.
200 */
201 if (!(ki_flags & IOCB_NOWAIT))
202 req->f.iocb.ki_complete = nvmet_file_io_done;
203
204 ret = nvmet_file_submit_bvec(req, pos, bv_cnt, total_len, ki_flags);
205
206 switch (ret) {
207 case -EIOCBQUEUED:
208 return true;
209 case -EAGAIN:
210 if (WARN_ON_ONCE(!(ki_flags & IOCB_NOWAIT)))
211 goto complete;
212 return false;
213 case -EOPNOTSUPP:
214 /*
215 * For file systems returning error -EOPNOTSUPP, handle
216 * IOCB_NOWAIT error case separately and retry without
217 * IOCB_NOWAIT.
218 */
219 if ((ki_flags & IOCB_NOWAIT))
220 return false;
221 break;
222 }
223
224complete:
225 nvmet_file_io_done(&req->f.iocb, ret, 0);
226 return true;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400227}
228
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -0400229static void nvmet_file_buffered_io_work(struct work_struct *w)
230{
231 struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
232
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500233 nvmet_file_execute_io(req, 0);
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -0400234}
235
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500236static void nvmet_file_submit_buffered_io(struct nvmet_req *req)
Chaitanya Kulkarni55eb942e2018-06-20 00:01:41 -0400237{
238 INIT_WORK(&req->f.work, nvmet_file_buffered_io_work);
239 queue_work(buffered_io_wq, &req->f.work);
240}
241
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500242static void nvmet_file_execute_rw(struct nvmet_req *req)
243{
Ming Lei02db9952019-03-27 17:07:22 +0800244 ssize_t nr_bvec = req->sg_cnt;
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500245
Israel Rukshin136cc1f2020-05-19 17:05:59 +0300246 if (!nvmet_check_transfer_len(req, nvmet_rw_data_len(req)))
Christoph Hellwige9061c32019-10-23 10:35:44 -0600247 return;
248
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500249 if (!req->sg_cnt || !nr_bvec) {
250 nvmet_req_complete(req, 0);
251 return;
252 }
253
254 if (nr_bvec > NVMET_MAX_INLINE_BIOVEC)
255 req->f.bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec),
256 GFP_KERNEL);
257 else
258 req->f.bvec = req->inline_bvec;
259
260 if (unlikely(!req->f.bvec)) {
261 /* fallback under memory pressure */
262 req->f.bvec = mempool_alloc(req->ns->bvec_pool, GFP_KERNEL);
263 req->f.mpool_alloc = true;
264 } else
265 req->f.mpool_alloc = false;
266
267 if (req->ns->buffered_io) {
268 if (likely(!req->f.mpool_alloc) &&
269 nvmet_file_execute_io(req, IOCB_NOWAIT))
270 return;
271 nvmet_file_submit_buffered_io(req);
272 } else
273 nvmet_file_execute_io(req, 0);
274}
275
Chaitanya Kulkarnidedf0be2018-08-07 23:01:07 -0700276u16 nvmet_file_flush(struct nvmet_req *req)
277{
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800278 return errno_to_nvme_status(req, vfs_fsync(req->ns->file, 1));
Chaitanya Kulkarnidedf0be2018-08-07 23:01:07 -0700279}
280
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400281static void nvmet_file_flush_work(struct work_struct *w)
282{
283 struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400284
Chaitanya Kulkarnidedf0be2018-08-07 23:01:07 -0700285 nvmet_req_complete(req, nvmet_file_flush(req));
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400286}
287
288static void nvmet_file_execute_flush(struct nvmet_req *req)
289{
Israel Rukshin136cc1f2020-05-19 17:05:59 +0300290 if (!nvmet_check_transfer_len(req, 0))
Christoph Hellwige9061c32019-10-23 10:35:44 -0600291 return;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400292 INIT_WORK(&req->f.work, nvmet_file_flush_work);
293 schedule_work(&req->f.work);
294}
295
296static void nvmet_file_execute_discard(struct nvmet_req *req)
297{
298 int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE;
299 struct nvme_dsm_range range;
Sagi Grimberg1b72b712018-07-11 12:43:16 +0300300 loff_t offset, len;
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800301 u16 status = 0;
302 int ret;
Sagi Grimberg1b72b712018-07-11 12:43:16 +0300303 int i;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400304
305 for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) {
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800306 status = nvmet_copy_from_sgl(req, i * sizeof(range), &range,
Sagi Grimberg1b72b712018-07-11 12:43:16 +0300307 sizeof(range));
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800308 if (status)
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400309 break;
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300310
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400311 offset = le64_to_cpu(range.slba) << req->ns->blksize_shift;
Bart Van Assche8eacd1b2018-10-08 14:28:52 -0700312 len = le32_to_cpu(range.nlb);
313 len <<= req->ns->blksize_shift;
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300314 if (offset + len > req->ns->size) {
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800315 req->error_slba = le64_to_cpu(range.slba);
316 status = errno_to_nvme_status(req, -ENOSPC);
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300317 break;
318 }
319
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800320 ret = vfs_fallocate(req->ns->file, mode, offset, len);
Christoph Hellwig005c6742019-03-13 18:55:09 +0100321 if (ret && ret != -EOPNOTSUPP) {
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800322 req->error_slba = le64_to_cpu(range.slba);
323 status = errno_to_nvme_status(req, ret);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400324 break;
Sagi Grimberg1b72b712018-07-11 12:43:16 +0300325 }
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400326 }
327
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800328 nvmet_req_complete(req, status);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400329}
330
331static void nvmet_file_dsm_work(struct work_struct *w)
332{
333 struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
334
335 switch (le32_to_cpu(req->cmd->dsm.attributes)) {
336 case NVME_DSMGMT_AD:
337 nvmet_file_execute_discard(req);
338 return;
339 case NVME_DSMGMT_IDR:
340 case NVME_DSMGMT_IDW:
341 default:
342 /* Not supported yet */
343 nvmet_req_complete(req, 0);
344 return;
345 }
346}
347
348static void nvmet_file_execute_dsm(struct nvmet_req *req)
349{
Sagi Grimbergb716e682020-01-26 23:23:28 -0800350 if (!nvmet_check_data_len_lte(req, nvmet_dsm_len(req)))
Christoph Hellwige9061c32019-10-23 10:35:44 -0600351 return;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400352 INIT_WORK(&req->f.work, nvmet_file_dsm_work);
353 schedule_work(&req->f.work);
354}
355
356static void nvmet_file_write_zeroes_work(struct work_struct *w)
357{
358 struct nvmet_req *req = container_of(w, struct nvmet_req, f.work);
359 struct nvme_write_zeroes_cmd *write_zeroes = &req->cmd->write_zeroes;
360 int mode = FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE;
361 loff_t offset;
362 loff_t len;
363 int ret;
364
365 offset = le64_to_cpu(write_zeroes->slba) << req->ns->blksize_shift;
366 len = (((sector_t)le16_to_cpu(write_zeroes->length) + 1) <<
367 req->ns->blksize_shift);
368
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300369 if (unlikely(offset + len > req->ns->size)) {
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800370 nvmet_req_complete(req, errno_to_nvme_status(req, -ENOSPC));
Sagi Grimberg9c891c12018-07-11 16:13:04 +0300371 return;
372 }
373
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400374 ret = vfs_fallocate(req->ns->file, mode, offset, len);
Chaitanya Kulkarnic6aa3542018-12-12 15:11:43 -0800375 nvmet_req_complete(req, ret < 0 ? errno_to_nvme_status(req, ret) : 0);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400376}
377
378static void nvmet_file_execute_write_zeroes(struct nvmet_req *req)
379{
Israel Rukshin136cc1f2020-05-19 17:05:59 +0300380 if (!nvmet_check_transfer_len(req, 0))
Christoph Hellwige9061c32019-10-23 10:35:44 -0600381 return;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400382 INIT_WORK(&req->f.work, nvmet_file_write_zeroes_work);
383 schedule_work(&req->f.work);
384}
385
386u16 nvmet_file_parse_io_cmd(struct nvmet_req *req)
387{
388 struct nvme_command *cmd = req->cmd;
389
390 switch (cmd->common.opcode) {
391 case nvme_cmd_read:
392 case nvme_cmd_write:
Chaitanya Kulkarni50a909d2018-11-14 01:12:19 -0500393 req->execute = nvmet_file_execute_rw;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400394 return 0;
395 case nvme_cmd_flush:
396 req->execute = nvmet_file_execute_flush;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400397 return 0;
398 case nvme_cmd_dsm:
399 req->execute = nvmet_file_execute_dsm;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400400 return 0;
401 case nvme_cmd_write_zeroes:
402 req->execute = nvmet_file_execute_write_zeroes;
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400403 return 0;
404 default:
Chaitanya Kulkarni1c2c7612021-02-09 21:47:57 -0800405 return nvmet_report_invalid_opcode(req);
Chaitanya Kulkarnid5eff332018-05-23 00:34:39 -0400406 }
407}