Skip to contentSkip to conversation

nvme: introduce nvme_gendisk_is_ns_head helper

PATCH Superseded Guixin Liu 2 participants 2 messages Dec 27, 2023 NVMe

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Guixin Liu

author
#0 lore raw

PATCH 1/1 nvme: introduce nvme_gendisk_is_ns_head helper

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().

---
 drivers/nvme/host/nvme.h  | 13 ++++++++++++-
 drivers/nvme/host/pr.c    |  2 +-
 drivers/nvme/host/sysfs.c |  9 +++++----
 3 files changed, 18 insertions(+), 6 deletions(-)
drivers/nvme/host/nvme.h +12 -1
index e7411dac00f7..9b8ecac32cc0 100644
@@ -1029,9 +1029,20 @@ static inline int nvme_update_zone_info(struct nvme_ns *ns, unsigned lbaf)
 }
 #endif
 
+/* Use gendisk's fops to distinguish ns_head from ns */
+static inline bool nvme_gendisk_is_ns_head(struct gendisk *disk)
+{
+	if (disk->fops == &nvme_ns_head_ops)
+		return true;
+	return false;
+}
+
 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_gendisk_is_ns_head(disk));
+	return disk->private_data;
 }
 
 #ifdef CONFIG_NVME_HWMON
drivers/nvme/host/pr.c +1 -1
index 391b1465ebfd..744d4bf959e3 100644
@@ -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_gendisk_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,
drivers/nvme/host/sysfs.c +5 -4
index c6b7fbd4d34d..6d2ff19d54dc 100644
@@ -39,10 +39,10 @@ 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_gendisk_is_ns_head(disk))
 		return disk->private_data;
+	else
+		return nvme_get_ns_from_dev(dev)->head;
 }
 
 static ssize_t wwid_show(struct device *dev, struct device_attribute *attr,
@@ -148,7 +148,8 @@ static umode_t nvme_ns_id_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_gendisk_is_ns_head(dev_to_disk(dev)))
 			return 0;
 		if (!nvme_ctrl_use_ana(nvme_get_ns_from_dev(dev)->ctrl))
 			return 0;
Signature · 1 line
2.43.0
#1 lore raw
Quoted 1 line

+/* Use gendisk's fops to distinguish ns_head from ns */

That's pretty obvious from the code below..

Quoted 1 line

+static inline bool nvme_gendisk_is_ns_head(struct gendisk *disk)

Except for the weird holdout del_gendisk we only use disk in names, not gendisk.

drivers/nvme/host/nvme.h static inline int nvme_update_zone_info(struct nvme_ns *ns, unsigned lbaf) · line 1037

Quoted 4 lines
drivers/nvme/host/nvme.h +4 -0
+{
+	if (disk->fops == &nvme_ns_head_ops)
+		return true;
+	return false;
	return disk->fops == &nvme_ns_head_ops;