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 | |
| 16 | void nvmet_file_ns_disable(struct nvmet_ns *ns) |
| 17 | { |
| 18 | if (ns->file) { |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 19 | if (ns->buffered_io) |
| 20 | flush_workqueue(buffered_io_wq); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 21 | mempool_destroy(ns->bvec_pool); |
| 22 | ns->bvec_pool = NULL; |
| 23 | kmem_cache_destroy(ns->bvec_cache); |
| 24 | ns->bvec_cache = NULL; |
| 25 | fput(ns->file); |
| 26 | ns->file = NULL; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | int nvmet_file_ns_enable(struct nvmet_ns *ns) |
| 31 | { |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 32 | int flags = O_RDWR | O_LARGEFILE; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 33 | struct kstat stat; |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 34 | int ret; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 35 | |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 36 | if (!ns->buffered_io) |
| 37 | flags |= O_DIRECT; |
| 38 | |
| 39 | ns->file = filp_open(ns->device_path, flags, 0); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 40 | if (IS_ERR(ns->file)) { |
| 41 | pr_err("failed to open file %s: (%ld)\n", |
Wei Yongjun | 81cf54e0 | 2018-05-31 01:55:58 +0000 | [diff] [blame] | 42 | ns->device_path, PTR_ERR(ns->file)); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 43 | return PTR_ERR(ns->file); |
| 44 | } |
| 45 | |
| 46 | ret = vfs_getattr(&ns->file->f_path, |
| 47 | &stat, STATX_SIZE, AT_STATX_FORCE_SYNC); |
| 48 | if (ret) |
| 49 | goto err; |
| 50 | |
| 51 | ns->size = stat.size; |
| 52 | ns->blksize_shift = file_inode(ns->file)->i_blkbits; |
| 53 | |
| 54 | ns->bvec_cache = kmem_cache_create("nvmet-bvec", |
| 55 | NVMET_MAX_MPOOL_BVEC * sizeof(struct bio_vec), |
| 56 | 0, SLAB_HWCACHE_ALIGN, NULL); |
Wei Yongjun | 1367bc8 | 2018-05-31 11:41:30 +0000 | [diff] [blame] | 57 | if (!ns->bvec_cache) { |
| 58 | ret = -ENOMEM; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 59 | goto err; |
Wei Yongjun | 1367bc8 | 2018-05-31 11:41:30 +0000 | [diff] [blame] | 60 | } |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 61 | |
| 62 | ns->bvec_pool = mempool_create(NVMET_MIN_MPOOL_OBJ, mempool_alloc_slab, |
| 63 | mempool_free_slab, ns->bvec_cache); |
| 64 | |
Wei Yongjun | 1367bc8 | 2018-05-31 11:41:30 +0000 | [diff] [blame] | 65 | if (!ns->bvec_pool) { |
| 66 | ret = -ENOMEM; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 67 | goto err; |
Wei Yongjun | 1367bc8 | 2018-05-31 11:41:30 +0000 | [diff] [blame] | 68 | } |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 69 | |
| 70 | return ret; |
| 71 | err: |
| 72 | ns->size = 0; |
| 73 | ns->blksize_shift = 0; |
| 74 | nvmet_file_ns_disable(ns); |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | static void nvmet_file_init_bvec(struct bio_vec *bv, struct sg_page_iter *iter) |
| 79 | { |
| 80 | bv->bv_page = sg_page_iter_page(iter); |
| 81 | bv->bv_offset = iter->sg->offset; |
| 82 | bv->bv_len = PAGE_SIZE - iter->sg->offset; |
| 83 | } |
| 84 | |
| 85 | static ssize_t nvmet_file_submit_bvec(struct nvmet_req *req, loff_t pos, |
| 86 | unsigned long nr_segs, size_t count) |
| 87 | { |
| 88 | struct kiocb *iocb = &req->f.iocb; |
| 89 | ssize_t (*call_iter)(struct kiocb *iocb, struct iov_iter *iter); |
| 90 | struct iov_iter iter; |
| 91 | int ki_flags = 0, rw; |
| 92 | ssize_t ret; |
| 93 | |
| 94 | if (req->cmd->rw.opcode == nvme_cmd_write) { |
| 95 | if (req->cmd->rw.control & cpu_to_le16(NVME_RW_FUA)) |
| 96 | ki_flags = IOCB_DSYNC; |
| 97 | call_iter = req->ns->file->f_op->write_iter; |
| 98 | rw = WRITE; |
| 99 | } else { |
| 100 | call_iter = req->ns->file->f_op->read_iter; |
| 101 | rw = READ; |
| 102 | } |
| 103 | |
| 104 | iov_iter_bvec(&iter, ITER_BVEC | rw, req->f.bvec, nr_segs, count); |
| 105 | |
| 106 | iocb->ki_pos = pos; |
| 107 | iocb->ki_filp = req->ns->file; |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 108 | iocb->ki_flags = ki_flags | iocb_flags(req->ns->file); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 109 | |
| 110 | ret = call_iter(iocb, &iter); |
| 111 | |
| 112 | if (ret != -EIOCBQUEUED && iocb->ki_complete) |
| 113 | iocb->ki_complete(iocb, ret, 0); |
| 114 | |
| 115 | return ret; |
| 116 | } |
| 117 | |
| 118 | static void nvmet_file_io_done(struct kiocb *iocb, long ret, long ret2) |
| 119 | { |
| 120 | struct nvmet_req *req = container_of(iocb, struct nvmet_req, f.iocb); |
| 121 | |
| 122 | if (req->f.bvec != req->inline_bvec) { |
| 123 | if (likely(req->f.mpool_alloc == false)) |
| 124 | kfree(req->f.bvec); |
| 125 | else |
| 126 | mempool_free(req->f.bvec, req->ns->bvec_pool); |
| 127 | } |
| 128 | |
| 129 | nvmet_req_complete(req, ret != req->data_len ? |
| 130 | NVME_SC_INTERNAL | NVME_SC_DNR : 0); |
| 131 | } |
| 132 | |
| 133 | static void nvmet_file_execute_rw(struct nvmet_req *req) |
| 134 | { |
| 135 | ssize_t nr_bvec = DIV_ROUND_UP(req->data_len, PAGE_SIZE); |
| 136 | struct sg_page_iter sg_pg_iter; |
| 137 | unsigned long bv_cnt = 0; |
| 138 | bool is_sync = false; |
| 139 | size_t len = 0, total_len = 0; |
| 140 | ssize_t ret = 0; |
| 141 | loff_t pos; |
| 142 | |
| 143 | if (!req->sg_cnt || !nr_bvec) { |
| 144 | nvmet_req_complete(req, 0); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | if (nr_bvec > NVMET_MAX_INLINE_BIOVEC) |
| 149 | req->f.bvec = kmalloc_array(nr_bvec, sizeof(struct bio_vec), |
| 150 | GFP_KERNEL); |
| 151 | else |
| 152 | req->f.bvec = req->inline_bvec; |
| 153 | |
| 154 | req->f.mpool_alloc = false; |
| 155 | if (unlikely(!req->f.bvec)) { |
| 156 | /* fallback under memory pressure */ |
| 157 | req->f.bvec = mempool_alloc(req->ns->bvec_pool, GFP_KERNEL); |
| 158 | req->f.mpool_alloc = true; |
| 159 | if (nr_bvec > NVMET_MAX_MPOOL_BVEC) |
| 160 | is_sync = true; |
| 161 | } |
| 162 | |
| 163 | pos = le64_to_cpu(req->cmd->rw.slba) << req->ns->blksize_shift; |
| 164 | |
| 165 | memset(&req->f.iocb, 0, sizeof(struct kiocb)); |
| 166 | for_each_sg_page(req->sg, &sg_pg_iter, req->sg_cnt, 0) { |
| 167 | nvmet_file_init_bvec(&req->f.bvec[bv_cnt], &sg_pg_iter); |
| 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)) { |
| 176 | ret = nvmet_file_submit_bvec(req, pos, bv_cnt, len); |
| 177 | if (ret < 0) |
| 178 | goto out; |
| 179 | pos += len; |
| 180 | bv_cnt = 0; |
| 181 | len = 0; |
| 182 | } |
| 183 | nr_bvec--; |
| 184 | } |
| 185 | |
| 186 | if (WARN_ON_ONCE(total_len != req->data_len)) |
| 187 | ret = -EIO; |
| 188 | out: |
| 189 | if (unlikely(is_sync || ret)) { |
| 190 | nvmet_file_io_done(&req->f.iocb, ret < 0 ? ret : total_len, 0); |
| 191 | return; |
| 192 | } |
| 193 | req->f.iocb.ki_complete = nvmet_file_io_done; |
| 194 | nvmet_file_submit_bvec(req, pos, bv_cnt, total_len); |
| 195 | } |
| 196 | |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 197 | static void nvmet_file_buffered_io_work(struct work_struct *w) |
| 198 | { |
| 199 | struct nvmet_req *req = container_of(w, struct nvmet_req, f.work); |
| 200 | |
| 201 | nvmet_file_execute_rw(req); |
| 202 | } |
| 203 | |
| 204 | static void nvmet_file_execute_rw_buffered_io(struct nvmet_req *req) |
| 205 | { |
| 206 | INIT_WORK(&req->f.work, nvmet_file_buffered_io_work); |
| 207 | queue_work(buffered_io_wq, &req->f.work); |
| 208 | } |
| 209 | |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 210 | static void nvmet_file_flush_work(struct work_struct *w) |
| 211 | { |
| 212 | struct nvmet_req *req = container_of(w, struct nvmet_req, f.work); |
| 213 | int ret; |
| 214 | |
| 215 | ret = vfs_fsync(req->ns->file, 1); |
| 216 | |
| 217 | nvmet_req_complete(req, ret < 0 ? NVME_SC_INTERNAL | NVME_SC_DNR : 0); |
| 218 | } |
| 219 | |
| 220 | static void nvmet_file_execute_flush(struct nvmet_req *req) |
| 221 | { |
| 222 | INIT_WORK(&req->f.work, nvmet_file_flush_work); |
| 223 | schedule_work(&req->f.work); |
| 224 | } |
| 225 | |
| 226 | static void nvmet_file_execute_discard(struct nvmet_req *req) |
| 227 | { |
| 228 | int mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE; |
| 229 | struct nvme_dsm_range range; |
Sagi Grimberg | 1b72b71 | 2018-07-11 12:43:16 +0300 | [diff] [blame^] | 230 | loff_t offset, len; |
| 231 | u16 ret; |
| 232 | int i; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 233 | |
| 234 | for (i = 0; i <= le32_to_cpu(req->cmd->dsm.nr); i++) { |
Sagi Grimberg | 1b72b71 | 2018-07-11 12:43:16 +0300 | [diff] [blame^] | 235 | ret = nvmet_copy_from_sgl(req, i * sizeof(range), &range, |
| 236 | sizeof(range)); |
| 237 | if (ret) |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 238 | break; |
| 239 | offset = le64_to_cpu(range.slba) << req->ns->blksize_shift; |
| 240 | len = le32_to_cpu(range.nlb) << req->ns->blksize_shift; |
Sagi Grimberg | 1b72b71 | 2018-07-11 12:43:16 +0300 | [diff] [blame^] | 241 | if (vfs_fallocate(req->ns->file, mode, offset, len)) { |
| 242 | ret = NVME_SC_INTERNAL | NVME_SC_DNR; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 243 | break; |
Sagi Grimberg | 1b72b71 | 2018-07-11 12:43:16 +0300 | [diff] [blame^] | 244 | } |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Sagi Grimberg | 1b72b71 | 2018-07-11 12:43:16 +0300 | [diff] [blame^] | 247 | nvmet_req_complete(req, ret); |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static void nvmet_file_dsm_work(struct work_struct *w) |
| 251 | { |
| 252 | struct nvmet_req *req = container_of(w, struct nvmet_req, f.work); |
| 253 | |
| 254 | switch (le32_to_cpu(req->cmd->dsm.attributes)) { |
| 255 | case NVME_DSMGMT_AD: |
| 256 | nvmet_file_execute_discard(req); |
| 257 | return; |
| 258 | case NVME_DSMGMT_IDR: |
| 259 | case NVME_DSMGMT_IDW: |
| 260 | default: |
| 261 | /* Not supported yet */ |
| 262 | nvmet_req_complete(req, 0); |
| 263 | return; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | static void nvmet_file_execute_dsm(struct nvmet_req *req) |
| 268 | { |
| 269 | INIT_WORK(&req->f.work, nvmet_file_dsm_work); |
| 270 | schedule_work(&req->f.work); |
| 271 | } |
| 272 | |
| 273 | static void nvmet_file_write_zeroes_work(struct work_struct *w) |
| 274 | { |
| 275 | struct nvmet_req *req = container_of(w, struct nvmet_req, f.work); |
| 276 | struct nvme_write_zeroes_cmd *write_zeroes = &req->cmd->write_zeroes; |
| 277 | int mode = FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE; |
| 278 | loff_t offset; |
| 279 | loff_t len; |
| 280 | int ret; |
| 281 | |
| 282 | offset = le64_to_cpu(write_zeroes->slba) << req->ns->blksize_shift; |
| 283 | len = (((sector_t)le16_to_cpu(write_zeroes->length) + 1) << |
| 284 | req->ns->blksize_shift); |
| 285 | |
| 286 | ret = vfs_fallocate(req->ns->file, mode, offset, len); |
| 287 | nvmet_req_complete(req, ret < 0 ? NVME_SC_INTERNAL | NVME_SC_DNR : 0); |
| 288 | } |
| 289 | |
| 290 | static void nvmet_file_execute_write_zeroes(struct nvmet_req *req) |
| 291 | { |
| 292 | INIT_WORK(&req->f.work, nvmet_file_write_zeroes_work); |
| 293 | schedule_work(&req->f.work); |
| 294 | } |
| 295 | |
| 296 | u16 nvmet_file_parse_io_cmd(struct nvmet_req *req) |
| 297 | { |
| 298 | struct nvme_command *cmd = req->cmd; |
| 299 | |
| 300 | switch (cmd->common.opcode) { |
| 301 | case nvme_cmd_read: |
| 302 | case nvme_cmd_write: |
Chaitanya Kulkarni | 55eb942e | 2018-06-20 00:01:41 -0400 | [diff] [blame] | 303 | if (req->ns->buffered_io) |
| 304 | req->execute = nvmet_file_execute_rw_buffered_io; |
| 305 | else |
| 306 | req->execute = nvmet_file_execute_rw; |
Chaitanya Kulkarni | d5eff33 | 2018-05-23 00:34:39 -0400 | [diff] [blame] | 307 | req->data_len = nvmet_rw_len(req); |
| 308 | return 0; |
| 309 | case nvme_cmd_flush: |
| 310 | req->execute = nvmet_file_execute_flush; |
| 311 | req->data_len = 0; |
| 312 | return 0; |
| 313 | case nvme_cmd_dsm: |
| 314 | req->execute = nvmet_file_execute_dsm; |
| 315 | req->data_len = (le32_to_cpu(cmd->dsm.nr) + 1) * |
| 316 | sizeof(struct nvme_dsm_range); |
| 317 | return 0; |
| 318 | case nvme_cmd_write_zeroes: |
| 319 | req->execute = nvmet_file_execute_write_zeroes; |
| 320 | req->data_len = 0; |
| 321 | return 0; |
| 322 | default: |
| 323 | pr_err("unhandled cmd for file ns %d on qid %d\n", |
| 324 | cmd->common.opcode, req->sq->qid); |
| 325 | return NVME_SC_INVALID_OPCODE | NVME_SC_DNR; |
| 326 | } |
| 327 | } |