nvme: introduce nvme_disk_is_ns_head helper
authorGuixin Liu <kanie@linux.alibaba.com>
Wed, 27 Dec 2023 09:31:06 +0000 (17:31 +0800)
committerKeith Busch <kbusch@kernel.org>
Fri, 5 Jan 2024 21:15:41 +0000 (13:15 -0800)
We currently rely on gendisk's file operations (fops) to distinguish
between a namespace head (ns_head) and a regular namespace. To enhance
code readability, introduce a helper function.
Additionally, we must ensure that the device is not an ns_head before
calling nvme_get_ns_from_dev(). To enforce this, add a WARN_ON check
within the nvme_get_ns_from_dev().

Signed-off-by: Guixin Liu <kanie@linux.alibaba.com>
Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Liu Song <liusong@linux.alibaba.com>
[include fix: https://lore.kernel.org/oe-kbuild-all/202401031943.0N72Tkji-lkp@intel.com/]
Signed-off-by: Keith Busch <kbusch@kernel.org>
drivers/nvme/host/nvme.h
drivers/nvme/host/pr.c
drivers/nvme/host/sysfs.c

index 297b80430f1b69d710f38f46fca7d9f61b935bf8..6092cc3618375c0e27127956fa0e6ee63d3d8a39 100644 (file)
@@ -920,6 +920,10 @@ extern struct device_attribute dev_attr_ana_grpid;
 extern struct device_attribute dev_attr_ana_state;
 extern struct device_attribute subsys_attr_iopolicy;
 
+static inline bool nvme_disk_is_ns_head(struct gendisk *disk)
+{
+       return disk->fops == &nvme_ns_head_ops;
+}
 #else
 #define multipath false
 static inline bool nvme_ctrl_use_ana(struct nvme_ctrl *ctrl)
@@ -997,6 +1001,10 @@ static inline void nvme_mpath_start_request(struct request *rq)
 static inline void nvme_mpath_end_request(struct request *rq)
 {
 }
+static inline bool nvme_disk_is_ns_head(struct gendisk *disk)
+{
+       return false;
+}
 #endif /* CONFIG_NVME_MULTIPATH */
 
 int nvme_revalidate_zones(struct nvme_ns *ns);
@@ -1025,7 +1033,10 @@ static inline int nvme_update_zone_info(struct nvme_ns *ns, unsigned lbaf)
 
 static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
 {
-       return dev_to_disk(dev)->private_data;
+       struct gendisk *disk = dev_to_disk(dev);
+
+       WARN_ON(nvme_disk_is_ns_head(disk));
+       return disk->private_data;
 }
 
 #ifdef CONFIG_NVME_HWMON
index 391b1465ebfd5e0067dfb698f8bd199a9fe3d148..fc3eed00f9ff1196189415ef1bccd0a6c1e02551 100644 (file)
@@ -98,7 +98,7 @@ static int nvme_send_pr_command(struct block_device *bdev,
                struct nvme_command *c, void *data, unsigned int data_len)
 {
        if (IS_ENABLED(CONFIG_NVME_MULTIPATH) &&
-           bdev->bd_disk->fops == &nvme_ns_head_ops)
+           nvme_disk_is_ns_head(bdev->bd_disk))
                return nvme_send_ns_head_pr_command(bdev, c, data, data_len);
 
        return nvme_send_ns_pr_command(bdev->bd_disk->private_data, c, data,
index ac24ad102380600cef13428eb0b3e31c0e32fecc..754e911110420f5f30074762c7787a88b183830a 100644 (file)
@@ -39,10 +39,9 @@ static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev)
 {
        struct gendisk *disk = dev_to_disk(dev);
 
-       if (disk->fops == &nvme_bdev_ops)
-               return nvme_get_ns_from_dev(dev)->head;
-       else
+       if (nvme_disk_is_ns_head(disk))
                return disk->private_data;
+       return nvme_get_ns_from_dev(dev)->head;
 }
 
 static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
@@ -233,7 +232,8 @@ static umode_t nvme_ns_attrs_are_visible(struct kobject *kobj,
        }
 #ifdef CONFIG_NVME_MULTIPATH
        if (a == &dev_attr_ana_grpid.attr || a == &dev_attr_ana_state.attr) {
-               if (dev_to_disk(dev)->fops != &nvme_bdev_ops) /* per-path attr */
+               /* per-path attr */
+               if (nvme_disk_is_ns_head(dev_to_disk(dev)))
                        return 0;
                if (!nvme_ctrl_use_ana(nvme_get_ns_from_dev(dev)->ctrl))
                        return 0;