Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 1 | // 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> |
Maurizio Lombardi | f2a58ff | 2021-11-22 11:08:27 +0100 | [diff] [blame] | 11 | #include <linux/fs.h> |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 12 | #include "nvmet.h" |
| 13 | |
| 14 | #define NVMET_MAX_MPOOL_BVEC 16 |
| 15 | #define NVMET_MIN_MPOOL_OBJ 16 |
| 16 | |
Anthony Iliopoulos | e8cd1ff | 2020-04-19 16:48:50 -0700 | [diff] [blame] | 17 | int nvmet_file_ns_revalidate(struct nvmet_ns *ns) |
| 18 | { |
| 19 | struct kstat stat; |
| 20 | int ret; |
| 21 | |
| 22 | ret = vfs_getattr(&ns->file->f_path, &stat, STATX_SIZE, |
| 23 | AT_STATX_FORCE_SYNC); |
| 24 | if (!ret) |
| 25 | ns->size = stat.size; |
| 26 | return ret; |
| 27 | } |
| 28 | |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 29 | void nvmet_file_ns_disable(struct nvmet_ns *ns) |
| 30 | { |
| 31 | if (ns->file) { |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 32 | if (ns->buffered_io) |
| 33 | flush_workqueue(buffered_io_wq); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 34 | mempool_destroy(ns->bvec_pool); |
| 35 | ns->bvec_pool = NULL; |
| 36 | kmem_cache_destroy(ns->bvec_cache); |
| 37 | ns->bvec_cache = NULL; |
| 38 | fput(ns->file); |
| 39 | ns->file = NULL; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | int nvmet_file_ns_enable(struct nvmet_ns *ns) |
| 44 | { |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 45 | int flags = O_RDWR | O_LARGEFILE; |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 46 | int ret; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 47 | |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 48 | if (!ns->buffered_io) |
| 49 | flags |= O_DIRECT; |
| 50 | |
| 51 | ns->file = filp_open(ns->device_path, flags, 0); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 52 | if (IS_ERR(ns->file)) { |
Daniel Wagner | e207bbf | 2021-05-12 16:50:05 +0200 | [diff] [blame] | 53 | ret = PTR_ERR(ns->file); |
| 54 | pr_err("failed to open file %s: (%d)\n", |
| 55 | ns->device_path, ret); |
| 56 | ns->file = NULL; |
| 57 | return ret; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Anthony Iliopoulos | e8cd1ff | 2020-04-19 16:48:50 -0700 | [diff] [blame] | 60 | ret = nvmet_file_ns_revalidate(ns); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 61 | if (ret) |
| 62 | goto err; |
| 63 | |
Sagi Grimberg | 525ec49 | 2019-04-24 11:43:23 -0700 | [diff] [blame] | 64 | /* |
| 65 | * i_blkbits can be greater than the universally accepted upper bound, |
| 66 | * so make sure we export a sane namespace lba_shift. |
| 67 | */ |
| 68 | ns->blksize_shift = min_t(u8, |
| 69 | file_inode(ns->file)->i_blkbits, 12); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 70 | |
| 71 | ns->bvec_cache = kmem_cache_create("nvmet-bvec", |
| 72 | NVMET_MAX_MPOOL_BVEC * sizeof(struct bio_vec), |
| 73 | 0, SLAB_HWCACHE_ALIGN, NULL); |
Wei Yongjun | 1367bc8 | 2018-05-31 11:41:30 +0000 | [diff] [blame] | 74 | if (!ns->bvec_cache) { |
| 75 | ret = -ENOMEM; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 76 | goto err; |
Wei Yongjun | 1367bc8 | 2018-05-31 11:41:30 +0000 | [diff] [blame] | 77 | } |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 78 | |
| 79 | ns->bvec_pool = mempool_create(NVMET_MIN_MPOOL_OBJ, mempool_alloc_slab, |
| 80 | mempool_free_slab, ns->bvec_cache); |
| 81 | |
Wei Yongjun | 1367bc8 | 2018-05-31 11:41:30 +0000 | [diff] [blame] | 82 | if (!ns->bvec_pool) { |
| 83 | ret = -ENOMEM; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 84 | goto err; |
Wei Yongjun | 1367bc8 | 2018-05-31 11:41:30 +0000 | [diff] [blame] | 85 | } |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 86 | |
| 87 | return ret; |
| 88 | err: |
| 89 | ns->size = 0; |
| 90 | ns->blksize_shift = 0; |
| 91 | nvmet_file_ns_disable(ns); |
| 92 | return ret; |
| 93 | } |
| 94 | |
Ming Lei | 02db995 | 2019-03-27 17:07:22 +0800 | [diff] [blame] | 95 | static void nvmet_file_init_bvec(struct bio_vec *bv, struct scatterlist *sg) |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 96 | { |
Ming Lei | 02db995 | 2019-03-27 17:07:22 +0800 | [diff] [blame] | 97 | bv->bv_page = sg_page(sg); |
| 98 | bv->bv_offset = sg->offset; |
| 99 | bv->bv_len = sg->length; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static ssize_t nvmet_file_submit_bvec(struct nvmet_req *req, loff_t pos, |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 103 | unsigned long nr_segs, size_t count, int ki_flags) |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 104 | { |
| 105 | struct kiocb *iocb = &req->f.iocb; |
| 106 | ssize_t (*call_iter)(struct kiocb *iocb, struct iov_iter *iter); |
| 107 | struct iov_iter iter; |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 108 | int rw; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 109 | |
| 110 | if (req->cmd->rw.opcode == nvme_cmd_write) { |
| 111 | if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA)) |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 112 | ki_flags |= IOCB_DSYNC; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 113 | call_iter = req->ns->file->f_op->write_iter; |
| 114 | rw = WRITE; |
| 115 | } else { |
| 116 | call_iter = req->ns->file->f_op->read_iter; |
| 117 | rw = READ; |
| 118 | } |
| 119 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 120 | iov_iter_bvec(&iter, rw, req->f.bvec, nr_segs, count); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 121 | |
| 122 | iocb->ki_pos = pos; |
| 123 | iocb->ki_filp = req->ns->file; |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 124 | iocb->ki_flags = ki_flags | iocb_flags(req->ns->file); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 125 | |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 126 | return call_iter(iocb, &iter); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static void nvmet_file_io_done(struct kiocb *iocb, long ret, long ret2) |
| 130 | { |
| 131 | struct nvmet_req *req = container_of(iocb, struct nvmet_req, f.iocb); |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 132 | u16 status = NVME_SC_SUCCESS; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 133 | |
| 134 | if (req->f.bvec != req->inline_bvec) { |
| 135 | if (likely(req->f.mpool_alloc == false)) |
| 136 | kfree(req->f.bvec); |
| 137 | else |
| 138 | mempool_free(req->f.bvec, req->ns->bvec_pool); |
| 139 | } |
| 140 | |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 141 | if (unlikely(ret != req->transfer_len)) |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 142 | status = errno_to_nvme_status(req, ret); |
| 143 | nvmet_req_complete(req, status); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 144 | } |
| 145 | |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 146 | static bool nvmet_file_execute_io(struct nvmet_req *req, int ki_flags) |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 147 | { |
Ming Lei | 02db995 | 2019-03-27 17:07:22 +0800 | [diff] [blame] | 148 | ssize_t nr_bvec = req->sg_cnt; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 149 | unsigned long bv_cnt = 0; |
| 150 | bool is_sync = false; |
| 151 | size_t len = 0, total_len = 0; |
| 152 | ssize_t ret = 0; |
| 153 | loff_t pos; |
Ming Lei | 02db995 | 2019-03-27 17:07:22 +0800 | [diff] [blame] | 154 | int i; |
| 155 | struct scatterlist *sg; |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 156 | |
| 157 | if (req->f.mpool_alloc && nr_bvec > NVMET_MAX_MPOOL_BVEC) |
| 158 | is_sync = true; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 159 | |
Sagi Grimberg | 9c891c1 | 2018-07-11 16:13:04 +0300 | [diff] [blame] | 160 | pos = le64_to_cpu(req->cmd->rw.slba) << req->ns->blksize_shift; |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 161 | if (unlikely(pos + req->transfer_len > req->ns->size)) { |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 162 | nvmet_req_complete(req, errno_to_nvme_status(req, -ENOSPC)); |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 163 | return true; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 164 | } |
| 165 | |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 166 | memset(&req->f.iocb, 0, sizeof(struct kiocb)); |
Ming Lei | 02db995 | 2019-03-27 17:07:22 +0800 | [diff] [blame] | 167 | for_each_sg(req->sg, sg, req->sg_cnt, i) { |
| 168 | nvmet_file_init_bvec(&req->f.bvec[bv_cnt], sg); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 169 | len += req->f.bvec[bv_cnt].bv_len; |
| 170 | total_len += req->f.bvec[bv_cnt].bv_len; |
| 171 | bv_cnt++; |
| 172 | |
| 173 | WARN_ON_ONCE((nr_bvec - 1) < 0); |
| 174 | |
| 175 | if (unlikely(is_sync) && |
| 176 | (nr_bvec - 1 == 0 || bv_cnt == NVMET_MAX_MPOOL_BVEC)) { |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 177 | ret = nvmet_file_submit_bvec(req, pos, bv_cnt, len, 0); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 178 | if (ret < 0) |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 179 | goto complete; |
| 180 | |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 181 | pos += len; |
| 182 | bv_cnt = 0; |
| 183 | len = 0; |
| 184 | } |
| 185 | nr_bvec--; |
| 186 | } |
| 187 | |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 188 | if (WARN_ON_ONCE(total_len != req->transfer_len)) { |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 189 | ret = -EIO; |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 190 | goto complete; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 191 | } |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 192 | |
| 193 | if (unlikely(is_sync)) { |
| 194 | ret = total_len; |
| 195 | goto complete; |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * A NULL ki_complete ask for synchronous execution, which we want |
| 200 | * for the IOCB_NOWAIT case. |
| 201 | */ |
| 202 | if (!(ki_flags & IOCB_NOWAIT)) |
| 203 | req->f.iocb.ki_complete = nvmet_file_io_done; |
| 204 | |
| 205 | ret = nvmet_file_submit_bvec(req, pos, bv_cnt, total_len, ki_flags); |
| 206 | |
| 207 | switch (ret) { |
| 208 | case -EIOCBQUEUED: |
| 209 | return true; |
| 210 | case -EAGAIN: |
| 211 | if (WARN_ON_ONCE(!(ki_flags & IOCB_NOWAIT))) |
| 212 | goto complete; |
| 213 | return false; |
| 214 | case -EOPNOTSUPP: |
| 215 | /* |
| 216 | * For file systems returning error -EOPNOTSUPP, handle |
| 217 | * IOCB_NOWAIT error case separately and retry without |
| 218 | * IOCB_NOWAIT. |
| 219 | */ |
| 220 | if ((ki_flags & IOCB_NOWAIT)) |
| 221 | return false; |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | complete: |
| 226 | nvmet_file_io_done(&req->f.iocb, ret, 0); |
| 227 | return true; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 230 | static void nvmet_file_buffered_io_work(struct work_struct *w) |
| 231 | { |
| 232 | struct nvmet_req *req = container_of(w, struct nvmet_req, f.work); |
| 233 | |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 234 | nvmet_file_execute_io(req, 0); |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 235 | } |
| 236 | |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 237 | static void nvmet_file_submit_buffered_io(struct nvmet_req *req) |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 238 | { |
| 239 | INIT_WORK(&req->f.work, nvmet_file_buffered_io_work); |
| 240 | queue_work(buffered_io_wq, &req->f.work); |
| 241 | } |
| 242 | |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 243 | static void nvmet_file_execute_rw(struct nvmet_req *req) |
| 244 | { |
Ming Lei | 02db995 | 2019-03-27 17:07:22 +0800 | [diff] [blame] | 245 | ssize_t nr_bvec = req->sg_cnt; |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 246 | |
Israel Rukshin | 136cc1f | 2020-05-19 17:05:59 +0300 | [diff] [blame] | 247 | if (!nvmet_check_transfer_len(req, nvmet_rw_data_len(req))) |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 248 | return; |
| 249 | |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 250 | if (!req->sg_cnt || !nr_bvec) { |
| 251 | nvmet_req_complete(req, 0); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | if (nr_bvec > NVMET_MAX_INLINE_BIOVEC) |
| 256 | req->f.bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec), |
| 257 | GFP_KERNEL); |
| 258 | else |
| 259 | req->f.bvec = req->inline_bvec; |
| 260 | |
| 261 | if (unlikely(!req->f.bvec)) { |
| 262 | /* fallback under memory pressure */ |
| 263 | req->f.bvec = mempool_alloc(req->ns->bvec_pool, GFP_KERNEL); |
| 264 | req->f.mpool_alloc = true; |
| 265 | } else |
| 266 | req->f.mpool_alloc = false; |
| 267 | |
| 268 | if (req->ns->buffered_io) { |
| 269 | if (likely(!req->f.mpool_alloc) && |
Maurizio Lombardi | f2a58ff | 2021-11-22 11:08:27 +0100 | [diff] [blame] | 270 | (req->ns->file->f_mode & FMODE_NOWAIT) && |
| 271 | nvmet_file_execute_io(req, IOCB_NOWAIT)) |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 272 | return; |
| 273 | nvmet_file_submit_buffered_io(req); |
| 274 | } else |
| 275 | nvmet_file_execute_io(req, 0); |
| 276 | } |
| 277 | |
Chaitanya Kulkarni | dedf0be | 2018-08-07 23:01:07 -0700 | [diff] [blame] | 278 | u16 nvmet_file_flush(struct nvmet_req *req) |
| 279 | { |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 280 | return errno_to_nvme_status(req, vfs_fsync(req->ns->file, 1)); |
Chaitanya Kulkarni | dedf0be | 2018-08-07 23:01:07 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 283 | static void nvmet_file_flush_work(struct work_struct *w) |
| 284 | { |
| 285 | struct nvmet_req *req = container_of(w, struct nvmet_req, f.work); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 286 | |
Chaitanya Kulkarni | dedf0be | 2018-08-07 23:01:07 -0700 | [diff] [blame] | 287 | nvmet_req_complete(req, nvmet_file_flush(req)); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static void nvmet_file_execute_flush(struct nvmet_req *req) |
| 291 | { |
Israel Rukshin | 136cc1f | 2020-05-19 17:05:59 +0300 | [diff] [blame] | 292 | if (!nvmet_check_transfer_len(req, 0)) |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 293 | return; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 294 | INIT_WORK(&req->f.work, nvmet_file_flush_work); |
| 295 | schedule_work(&req->f.work); |
| 296 | } |
| 297 | |
| 298 | static void nvmet_file_execute_discard(struct nvmet_req *req) |
| 299 | { |
| 300 | int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; |
| 301 | struct nvme_dsm_range range; |
Sagi Grimberg | 1b72b71 | 2018-07-11 12:43:16 +0300 | [diff] [blame] | 302 | loff_t offset, len; |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 303 | u16 status = 0; |
| 304 | int ret; |
Sagi Grimberg | 1b72b71 | 2018-07-11 12:43:16 +0300 | [diff] [blame] | 305 | int i; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 306 | |
| 307 | for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) { |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 308 | status = nvmet_copy_from_sgl(req, i * sizeof(range), &range, |
Sagi Grimberg | 1b72b71 | 2018-07-11 12:43:16 +0300 | [diff] [blame] | 309 | sizeof(range)); |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 310 | if (status) |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 311 | break; |
Sagi Grimberg | 9c891c1 | 2018-07-11 16:13:04 +0300 | [diff] [blame] | 312 | |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 313 | offset = le64_to_cpu(range.slba) << req->ns->blksize_shift; |
Bart Van Assche | 8eacd1b | 2018-10-08 14:28:52 -0700 | [diff] [blame] | 314 | len = le32_to_cpu(range.nlb); |
| 315 | len <<= req->ns->blksize_shift; |
Sagi Grimberg | 9c891c1 | 2018-07-11 16:13:04 +0300 | [diff] [blame] | 316 | if (offset + len > req->ns->size) { |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 317 | req->error_slba = le64_to_cpu(range.slba); |
| 318 | status = errno_to_nvme_status(req, -ENOSPC); |
Sagi Grimberg | 9c891c1 | 2018-07-11 16:13:04 +0300 | [diff] [blame] | 319 | break; |
| 320 | } |
| 321 | |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 322 | ret = vfs_fallocate(req->ns->file, mode, offset, len); |
Christoph Hellwig | 005c674 | 2019-03-13 18:55:09 +0100 | [diff] [blame] | 323 | if (ret && ret != -EOPNOTSUPP) { |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 324 | req->error_slba = le64_to_cpu(range.slba); |
| 325 | status = errno_to_nvme_status(req, ret); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 326 | break; |
Sagi Grimberg | 1b72b71 | 2018-07-11 12:43:16 +0300 | [diff] [blame] | 327 | } |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 328 | } |
| 329 | |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 330 | nvmet_req_complete(req, status); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static void nvmet_file_dsm_work(struct work_struct *w) |
| 334 | { |
| 335 | struct nvmet_req *req = container_of(w, struct nvmet_req, f.work); |
| 336 | |
| 337 | switch (le32_to_cpu(req->cmd->dsm.attributes)) { |
| 338 | case NVME_DSMGMT_AD: |
| 339 | nvmet_file_execute_discard(req); |
| 340 | return; |
| 341 | case NVME_DSMGMT_IDR: |
| 342 | case NVME_DSMGMT_IDW: |
| 343 | default: |
| 344 | /* Not supported yet */ |
| 345 | nvmet_req_complete(req, 0); |
| 346 | return; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | static void nvmet_file_execute_dsm(struct nvmet_req *req) |
| 351 | { |
Sagi Grimberg | b716e68 | 2020-01-26 23:23:28 -0800 | [diff] [blame] | 352 | if (!nvmet_check_data_len_lte(req, nvmet_dsm_len(req))) |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 353 | return; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 354 | INIT_WORK(&req->f.work, nvmet_file_dsm_work); |
| 355 | schedule_work(&req->f.work); |
| 356 | } |
| 357 | |
| 358 | static void nvmet_file_write_zeroes_work(struct work_struct *w) |
| 359 | { |
| 360 | struct nvmet_req *req = container_of(w, struct nvmet_req, f.work); |
| 361 | struct nvme_write_zeroes_cmd *write_zeroes = &req->cmd->write_zeroes; |
| 362 | int mode = FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE; |
| 363 | loff_t offset; |
| 364 | loff_t len; |
| 365 | int ret; |
| 366 | |
| 367 | offset = le64_to_cpu(write_zeroes->slba) << req->ns->blksize_shift; |
| 368 | len = (((sector_t)le16_to_cpu(write_zeroes->length) + 1) << |
| 369 | req->ns->blksize_shift); |
| 370 | |
Sagi Grimberg | 9c891c1 | 2018-07-11 16:13:04 +0300 | [diff] [blame] | 371 | if (unlikely(offset + len > req->ns->size)) { |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 372 | nvmet_req_complete(req, errno_to_nvme_status(req, -ENOSPC)); |
Sagi Grimberg | 9c891c1 | 2018-07-11 16:13:04 +0300 | [diff] [blame] | 373 | return; |
| 374 | } |
| 375 | |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 376 | ret = vfs_fallocate(req->ns->file, mode, offset, len); |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 377 | nvmet_req_complete(req, ret < 0 ? errno_to_nvme_status(req, ret) : 0); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static void nvmet_file_execute_write_zeroes(struct nvmet_req *req) |
| 381 | { |
Israel Rukshin | 136cc1f | 2020-05-19 17:05:59 +0300 | [diff] [blame] | 382 | if (!nvmet_check_transfer_len(req, 0)) |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 383 | return; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 384 | INIT_WORK(&req->f.work, nvmet_file_write_zeroes_work); |
| 385 | schedule_work(&req->f.work); |
| 386 | } |
| 387 | |
| 388 | u16 nvmet_file_parse_io_cmd(struct nvmet_req *req) |
| 389 | { |
| 390 | struct nvme_command *cmd = req->cmd; |
| 391 | |
| 392 | switch (cmd->common.opcode) { |
| 393 | case nvme_cmd_read: |
| 394 | case nvme_cmd_write: |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 395 | req->execute = nvmet_file_execute_rw; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 396 | return 0; |
| 397 | case nvme_cmd_flush: |
| 398 | req->execute = nvmet_file_execute_flush; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 399 | return 0; |
| 400 | case nvme_cmd_dsm: |
| 401 | req->execute = nvmet_file_execute_dsm; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 402 | return 0; |
| 403 | case nvme_cmd_write_zeroes: |
| 404 | req->execute = nvmet_file_execute_write_zeroes; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 405 | return 0; |
| 406 | default: |
| 407 | pr_err("unhandled cmd for file ns %d on qid %d\n", |
| 408 | cmd->common.opcode, req->sq->qid); |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 409 | req->error_loc = offsetof(struct nvme_common_command, opcode); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 410 | return NVME_SC_INVALID_OPCODE | NVME_SC_DNR; |
| 411 | } |
| 412 | } |