* [PATCH V3] nvme: introduce nvme_disk_is_ns_head helper
@ 2023-12-27 9:31 Guixin Liu
2023-12-28 1:48 ` Liu Song
` (3 more replies)
0 siblings, 4 replies; 6+ messages in thread
From: Guixin Liu @ 2023-12-27 9:31 UTC (permalink / raw)
To: kbusch, axboe, hch, sagi; +Cc: linux-nvme
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>
---
Changes from v2 to v3:
- simplify to disk->fops == &nvme_ns_head_ops.
Changes from v1 to v2:
- change helper name from nvme_gendisk_is_ns_head to
nvme_disk_is_ns_head.
- remove redundant comment.
drivers/nvme/host/nvme.h | 10 +++++++++-
drivers/nvme/host/pr.c | 2 +-
drivers/nvme/host/sysfs.c | 9 +++++----
3 files changed, 15 insertions(+), 6 deletions(-)
diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h
index e7411dac00f7..3dcbf48e9fdf 100644
--- a/drivers/nvme/host/nvme.h
+++ b/drivers/nvme/host/nvme.h
@@ -1029,9 +1029,17 @@ static inline int nvme_update_zone_info(struct nvme_ns *ns, unsigned lbaf)
}
#endif
+static inline bool nvme_disk_is_ns_head(struct gendisk *disk)
+{
+ return disk->fops == &nvme_ns_head_ops;
+}
+
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
diff --git a/drivers/nvme/host/pr.c b/drivers/nvme/host/pr.c
index 391b1465ebfd..fc3eed00f9ff 100644
--- a/drivers/nvme/host/pr.c
+++ b/drivers/nvme/host/pr.c
@@ -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,
diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
index c6b7fbd4d34d..925042ca728b 100644
--- a/drivers/nvme/host/sysfs.c
+++ b/drivers/nvme/host/sysfs.c
@@ -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_disk_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_disk_is_ns_head(dev_to_disk(dev)))
return 0;
if (!nvme_ctrl_use_ana(nvme_get_ns_from_dev(dev)->ctrl))
return 0;
--
2.43.0
^ permalink raw reply related [flat|nested] 6+ messages in thread* Re: [PATCH V3] nvme: introduce nvme_disk_is_ns_head helper
2023-12-27 9:31 [PATCH V3] nvme: introduce nvme_disk_is_ns_head helper Guixin Liu
@ 2023-12-28 1:48 ` Liu Song
2023-12-28 6:17 ` Christoph Hellwig
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Liu Song @ 2023-12-28 1:48 UTC (permalink / raw)
To: Guixin Liu, kbusch, axboe, hch, sagi; +Cc: linux-nvme
Using helper functions makes the code more readable.
Reviewed-by: Liu Song <liusong@linux.alibaba.com>
在 2023/12/27 17:31, Guixin Liu 写道:
> 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>
> ---
> Changes from v2 to v3:
> - simplify to disk->fops == &nvme_ns_head_ops.
>
> Changes from v1 to v2:
> - change helper name from nvme_gendisk_is_ns_head to
> nvme_disk_is_ns_head.
> - remove redundant comment.
>
> drivers/nvme/host/nvme.h | 10 +++++++++-
> drivers/nvme/host/pr.c | 2 +-
> drivers/nvme/host/sysfs.c | 9 +++++----
> 3 files changed, 15 insertions(+), 6 deletions(-)
>
> diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h
> index e7411dac00f7..3dcbf48e9fdf 100644
> --- a/drivers/nvme/host/nvme.h
> +++ b/drivers/nvme/host/nvme.h
> @@ -1029,9 +1029,17 @@ static inline int nvme_update_zone_info(struct nvme_ns *ns, unsigned lbaf)
> }
> #endif
>
> +static inline bool nvme_disk_is_ns_head(struct gendisk *disk)
> +{
> + return disk->fops == &nvme_ns_head_ops;
> +}
> +
> 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
> diff --git a/drivers/nvme/host/pr.c b/drivers/nvme/host/pr.c
> index 391b1465ebfd..fc3eed00f9ff 100644
> --- a/drivers/nvme/host/pr.c
> +++ b/drivers/nvme/host/pr.c
> @@ -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,
> diff --git a/drivers/nvme/host/sysfs.c b/drivers/nvme/host/sysfs.c
> index c6b7fbd4d34d..925042ca728b 100644
> --- a/drivers/nvme/host/sysfs.c
> +++ b/drivers/nvme/host/sysfs.c
> @@ -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_disk_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_disk_is_ns_head(dev_to_disk(dev)))
> return 0;
> if (!nvme_ctrl_use_ana(nvme_get_ns_from_dev(dev)->ctrl))
> return 0;
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH V3] nvme: introduce nvme_disk_is_ns_head helper
2023-12-27 9:31 [PATCH V3] nvme: introduce nvme_disk_is_ns_head helper Guixin Liu
2023-12-28 1:48 ` Liu Song
@ 2023-12-28 6:17 ` Christoph Hellwig
2024-01-01 9:49 ` Sagi Grimberg
2024-01-02 21:05 ` Keith Busch
3 siblings, 0 replies; 6+ messages in thread
From: Christoph Hellwig @ 2023-12-28 6:17 UTC (permalink / raw)
To: Guixin Liu; +Cc: kbusch, axboe, hch, sagi, linux-nvme
Looks good:
Reviewed-by: Christoph Hellwig <hch@lst.de>
> + if (nvme_disk_is_ns_head(disk))
> return disk->private_data;
> + else
> + return nvme_get_ns_from_dev(dev)->head;
> }
Keith, maybe you can drop the superflous (pre-existing) else here when
applying.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH V3] nvme: introduce nvme_disk_is_ns_head helper
2023-12-27 9:31 [PATCH V3] nvme: introduce nvme_disk_is_ns_head helper Guixin Liu
2023-12-28 1:48 ` Liu Song
2023-12-28 6:17 ` Christoph Hellwig
@ 2024-01-01 9:49 ` Sagi Grimberg
2024-01-02 21:05 ` Keith Busch
3 siblings, 0 replies; 6+ messages in thread
From: Sagi Grimberg @ 2024-01-01 9:49 UTC (permalink / raw)
To: Guixin Liu, kbusch, axboe, hch; +Cc: linux-nvme
Reviewed-by: Sagi Grimberg <sagi@grimberg.me>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH V3] nvme: introduce nvme_disk_is_ns_head helper
2023-12-27 9:31 [PATCH V3] nvme: introduce nvme_disk_is_ns_head helper Guixin Liu
` (2 preceding siblings ...)
2024-01-01 9:49 ` Sagi Grimberg
@ 2024-01-02 21:05 ` Keith Busch
2024-01-03 16:18 ` Keith Busch
3 siblings, 1 reply; 6+ messages in thread
From: Keith Busch @ 2024-01-02 21:05 UTC (permalink / raw)
To: Guixin Liu; +Cc: axboe, hch, sagi, linux-nvme
On Wed, Dec 27, 2023 at 05:31:06PM +0800, Guixin Liu wrote:
> 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().
Thanks, applied to nvme-6.8 with Christoph's suggestion folded in.
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH V3] nvme: introduce nvme_disk_is_ns_head helper
2024-01-02 21:05 ` Keith Busch
@ 2024-01-03 16:18 ` Keith Busch
0 siblings, 0 replies; 6+ messages in thread
From: Keith Busch @ 2024-01-03 16:18 UTC (permalink / raw)
To: Guixin Liu; +Cc: axboe, hch, sagi, linux-nvme
On Tue, Jan 02, 2024 at 02:05:13PM -0700, Keith Busch wrote:
> On Wed, Dec 27, 2023 at 05:31:06PM +0800, Guixin Liu wrote:
> > 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().
>
> Thanks, applied to nvme-6.8 with Christoph's suggestion folded in.
Patch updated to include fix for build breakage:
https://lists.infradead.org/pipermail/linux-nvme/2024-January/044120.html
Final form is currently here if you want to double check what I did:
https://git.infradead.org/nvme.git/commitdiff/620a6a938a716c6bd10129ec265731d5cb098a27
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2024-01-03 16:18 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-27 9:31 [PATCH V3] nvme: introduce nvme_disk_is_ns_head helper Guixin Liu
2023-12-28 1:48 ` Liu Song
2023-12-28 6:17 ` Christoph Hellwig
2024-01-01 9:49 ` Sagi Grimberg
2024-01-02 21:05 ` Keith Busch
2024-01-03 16:18 ` Keith Busch
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox