nvmet: add simple file backed ns support
Subsystems (2)
PATCH v6 1/1 nvmet: add simple file backed ns support
This patch adds simple file backed namespace support for NVMeOF target.
In the current implementation, NVMeOF supports block device backed namespaces on the target side. With this implementation regular file(s) can be used to initialize the namespace(s) via target side configfs interface.
For admin smart log command on the target side, we only use block device backed namespaces to compute target side smart log.
The new file io-cmd-file.c is responsible for handling the code for I/O commands when ns is file backed. Also, we introduce mempools based slow path using sync I/Os for file backed ns to ensure forward progress under reclaim.
With introduction of the new handle type (file) we also restructure the code for block device I/O commands where we rename the io-cmd.c -> io->cmd-bdev.c, use "nvmet_bdev_" prefix to
I/O command handler function and introduce
nvmet_[bdev | file]_ns_[enable | disable]() variants for block device and file in respective files.
---
Changes since V5:- (Codebase :- 4.17.0-rc5)
1. Rename io-cmd.c to io-cmd-bdev.c, and add "nvmet_bdev" prefix to I/O command handlers in the same file.
2. Move bdev and file ns enable|disable functions to io-cmd-bdev.c io-cmd-file.c
3. Change the kmem_cache identifier from "nvmet-fs" -> "nvmet-bvec".
4. Remove error print for nvmet_parse_io_cmd() for invalid handle type.
5. Trim the file header for io-cmd-file.c.
6. Remove typedefs for function pointers.
7. Restructure the file I/O code and aggregate slow and fast path into one function.
8. Rename NVMET_MAX_BVEC -> NVMET_MAX_MPOOL_BVEC.
9. Move nvmet_rw_len() to nvmet.h and keep it inline.
10. Define NVMET_MPOOL_MIN_OBJ directly.
11. Rename ns->cachep -> ns->bvec_cache and ns->mempool -> ns->bvec_pool.
12. Only issue mempool backed sync I/O when
nr_bvec > NVMET_MAX_MPOOL_BVEC.
Changes since V4:-
1. Adjust the code for 4.17.0-rc5.
2. Change name of nvmet_parse_io_cmd -> nvmet_parse_io_cmd_blkdev() in io-cmd.c.
3. Isolate the file backed ns I/O command handlers to separate file io-cmd-file.c.
4. Integrate Christoph's relative patch to use anonymous unions and file I/O command parse cleanup.
5. Introduce per namespace mempool to ensure forward progress under reclaim for each namespace.
6. Introduce centralize I/O command parse function to parse I/O commands based on the ns handle (device and file).
7. Introduce slow path for read/write commands, here we iterate through bvec allocated from mempool to complete file read/write handling.
8. Restructure nvmet_execute_rw_file() to handle new allocations and slow path.
Changes since V3:-
1. Fix the comment in the smart-log target code.
2. Change suffix from blk to blkdev.
3. Get rid of flags variable in nvmet_ns_enable_[blkdev|file]().
4. Skip the error print when path is not block device for nvmet_ns_enable_blkkdev().
5. Return PTR_ERR if blkdev open fails.
6. Remove NULL check after filp_open() and return PTR_ERR on filp_open() error.
7. Use AT_STATX_FORCE_SYNC and STATX_SIZE for vfs_getattr().
8. Separate line for each assignment.
9. Remove new lines before return.
10. Remove nvmet_ns_dev_enable() and call nvmet_ns_enable_[blkdev|file]() to enable ns handle from nvmet_ns_enable().
11. Use workqueue for discard and write zeroes.
12. Separate the block device and file parsing commands.
13. Move #include <linux/file.h> core.c (needed for fput) and
#include <linux/falloc.h> io-cmd.c from nvmet.h.
14. Unionize file and block device handles.
15. Only initialize iocb for file IO.
16. Add WARN_ON_ONCE() in nvmet_ns_dev_disable().
Note :- Replacing kmalloc_array() with bvec_alloc() is not done in this
version, as discussion is in progress.
Changes since V2:-
1. Adjust this patch for 4.17.0-rc4.
Changes since V1:-
1. Replace filp to file, filp_close to fput, call_[read|write]_iter() to [read|write]_iter().
2. Add code for the offloading flush operation for file-backed ns.
3. Aggregate the patches for O_DIRECT implementation.
4. Use inline bvec for smaller I/Os for file-backed ns.
5. Use file_inode() instead of open coding.
6. Get rid of bvec calculation macro and use DIV_ROUND_UP for the number of bvec calculation.
7. Avoid multiplication on the IO code patch.
8. Remove the req->bvec = NULL in IO completion callback.
9. Remove the sg mapping iterator and use simple for_each_sg_page() to iterate the pages.
---
drivers/nvme/target/Makefile | 4 +-
drivers/nvme/target/admin-cmd.c | 8 +
drivers/nvme/target/core.c | 52 ++--
drivers/nvme/target/{io-cmd.c => io-cmd-bdev.c} | 66 +++--
drivers/nvme/target/io-cmd-file.c | 318 ++++++++++++++++++++++++
drivers/nvme/target/nvmet.h | 28 ++-
6 files changed, 433 insertions(+), 43 deletions(-)
rename drivers/nvme/target/{io-cmd.c => io-cmd-bdev.c} (77%)
create mode 100644 drivers/nvme/target/io-cmd-file.cdrivers/nvme/target/Makefile +2 -2
@@ -6,8 +6,8 @@ obj-$(CONFIG_NVME_TARGET_RDMA) += nvmet-rdma.o obj-$(CONFIG_NVME_TARGET_FC) += nvmet-fc.o obj-$(CONFIG_NVME_TARGET_FCLOOP) += nvme-fcloop.o -nvmet-y += core.o configfs.o admin-cmd.o io-cmd.o fabrics-cmd.o \ - discovery.o +nvmet-y += core.o configfs.o admin-cmd.o io-cmd-bdev.o fabrics-cmd.o \ + discovery.o io-cmd-file.o nvme-loop-y += loop.o nvmet-rdma-y += rdma.o nvmet-fc-y += fc.o
drivers/nvme/target/admin-cmd.c +8 -0
@@ -45,6 +45,10 @@ static u16 nvmet_get_smart_log_nsid(struct nvmet_req *req,return NVME_SC_INVALID_NS ; } +/* we don't have the right data for file backed ns */ +if (!ns->bdev) + goto out; + host_reads =part_stat_read (ns->bdev->bd_part, ios[READ ]); data_units_read =part_stat_read (ns->bdev->bd_part, sectors[READ ]); host_writes =part_stat_read (ns->bdev->bd_part, ios[WRITE ]);
@@ -54,6 +58,7 @@ static u16 nvmet_get_smart_log_nsid(struct nvmet_req *req,put_unaligned_le64 (data_units_read, &slog->data_units_read[0 ]);put_unaligned_le64 (host_writes, &slog->host_writes[0 ]);put_unaligned_le64 (data_units_written, &slog->data_units_written[0 ]); +out:nvmet_put_namespace (ns);return NVME_SC_SUCCESS ;
@@ -71,6 +76,9 @@ static u16 nvmet_get_smart_log_all(struct nvmet_req *req,rcu_read_lock ();list_for_each_entry_rcu (ns, &ctrl->subsys->namespaces, dev_link) { +/* we don't have the right data for file backed ns */ +if (!ns->bdev) +continue ; host_reads +=part_stat_read (ns->bdev->bd_part, ios[READ ]); data_units_read +=part_stat_read (ns->bdev->bd_part, sectors[READ ]);
drivers/nvme/target/core.c +35 -17
87 lines not on this page · raw message
drivers/nvme/target/io-cmd-bdev.c renamed from drivers/nvme/target/io-cmd.c +44 -22
145 lines not on this page · raw message
drivers/nvme/target/io-cmd-file.c new file +318 -0
318 lines not on this page · raw message
drivers/nvme/target/nvmet.h +26 -2
@@ -43,6 +43,7 @@ struct nvmet_ns {struct list_head dev_link;struct percpu_ref ref;struct block_device *bdev; +struct file *file;u32 nsid;u32 blksize_shift;loff_t size;
@@ -57,6 +58,8 @@ struct nvmet_ns {struct config_group group;struct completion disable_done; +mempool_t *bvec_pool; +struct kmem_cache *bvec_cache; };static inline struct nvmet_ns *to_nvmet_ns (struct config_item *item)
@@ -222,8 +225,18 @@ struct nvmet_req {struct nvmet_cq *cq;struct nvmet_ns *ns;struct scatterlist *sg; -struct bio inline_bio;struct bio_vec inline_bvec[NVMET_MAX_INLINE_BIOVEC ]; +union { +struct { +struct bio inline_bio; + } b; +struct { +bool mpool_alloc; +struct kiocb iocb; +struct bio_vec *bvec; +struct work_struct work; + } f; + };int sg_cnt;/* data length as parsed from the command: */ size_t data_len;
@@ -263,7 +276,8 @@ struct nvmet_async_event { };u16 nvmet_parse_connect_cmd (struct nvmet_req *req); -u16 nvmet_parse_io_cmd (struct nvmet_req *req); +u16 nvmet_bdev_parse_io_cmd (struct nvmet_req *req); +u16 nvmet_file_parse_io_cmd (struct nvmet_req *req);u16 nvmet_parse_admin_cmd (struct nvmet_req *req);u16 nvmet_parse_discovery_cmd (struct nvmet_req *req);u16 nvmet_parse_fabrics_cmd (struct nvmet_req *req);
@@ -338,4 +352,14 @@ extern struct rw_semaphore nvmet_config_sem;bool nvmet_host_allowed (struct nvmet_req *req,struct nvmet_subsys *subsys,const char *hostnqn); +int nvmet_bdev_ns_enable (struct nvmet_ns *ns); +int nvmet_file_ns_enable (struct nvmet_ns *ns); +void nvmet_bdev_ns_disable (struct nvmet_ns *ns); +void nvmet_file_ns_disable (struct nvmet_ns *ns); + +static inline u32 nvmet_rw_len (struct nvmet_req *req) +{ +return ((u32 )le16_to_cpu (req->cmd->rw.length) +1 ) << + req->ns->blksize_shift; +}#endif /* _NVMET_H */
Signature · 1 line
2.9.5
Christoph Hellwig
maintainerPATCH v6 1/1 nvmet: add simple file backed ns support
Hi Chaitanya,
this looks great!
Just a few cosmetic fixups for a minor resend, and one thing that I think is a minor bug at the end:
Review
Quoted 3 lines
(quoted diff) +1
-2-nvmet-y += core.o configfs.o admin-cmd.o io-cmd.o fabrics-cmd.o \ - discovery.o +nvmet-y += core.o configfs.o admin-cmd.o io-cmd-bdev.o fabrics-cmd.o \
Please keep the length of the line under 80 chars for the makefile as well.
Review
Quoted 3 lines
(quoted diff) +3
-0+ /* fallback under memory pressure */ + + req->f.mpool_alloc = false;
I'd remove the empty line above to keep the comment closer to the code described by it.
Review
Quoted 2 lines
(quoted diff) +2
-0+ if (is_sync && (nr_bvec - 1 == 0 || + bv_cnt == NVMET_MAX_MPOOL_BVEC)) {
I usually try to align line breaks to boundaries of the logical operations to make the statement easier to read, e.g.:
if (is_sync &&
((nr_bvec - 1 == 0 || bv_cnt == NVMET_MAX_MPOOL_BVEC)) {Review
Quoted 8 lines
(quoted diff) +8
-0+ if (likely(!is_sync)) { + req->f.iocb.ki_complete = nvmet_file_io_done; + nvmet_file_submit_bvec(req, pos, bv_cnt, map_len); + } else if (req->f.mpool_alloc) { + mempool_free(req->f.bvec, req->ns->bvec_pool); + nvmet_req_complete(req, + ret < 0 ? NVME_SC_INTERNAL | NVME_SC_DNR : 0); + }
Don't we need to do the completion and freeing here also for the non-mempool case? What do you think about this patch that just uses the completion handler for the sync case as well and cleans up this function a bit:
drivers/nvme/target/io-cmd-file.c +17 -35
@@ -66,14 +66,11 @@ int nvmet_file_ns_enable(struct nvmet_ns *ns)return ret; } -static size_t nvmet_file_init_bvec (struct bio_vec *bv, -struct sg_page_iter *iter) +static void nvmet_file_init_bvec (struct bio_vec *bv,struct sg_page_iter *iter) { bv->bv_page =sg_page_iter_page (iter); bv->bv_offset = iter->sg->offset; bv->bv_len =PAGE_SIZE - iter->sg->offset; - -return bv->bv_len; } static ssize_t nvmet_file_submit_bvec(struct nvmet_req *req, loff_t pos,
@@ -130,9 +127,8 @@ static void nvmet_file_execute_rw(struct nvmet_req *req)struct sg_page_iter sg_pg_iter;unsigned long bv_cnt =0 ;bool is_sync = false; -size_t map_len =0 ; -size_t len =0 ; -ssize_t ret; +size_t len =0 , total_len =0 ; +ssize_t ret =0 ;loff_t pos;if (!req->sg_cnt || !nr_bvec) {
@@ -147,7 +143,6 @@ static void nvmet_file_execute_rw(struct nvmet_req *req) req->f.bvec = req->inline_bvec;/* fallback under memory pressure */ - req->f.mpool_alloc = false;if (unlikely (!req->f.bvec)) { req->f.bvec =mempool_alloc (req->ns->bvec_pool,GFP_KERNEL );
@@ -160,19 +155,18 @@ static void nvmet_file_execute_rw(struct nvmet_req *req)memset (&req->f.iocb,0 ,sizeof (struct kiocb ));for_each_sg_page (req->sg, &sg_pg_iter, req->sg_cnt,0 ) { - map_len +=nvmet_file_init_bvec (&req->f.bvec[bv_cnt], - &sg_pg_iter); +nvmet_file_init_bvec (&req->f.bvec[bv_cnt], &sg_pg_iter); len += req->f.bvec[bv_cnt].bv_len; + total_len += req->f.bvec[bv_cnt].bv_len; bv_cnt++; -WARN_ON ((nr_bvec -1 ) <0 ); +WARN_ON_ONCE (nr_bvec -1 <0 ); -if (is_sync && (nr_bvec -1 ==0 || - bv_cnt ==NVMET_MAX_MPOOL_BVEC )) { +if (unlikely (is_sync) && + (nr_bvec -1 ==0 || bv_cnt ==NVMET_MAX_MPOOL_BVEC )) { ret =nvmet_file_submit_bvec (req, pos, bv_cnt, len);if (ret <0 ) - goto err; - + goto out; pos += len; bv_cnt =0 ; len =0 ;
@@ -180,27 +174,15 @@ static void nvmet_file_execute_rw(struct nvmet_req *req) nr_bvec--; } -if (map_len != req->data_len) - goto err; - -if (likely (!is_sync)) { - req->f.iocb.ki_complete = nvmet_file_io_done; -nvmet_file_submit_bvec (req, pos, bv_cnt, map_len); - }else if (req->f.mpool_alloc) { -mempool_free (req->f.bvec, req->ns->bvec_pool); -nvmet_req_complete (req, - ret <0 ?NVME_SC_INTERNAL |NVME_SC_DNR :0 ); - } -return ; -err: -if (req->f.bvec != req->inline_bvec) { -if (req->f.mpool_alloc == false) -kfree (req->f.bvec); -else -mempool_free (req->f.bvec, req->ns->bvec_pool); +if (WARN_ON_ONCE (total_len != req->data_len)) + ret = -EIO ; +out: +if (unlikely (is_sync || ret)) { +nvmet_file_io_done (&req->f.iocb, ret,0 ); +return ; } - -nvmet_req_complete (req,NVME_SC_INTERNAL |NVME_SC_DNR ); + req->f.iocb.ki_complete = nvmet_file_io_done; +nvmet_file_submit_bvec (req, pos, bv_cnt, total_len); }static void nvmet_file_flush_work (struct work_struct *w)