Skip to content

Commit bafd590

Browse files
guixinliu1995keithbusch
authored andcommitted
nvme: introduce nvme_disk_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(). Signed-off-by: Guixin Liu <[email protected]> Reviewed-by: Sagi Grimberg <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Reviewed-by: Liu Song <[email protected]> [include fix: https://lore.kernel.org/oe-kbuild-all/[email protected]/] Signed-off-by: Keith Busch <[email protected]>
1 parent bd029a0 commit bafd590

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

drivers/nvme/host/nvme.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,10 @@ extern struct device_attribute dev_attr_ana_grpid;
920920
extern struct device_attribute dev_attr_ana_state;
921921
extern struct device_attribute subsys_attr_iopolicy;
922922

923+
static inline bool nvme_disk_is_ns_head(struct gendisk *disk)
924+
{
925+
return disk->fops == &nvme_ns_head_ops;
926+
}
923927
#else
924928
#define multipath false
925929
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)
9971001
static inline void nvme_mpath_end_request(struct request *rq)
9981002
{
9991003
}
1004+
static inline bool nvme_disk_is_ns_head(struct gendisk *disk)
1005+
{
1006+
return false;
1007+
}
10001008
#endif /* CONFIG_NVME_MULTIPATH */
10011009

10021010
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)
10251033

10261034
static inline struct nvme_ns *nvme_get_ns_from_dev(struct device *dev)
10271035
{
1028-
return dev_to_disk(dev)->private_data;
1036+
struct gendisk *disk = dev_to_disk(dev);
1037+
1038+
WARN_ON(nvme_disk_is_ns_head(disk));
1039+
return disk->private_data;
10291040
}
10301041

10311042
#ifdef CONFIG_NVME_HWMON

drivers/nvme/host/pr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static int nvme_send_pr_command(struct block_device *bdev,
9898
struct nvme_command *c, void *data, unsigned int data_len)
9999
{
100100
if (IS_ENABLED(CONFIG_NVME_MULTIPATH) &&
101-
bdev->bd_disk->fops == &nvme_ns_head_ops)
101+
nvme_disk_is_ns_head(bdev->bd_disk))
102102
return nvme_send_ns_head_pr_command(bdev, c, data, data_len);
103103

104104
return nvme_send_ns_pr_command(bdev->bd_disk->private_data, c, data,

drivers/nvme/host/sysfs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ static inline struct nvme_ns_head *dev_to_ns_head(struct device *dev)
3939
{
4040
struct gendisk *disk = dev_to_disk(dev);
4141

42-
if (disk->fops == &nvme_bdev_ops)
43-
return nvme_get_ns_from_dev(dev)->head;
44-
else
42+
if (nvme_disk_is_ns_head(disk))
4543
return disk->private_data;
44+
return nvme_get_ns_from_dev(dev)->head;
4645
}
4746

4847
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,
233232
}
234233
#ifdef CONFIG_NVME_MULTIPATH
235234
if (a == &dev_attr_ana_grpid.attr || a == &dev_attr_ana_state.attr) {
236-
if (dev_to_disk(dev)->fops != &nvme_bdev_ops) /* per-path attr */
235+
/* per-path attr */
236+
if (nvme_disk_is_ns_head(dev_to_disk(dev)))
237237
return 0;
238238
if (!nvme_ctrl_use_ana(nvme_get_ns_from_dev(dev)->ctrl))
239239
return 0;

0 commit comments

Comments
 (0)