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