nvme: introduce nvme_ctrl_get_by_path()
authorLogan Gunthorpe <logang@deltatee.com>
Fri, 24 Jul 2020 17:25:15 +0000 (11:25 -0600)
committerChristoph Hellwig <hch@lst.de>
Wed, 29 Jul 2020 05:45:21 +0000 (07:45 +0200)
nvme_ctrl_get_by_path() is analogous to blkdev_get_by_path() except it
gets a struct nvme_ctrl from the path to its char dev (/dev/nvme0).
It makes use of filp_open() to open the file and uses the private
data to obtain a pointer to the struct nvme_ctrl. If the fops of the
file do not match, -EINVAL is returned.

The purpose of this function is to support NVMe-OF target passthru
and is exported under the NVME_TARGET_PASSTHRU namespace.

Signed-off-by: Logan Gunthorpe <logang@deltatee.com>
Reviewed-by: Keith Busch <kbusch@kernel.org>
Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
Reviewed-by: Max Gurtovoy <maxg@mellanox.com>
Signed-off-by: Christoph Hellwig <hch@lst.de>
drivers/nvme/host/core.c
drivers/nvme/host/nvme.h

index 8296c1248f8784c9621faf0ec7232320e4449be9..c44316c5018f5a07b17ebf4e066cb9dbecd5caa8 100644 (file)
@@ -4590,6 +4590,29 @@ void nvme_sync_queues(struct nvme_ctrl *ctrl)
 }
 EXPORT_SYMBOL_GPL(nvme_sync_queues);
 
+struct nvme_ctrl *nvme_ctrl_get_by_path(const char *path)
+{
+       struct nvme_ctrl *ctrl;
+       struct file *f;
+
+       f = filp_open(path, O_RDWR, 0);
+       if (IS_ERR(f))
+               return ERR_CAST(f);
+
+       if (f->f_op != &nvme_dev_fops) {
+               ctrl = ERR_PTR(-EINVAL);
+               goto out_close;
+       }
+
+       ctrl = f->private_data;
+       nvme_get_ctrl(ctrl);
+
+out_close:
+       filp_close(f, NULL);
+       return ctrl;
+}
+EXPORT_SYMBOL_NS_GPL(nvme_ctrl_get_by_path, NVME_TARGET_PASSTHRU);
+
 /*
  * Check we didn't inadvertently grow the command structure sizes:
  */
index 4e3bc4b66c57cc8b27626476b39dec99be4838bd..5af59a9bac53f5d6aa9e7821db8a145e876c4a4d 100644 (file)
@@ -793,5 +793,6 @@ static inline void nvme_hwmon_init(struct nvme_ctrl *ctrl) { }
 u32 nvme_command_effects(struct nvme_ctrl *ctrl, struct nvme_ns *ns,
                         u8 opcode);
 void nvme_execute_passthru_rq(struct request *rq);
+struct nvme_ctrl *nvme_ctrl_get_by_path(const char *path);
 
 #endif /* _NVME_H */