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> |
| 11 | #include "nvmet.h" |
| 12 | |
| 13 | #define NVMET_MAX_MPOOL_BVEC 16 |
| 14 | #define NVMET_MIN_MPOOL_OBJ 16 |
| 15 | |
Anthony Iliopoulos | e8cd1ff | 2020-04-19 16:48:50 -0700 | [diff] [blame] | 16 | int 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 Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 28 | void nvmet_file_ns_disable(struct nvmet_ns *ns) |
| 29 | { |
| 30 | if (ns->file) { |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 31 | if (ns->buffered_io) |
| 32 | flush_workqueue(buffered_io_wq); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 33 | 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 | |
| 42 | int nvmet_file_ns_enable(struct nvmet_ns *ns) |
| 43 | { |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 44 | int flags = O_RDWR | O_LARGEFILE; |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 45 | int ret; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 46 | |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 47 | if (!ns->buffered_io) |
| 48 | flags |= O_DIRECT; |
| 49 | |
| 50 | ns->file = filp_open(ns->device_path, flags, 0); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 51 | if (IS_ERR(ns->file)) { |
Daniel Wagner | 85428be | 2021-05-12 16:50:05 +0200 | [diff] [blame^] | 52 | 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 Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 57 | } |
| 58 | |
Anthony Iliopoulos | e8cd1ff | 2020-04-19 16:48:50 -0700 | [diff] [blame] | 59 | ret = nvmet_file_ns_revalidate(ns); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 60 | if (ret) |
| 61 | goto err; |
| 62 | |
Sagi Grimberg | 525ec49 | 2019-04-24 11:43:23 -0700 | [diff] [blame] | 63 | /* |
| 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 Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 69 | |
| 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 Yongjun | 1367bc8 | 2018-05-31 11:41:30 +0000 | [diff] [blame] | 73 | if (!ns->bvec_cache) { |
| 74 | ret = -ENOMEM; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 75 | goto err; |
Wei Yongjun | 1367bc8 | 2018-05-31 11:41:30 +0000 | [diff] [blame] | 76 | } |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 77 | |
| 78 | ns->bvec_pool = mempool_create(NVMET_MIN_MPOOL_OBJ, mempool_alloc_slab, |
| 79 | mempool_free_slab, ns->bvec_cache); |
| 80 | |
Wei Yongjun | 1367bc8 | 2018-05-31 11:41:30 +0000 | [diff] [blame] | 81 | if (!ns->bvec_pool) { |
| 82 | ret = -ENOMEM; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 83 | goto err; |
Wei Yongjun | 1367bc8 | 2018-05-31 11:41:30 +0000 | [diff] [blame] | 84 | } |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 85 | |
| 86 | return ret; |
| 87 | err: |
| 88 | ns->size = 0; |
| 89 | ns->blksize_shift = 0; |
| 90 | nvmet_file_ns_disable(ns); |
| 91 | return ret; |
| 92 | } |
| 93 | |
Ming Lei | 02db995 | 2019-03-27 17:07:22 +0800 | [diff] [blame] | 94 | 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] | 95 | { |
Ming Lei | 02db995 | 2019-03-27 17:07:22 +0800 | [diff] [blame] | 96 | bv->bv_page = sg_page(sg); |
| 97 | bv->bv_offset = sg->offset; |
| 98 | bv->bv_len = sg->length; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | 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] | 102 | unsigned long nr_segs, size_t count, int ki_flags) |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 103 | { |
| 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 Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 107 | int rw; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 108 | |
| 109 | if (req->cmd->rw.opcode == nvme_cmd_write) { |
| 110 | if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA)) |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 111 | ki_flags |= IOCB_DSYNC; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 112 | 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 Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 119 | iov_iter_bvec(&iter, rw, req->f.bvec, nr_segs, count); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 120 | |
| 121 | iocb->ki_pos = pos; |
| 122 | iocb->ki_filp = req->ns->file; |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 123 | iocb->ki_flags = ki_flags | iocb_flags(req->ns->file); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 124 | |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 125 | return call_iter(iocb, &iter); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | static 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 Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 131 | u16 status = NVME_SC_SUCCESS; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 132 | |
| 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 Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 140 | if (unlikely(ret != req->transfer_len)) |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 141 | status = errno_to_nvme_status(req, ret); |
| 142 | nvmet_req_complete(req, status); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 145 | 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] | 146 | { |
Ming Lei | 02db995 | 2019-03-27 17:07:22 +0800 | [diff] [blame] | 147 | ssize_t nr_bvec = req->sg_cnt; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 148 | 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 Lei | 02db995 | 2019-03-27 17:07:22 +0800 | [diff] [blame] | 153 | int i; |
| 154 | struct scatterlist *sg; |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 155 | |
| 156 | if (req->f.mpool_alloc && nr_bvec > NVMET_MAX_MPOOL_BVEC) |
| 157 | is_sync = true; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 158 | |
Sagi Grimberg | 9c891c1 | 2018-07-11 16:13:04 +0300 | [diff] [blame] | 159 | pos = le64_to_cpu(req->cmd->rw.slba) << req->ns->blksize_shift; |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 160 | if (unlikely(pos + req->transfer_len > req->ns->size)) { |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 161 | nvmet_req_complete(req, errno_to_nvme_status(req, -ENOSPC)); |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 162 | return true; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 165 | memset(&req->f.iocb, 0, sizeof(struct kiocb)); |
Ming Lei | 02db995 | 2019-03-27 17:07:22 +0800 | [diff] [blame] | 166 | for_each_sg(req->sg, sg, req->sg_cnt, i) { |
| 167 | nvmet_file_init_bvec(&req->f.bvec[bv_cnt], sg); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 168 | 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 Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 176 | ret = nvmet_file_submit_bvec(req, pos, bv_cnt, len, 0); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 177 | if (ret < 0) |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 178 | goto complete; |
| 179 | |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 180 | pos += len; |
| 181 | bv_cnt = 0; |
| 182 | len = 0; |
| 183 | } |
| 184 | nr_bvec--; |
| 185 | } |
| 186 | |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 187 | if (WARN_ON_ONCE(total_len != req->transfer_len)) { |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 188 | ret = -EIO; |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 189 | goto complete; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 190 | } |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 191 | |
| 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 | |
| 224 | complete: |
| 225 | nvmet_file_io_done(&req->f.iocb, ret, 0); |
| 226 | return true; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 227 | } |
| 228 | |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 229 | static 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 Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 233 | nvmet_file_execute_io(req, 0); |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 234 | } |
| 235 | |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 236 | static void nvmet_file_submit_buffered_io(struct nvmet_req *req) |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 237 | { |
| 238 | INIT_WORK(&req->f.work, nvmet_file_buffered_io_work); |
| 239 | queue_work(buffered_io_wq, &req->f.work); |
| 240 | } |
| 241 | |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 242 | static void nvmet_file_execute_rw(struct nvmet_req *req) |
| 243 | { |
Ming Lei | 02db995 | 2019-03-27 17:07:22 +0800 | [diff] [blame] | 244 | ssize_t nr_bvec = req->sg_cnt; |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 245 | |
Israel Rukshin | 136cc1f | 2020-05-19 17:05:59 +0300 | [diff] [blame] | 246 | if (!nvmet_check_transfer_len(req, nvmet_rw_data_len(req))) |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 247 | return; |
| 248 | |
Chaitanya Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 249 | 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 Kulkarni | dedf0be | 2018-08-07 23:01:07 -0700 | [diff] [blame] | 276 | u16 nvmet_file_flush(struct nvmet_req *req) |
| 277 | { |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 278 | return errno_to_nvme_status(req, vfs_fsync(req->ns->file, 1)); |
Chaitanya Kulkarni | dedf0be | 2018-08-07 23:01:07 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 281 | static void nvmet_file_flush_work(struct work_struct *w) |
| 282 | { |
| 283 | struct nvmet_req *req = container_of(w, struct nvmet_req, f.work); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 284 | |
Chaitanya Kulkarni | dedf0be | 2018-08-07 23:01:07 -0700 | [diff] [blame] | 285 | nvmet_req_complete(req, nvmet_file_flush(req)); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | static void nvmet_file_execute_flush(struct nvmet_req *req) |
| 289 | { |
Israel Rukshin | 136cc1f | 2020-05-19 17:05:59 +0300 | [diff] [blame] | 290 | if (!nvmet_check_transfer_len(req, 0)) |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 291 | return; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 292 | INIT_WORK(&req->f.work, nvmet_file_flush_work); |
| 293 | schedule_work(&req->f.work); |
| 294 | } |
| 295 | |
| 296 | static 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 Grimberg | 1b72b71 | 2018-07-11 12:43:16 +0300 | [diff] [blame] | 300 | loff_t offset, len; |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 301 | u16 status = 0; |
| 302 | int ret; |
Sagi Grimberg | 1b72b71 | 2018-07-11 12:43:16 +0300 | [diff] [blame] | 303 | int i; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 304 | |
| 305 | for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) { |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 306 | status = nvmet_copy_from_sgl(req, i * sizeof(range), &range, |
Sagi Grimberg | 1b72b71 | 2018-07-11 12:43:16 +0300 | [diff] [blame] | 307 | sizeof(range)); |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 308 | if (status) |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 309 | break; |
Sagi Grimberg | 9c891c1 | 2018-07-11 16:13:04 +0300 | [diff] [blame] | 310 | |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 311 | offset = le64_to_cpu(range.slba) << req->ns->blksize_shift; |
Bart Van Assche | 8eacd1b | 2018-10-08 14:28:52 -0700 | [diff] [blame] | 312 | len = le32_to_cpu(range.nlb); |
| 313 | len <<= req->ns->blksize_shift; |
Sagi Grimberg | 9c891c1 | 2018-07-11 16:13:04 +0300 | [diff] [blame] | 314 | if (offset + len > req->ns->size) { |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 315 | req->error_slba = le64_to_cpu(range.slba); |
| 316 | status = errno_to_nvme_status(req, -ENOSPC); |
Sagi Grimberg | 9c891c1 | 2018-07-11 16:13:04 +0300 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 320 | ret = vfs_fallocate(req->ns->file, mode, offset, len); |
Christoph Hellwig | 005c674 | 2019-03-13 18:55:09 +0100 | [diff] [blame] | 321 | if (ret && ret != -EOPNOTSUPP) { |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 322 | req->error_slba = le64_to_cpu(range.slba); |
| 323 | status = errno_to_nvme_status(req, ret); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 324 | break; |
Sagi Grimberg | 1b72b71 | 2018-07-11 12:43:16 +0300 | [diff] [blame] | 325 | } |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 326 | } |
| 327 | |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 328 | nvmet_req_complete(req, status); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static 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 | |
| 348 | static void nvmet_file_execute_dsm(struct nvmet_req *req) |
| 349 | { |
Sagi Grimberg | b716e68 | 2020-01-26 23:23:28 -0800 | [diff] [blame] | 350 | if (!nvmet_check_data_len_lte(req, nvmet_dsm_len(req))) |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 351 | return; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 352 | INIT_WORK(&req->f.work, nvmet_file_dsm_work); |
| 353 | schedule_work(&req->f.work); |
| 354 | } |
| 355 | |
| 356 | static 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 Grimberg | 9c891c1 | 2018-07-11 16:13:04 +0300 | [diff] [blame] | 369 | if (unlikely(offset + len > req->ns->size)) { |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 370 | nvmet_req_complete(req, errno_to_nvme_status(req, -ENOSPC)); |
Sagi Grimberg | 9c891c1 | 2018-07-11 16:13:04 +0300 | [diff] [blame] | 371 | return; |
| 372 | } |
| 373 | |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 374 | ret = vfs_fallocate(req->ns->file, mode, offset, len); |
Chaitanya Kulkarni | c6aa354 | 2018-12-12 15:11:43 -0800 | [diff] [blame] | 375 | 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] | 376 | } |
| 377 | |
| 378 | static void nvmet_file_execute_write_zeroes(struct nvmet_req *req) |
| 379 | { |
Israel Rukshin | 136cc1f | 2020-05-19 17:05:59 +0300 | [diff] [blame] | 380 | if (!nvmet_check_transfer_len(req, 0)) |
Christoph Hellwig | e9061c3 | 2019-10-23 10:35:44 -0600 | [diff] [blame] | 381 | return; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 382 | INIT_WORK(&req->f.work, nvmet_file_write_zeroes_work); |
| 383 | schedule_work(&req->f.work); |
| 384 | } |
| 385 | |
| 386 | u16 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 Kulkarni | 50a909d | 2018-11-14 01:12:19 -0500 | [diff] [blame] | 393 | req->execute = nvmet_file_execute_rw; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 394 | return 0; |
| 395 | case nvme_cmd_flush: |
| 396 | req->execute = nvmet_file_execute_flush; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 397 | return 0; |
| 398 | case nvme_cmd_dsm: |
| 399 | req->execute = nvmet_file_execute_dsm; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 400 | return 0; |
| 401 | case nvme_cmd_write_zeroes: |
| 402 | req->execute = nvmet_file_execute_write_zeroes; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 403 | return 0; |
| 404 | default: |
Chaitanya Kulkarni | 1c2c761 | 2021-02-09 21:47:57 -0800 | [diff] [blame] | 405 | return nvmet_report_invalid_opcode(req); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 406 | } |
| 407 | } |