Skip to contentSkip to conversation

nvmet: add revalidate ns sysfs attribute to handle device resize

PATCH Superseded Mikhail Malygin 5 participants 19 messages Sep 26, 2019 – Oct 8, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmet: add revalidate ns sysfs attribute to handle device resize

Currently device size is cached in ns->size field on namespace enable, so any device size change after that can't bee seen by initiator. This patch adds revalidate namespace attribute. Once it is written, target refreshes ns->size property and calls nvmet_ns_changed so initator may perform namespace rescan

---
 drivers/nvme/target/configfs.c    | 14 +++++++++++
 drivers/nvme/target/core.c        | 23 +++++++++++++++++
 drivers/nvme/target/io-cmd-bdev.c | 16 ++++++++++--
 drivers/nvme/target/io-cmd-file.c | 42 ++++++++++++++++++++++---------
 drivers/nvme/target/nvmet.h       |  3 +++
 5 files changed, 84 insertions(+), 14 deletions(-)
drivers/nvme/target/configfs.c +14 -0
index 98613a45bd3b..06566ad1d197 100644
@@ -545,6 +545,19 @@ static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
 
 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
 
+static ssize_t nvmet_ns_revalidate_store(struct config_item *item,
+		const char *page, size_t count)
+{
+	struct nvmet_ns *ns = to_nvmet_ns(item);
+	int ret = 0;
+
+	ret = nvmet_ns_revalidate(ns);
+
+	return ret ? ret : count;
+}
+
+CONFIGFS_ATTR_WO(nvmet_ns_, revalidate);
+
 static struct configfs_attribute *nvmet_ns_attrs[] = {
 	&nvmet_ns_attr_device_path,
 	&nvmet_ns_attr_device_nguid,
@@ -552,6 +565,7 @@ static struct configfs_attribute *nvmet_ns_attrs[] = {
 	&nvmet_ns_attr_ana_grpid,
 	&nvmet_ns_attr_enable,
 	&nvmet_ns_attr_buffered_io,
+	&nvmet_ns_attr_revalidate,
 #ifdef CONFIG_PCI_P2PDMA
 	&nvmet_ns_attr_p2pmem,
 #endif
drivers/nvme/target/core.c +23 -0
index 3a67e244e568..1d90803e3c9a 100644
@@ -620,6 +620,29 @@ void nvmet_ns_disable(struct nvmet_ns *ns)
 	mutex_unlock(&subsys->lock);
 }
 
+int nvmet_ns_revalidate(struct nvmet_ns *ns)
+{
+	struct nvmet_subsys *subsys = ns->subsys;
+	int ret = 0;
+
+	mutex_lock(&subsys->lock);
+	if(!ns->enabled){
+		goto out_unlock;
+	}
+
+	if(ns->bdev){
+		ret = nvmet_bdev_ns_revalidate(ns);
+	}else if(ns->file){
+		ret = nvmet_file_ns_revalidate(ns);
+	}
+
+	nvmet_ns_changed(subsys, ns->nsid);
+
+out_unlock:
+	mutex_unlock(&subsys->lock);
+	return ret;
+}
+
 void nvmet_ns_free(struct nvmet_ns *ns)
 {
 	nvmet_ns_disable(ns);
drivers/nvme/target/io-cmd-bdev.c +14 -2
index de0bff70ebb6..ea55bbe93813 100644
@@ -47,6 +47,12 @@ void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id)
 	id->nows = to0based(ql->io_opt / ql->logical_block_size);
 }
 
+static void nvmet_bdev_ns_read_size(struct nvmet_ns *ns)
+{
+	ns->size = i_size_read(ns->bdev->bd_inode);
+	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
+}
+
 int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
 {
 	int ret;
@@ -62,8 +68,8 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
 		ns->bdev = NULL;
 		return ret;
 	}
-	ns->size = i_size_read(ns->bdev->bd_inode);
-	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
+
+	nvmet_bdev_ns_read_size(ns);
 	return 0;
 }
 
@@ -75,6 +81,12 @@ void nvmet_bdev_ns_disable(struct nvmet_ns *ns)
 	}
 }
 
+int nvmet_bdev_ns_revalidate(struct nvmet_ns *ns)
+{
+	nvmet_bdev_ns_read_size(ns);
+	return 0;
+}
+
 static u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts)
 {
 	u16 status = NVME_SC_SUCCESS;
drivers/nvme/target/io-cmd-file.c +30 -12

64 lines not on this page · raw message

drivers/nvme/target/nvmet.h +3 -0
index c51f8dd01dc4..a9453e63e5dd 100644
@@ -408,6 +408,7 @@ struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
 void nvmet_put_namespace(struct nvmet_ns *ns);
 int nvmet_ns_enable(struct nvmet_ns *ns);
 void nvmet_ns_disable(struct nvmet_ns *ns);
+int nvmet_ns_revalidate(struct nvmet_ns *ns);
 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
 void nvmet_ns_free(struct nvmet_ns *ns);
 
@@ -485,6 +486,8 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
 int nvmet_file_ns_enable(struct nvmet_ns *ns);
 void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
 void nvmet_file_ns_disable(struct nvmet_ns *ns);
+int nvmet_bdev_ns_revalidate(struct nvmet_ns *ns);
+int nvmet_file_ns_revalidate(struct nvmet_ns *ns);
 u16 nvmet_bdev_flush(struct nvmet_req *req);
 u16 nvmet_file_flush(struct nvmet_req *req);
 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
Signature · 4 lines
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
maintainer
#1 lore raw
Quoted 8 lines
drivers/nvme/target/core.c +7 -0
+int nvmet_ns_revalidate(struct nvmet_ns *ns)
+{
+	struct nvmet_subsys *subsys = ns->subsys;
+	int ret = 0;
+
+	mutex_lock(&subsys->lock);
+	if(!ns->enabled){

drivers/nvme/target/core.c void nvmet_ns_disable(struct nvmet_ns *ns) · line 639

Quoted 10 lines
drivers/nvme/target/core.c +10 -0
+		goto out_unlock;
+	}
+
+	if(ns->bdev){
+		ret = nvmet_bdev_ns_revalidate(ns);
+	}else if(ns->file){
+		ret = nvmet_file_ns_revalidate(ns);
+	}
+
+	nvmet_ns_changed(subsys, ns->nsid);

Shouldn't that happen only of the size changed?

So what is the model here? have some udev rule to
trigger this?

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#2 lore raw
Quoted 11 lines · Sagi Grimberg
Quoted 11 lines
drivers/nvme/target/core.c +11 -0
+int nvmet_ns_revalidate(struct nvmet_ns *ns)
+{
+	struct nvmet_subsys *subsys = ns->subsys;
+	int ret = 0;
+
+	mutex_lock(&subsys->lock);
+	if(!ns->enabled){
+		goto out_unlock;
+	}
+
+	if(ns->bdev){
Quoted 8 lines

drivers/nvme/target/core.c void nvmet_ns_disable(struct nvmet_ns *ns) · line 639

Quoted 6 lines
drivers/nvme/target/core.c +6 -0
+		ret = nvmet_bdev_ns_revalidate(ns);
+	}else if(ns->file){
+		ret = nvmet_file_ns_revalidate(ns);
+	}
+
+	nvmet_ns_changed(subsys, ns->nsid);

Shouldn't that happen only of the size changed?

s/of/if/ ?

I guess we could do a sanity check first.

Quoted 2 lines

So what is the model here? have some udev rule to
trigger this?

Well, even if there was a udev rule we can't directly rely on it. Mikhail actually talked with me about this at SDC, and I suggested to just implement a simple file to revalidate from userspace, as we have no good way to figure out if it changed in the kernel code.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

maintainer
#3 lore raw
Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
drivers/nvme/target/core.c +11 -0
+int nvmet_ns_revalidate(struct nvmet_ns *ns)
+{
+	struct nvmet_subsys *subsys = ns->subsys;
+	int ret = 0;
+
+	mutex_lock(&subsys->lock);
+	if(!ns->enabled){
+		goto out_unlock;
+	}
+
+	if(ns->bdev){
Quoted 8 lines

drivers/nvme/target/core.c void nvmet_ns_disable(struct nvmet_ns *ns) · line 639

Quoted 6 lines
drivers/nvme/target/core.c +6 -0
+		ret = nvmet_bdev_ns_revalidate(ns);
+	}else if(ns->file){
+		ret = nvmet_file_ns_revalidate(ns);
+	}
+
+	nvmet_ns_changed(subsys, ns->nsid);

Shouldn't that happen only of the size changed?

Quoted 1 line

s/of/if/ ?

Yes..

Quoted 6 lines

I guess we could do a sanity check first.

Quoted 2 lines

So what is the model here? have some udev rule to
trigger this?

Well, even if there was a udev rule we can't directly rely on it.

What do you mean we can't rely on it?

I do understand that we'd have no triggers for file-backed namespaces.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#4 lore raw
Quoted 5 lines · Sagi Grimberg
Quoted 1 line

Well, even if there was a udev rule we can't directly rely on it.

What do you mean we can't rely on it?

I do understand that we'd have no triggers for file-backed namespaces.

Do we have any generic way supported by all block device drivers to notify resizes? For files we could at least use dnotify/inotify/fsnotify/ etc in userspace.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#5 lore raw
In reply to Sagi Grimberg

Just posted an updated version with size check before triggering ns_changed.

Regarding the model, I’m managing both block devices and targets from userspace daemon, so just need any reliable way to update target ns size once I resized block device.

Quoted 23 lines · Sagi Grimberg
Quoted 9 lines
drivers/nvme/target/core.c +7 -0
+int nvmet_ns_revalidate(struct nvmet_ns *ns)
+{
+	struct nvmet_subsys *subsys = ns->subsys;
+	int ret = 0;
+
+	mutex_lock(&subsys->lock);
+	if(!ns->enabled){

drivers/nvme/target/core.c void nvmet_ns_disable(struct nvmet_ns *ns) · line 639

Quoted 10 lines
drivers/nvme/target/core.c +10 -0
+		goto out_unlock;
+	}
+
+	if(ns->bdev){
+		ret = nvmet_bdev_ns_revalidate(ns);
+	}else if(ns->file){
+		ret = nvmet_file_ns_revalidate(ns);
+	}
+
+	nvmet_ns_changed(subsys, ns->nsid);

Shouldn't that happen only of the size changed?

Quoted 2 lines

So what is the model here? have some udev rule to
trigger this?

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#6 lore raw

PATCH v2 1/1 nvmet: add revalidate ns sysfs attribute to handle device resize

In reply to Mikhail Malygin

From: Mikhail Malygin <[email protected]>

Currently device size is cached in ns->size field on namespace enable, so any device size change after that can't bee seen by initiator. This patch adds revalidate namespace attribute. Once it is written, target refreshes ns->size property and calls nvmet_ns_changed so initator may perform namespace rescan

---
 drivers/nvme/target/configfs.c    | 14 +++++++++++
 drivers/nvme/target/core.c        | 31 +++++++++++++++++++++++
 drivers/nvme/target/io-cmd-bdev.c | 16 ++++++++++--
 drivers/nvme/target/io-cmd-file.c | 42 ++++++++++++++++++++++---------
 drivers/nvme/target/nvmet.h       |  3 +++
 5 files changed, 92 insertions(+), 14 deletions(-)
drivers/nvme/target/configfs.c +14 -0
index 98613a45bd3b..06566ad1d197 100644
@@ -545,6 +545,19 @@ static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
 
 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
 
+static ssize_t nvmet_ns_revalidate_store(struct config_item *item,
+		const char *page, size_t count)
+{
+	struct nvmet_ns *ns = to_nvmet_ns(item);
+	int ret = 0;
+
+	ret = nvmet_ns_revalidate(ns);
+
+	return ret ? ret : count;
+}
+
+CONFIGFS_ATTR_WO(nvmet_ns_, revalidate);
+
 static struct configfs_attribute *nvmet_ns_attrs[] = {
 	&nvmet_ns_attr_device_path,
 	&nvmet_ns_attr_device_nguid,
@@ -552,6 +565,7 @@ static struct configfs_attribute *nvmet_ns_attrs[] = {
 	&nvmet_ns_attr_ana_grpid,
 	&nvmet_ns_attr_enable,
 	&nvmet_ns_attr_buffered_io,
+	&nvmet_ns_attr_revalidate,
 #ifdef CONFIG_PCI_P2PDMA
 	&nvmet_ns_attr_p2pmem,
 #endif
drivers/nvme/target/core.c +31 -0
index 3a67e244e568..2f5933c2d300 100644
@@ -620,6 +620,37 @@ void nvmet_ns_disable(struct nvmet_ns *ns)
 	mutex_unlock(&subsys->lock);
 }
 
+int nvmet_ns_revalidate(struct nvmet_ns *ns)
+{
+	struct nvmet_subsys *subsys = ns->subsys;
+	loff_t ns_size = ns->size;
+	u32 ns_blksize_shift = ns->blksize_shift;
+	int ret = 0;
+
+	mutex_lock(&subsys->lock);
+	if(!ns->enabled){
+		goto out_unlock;
+	}
+
+	if(ns->bdev){
+		ret = nvmet_bdev_ns_revalidate(ns);
+	}else if(ns->file){
+		ret = nvmet_file_ns_revalidate(ns);
+	}
+
+	if(ret){
+		goto out_unlock;
+	}
+
+	if(ns->size != ns_size || ns->blksize_shift != ns_blksize_shift){
+		nvmet_ns_changed(subsys, ns->nsid);
+	}
+
+out_unlock:
+	mutex_unlock(&subsys->lock);
+	return ret;
+}
+
 void nvmet_ns_free(struct nvmet_ns *ns)
 {
 	nvmet_ns_disable(ns);
drivers/nvme/target/io-cmd-bdev.c +14 -2
index de0bff70ebb6..ea55bbe93813 100644
@@ -47,6 +47,12 @@ void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id)
 	id->nows = to0based(ql->io_opt / ql->logical_block_size);
 }
 
+static void nvmet_bdev_ns_read_size(struct nvmet_ns *ns)
+{
+	ns->size = i_size_read(ns->bdev->bd_inode);
+	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
+}
+
 int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
 {
 	int ret;
@@ -62,8 +68,8 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
 		ns->bdev = NULL;
 		return ret;
 	}
-	ns->size = i_size_read(ns->bdev->bd_inode);
-	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
+
+	nvmet_bdev_ns_read_size(ns);
 	return 0;
 }
 
@@ -75,6 +81,12 @@ void nvmet_bdev_ns_disable(struct nvmet_ns *ns)
 	}
 }
 
+int nvmet_bdev_ns_revalidate(struct nvmet_ns *ns)
+{
+	nvmet_bdev_ns_read_size(ns);
+	return 0;
+}
+
 static u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts)
 {
 	u16 status = NVME_SC_SUCCESS;
drivers/nvme/target/io-cmd-file.c +30 -12

64 lines not on this page · raw message

drivers/nvme/target/nvmet.h +3 -0
index c51f8dd01dc4..a9453e63e5dd 100644
@@ -408,6 +408,7 @@ struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
 void nvmet_put_namespace(struct nvmet_ns *ns);
 int nvmet_ns_enable(struct nvmet_ns *ns);
 void nvmet_ns_disable(struct nvmet_ns *ns);
+int nvmet_ns_revalidate(struct nvmet_ns *ns);
 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
 void nvmet_ns_free(struct nvmet_ns *ns);
 
@@ -485,6 +486,8 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
 int nvmet_file_ns_enable(struct nvmet_ns *ns);
 void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
 void nvmet_file_ns_disable(struct nvmet_ns *ns);
+int nvmet_bdev_ns_revalidate(struct nvmet_ns *ns);
+int nvmet_file_ns_revalidate(struct nvmet_ns *ns);
 u16 nvmet_bdev_flush(struct nvmet_req *req);
 u16 nvmet_file_flush(struct nvmet_req *req);
 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
Signature · 4 lines
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#9 lore raw

Just posted updated version with code style cleanup.

Quoted 11 lines · Christoph Hellwig
Quoted 4 lines · Sagi Grimberg

This looks fine to me,

Christoph, you happy with this version?

Conceptually yes, but it really needs to at least follow the kernel style. I'm also not sure I'm happy about all the extra indirections, but I can be talked into it.

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#10 lore raw

PATCH v3 1/1 nvmet: add revalidate ns sysfs attribute to handle device resize

In reply to Mikhail Malygin

From: Mikhail Malygin <[email protected]>

Currently device size is cached in ns->size field on namespace enable, so any device size change after that can't bee seen by initiator. This patch adds revalidate namespace attribute. Once it is written, target refreshes ns->size property and calls nvmet_ns_changed so initator may perform namespace rescan

---
 drivers/nvme/target/configfs.c    | 14 +++++++++++
 drivers/nvme/target/core.c        | 27 +++++++++++++++++++++
 drivers/nvme/target/io-cmd-bdev.c | 16 +++++++++++--
 drivers/nvme/target/io-cmd-file.c | 39 ++++++++++++++++++++++---------
 drivers/nvme/target/nvmet.h       |  3 +++
 5 files changed, 86 insertions(+), 13 deletions(-)
drivers/nvme/target/configfs.c +14 -0
index 98613a45bd3b..06566ad1d197 100644
@@ -545,6 +545,19 @@ static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
 
 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
 
+static ssize_t nvmet_ns_revalidate_store(struct config_item *item,
+		const char *page, size_t count)
+{
+	struct nvmet_ns *ns = to_nvmet_ns(item);
+	int ret = 0;
+
+	ret = nvmet_ns_revalidate(ns);
+
+	return ret ? ret : count;
+}
+
+CONFIGFS_ATTR_WO(nvmet_ns_, revalidate);
+
 static struct configfs_attribute *nvmet_ns_attrs[] = {
 	&nvmet_ns_attr_device_path,
 	&nvmet_ns_attr_device_nguid,
@@ -552,6 +565,7 @@ static struct configfs_attribute *nvmet_ns_attrs[] = {
 	&nvmet_ns_attr_ana_grpid,
 	&nvmet_ns_attr_enable,
 	&nvmet_ns_attr_buffered_io,
+	&nvmet_ns_attr_revalidate,
 #ifdef CONFIG_PCI_P2PDMA
 	&nvmet_ns_attr_p2pmem,
 #endif
drivers/nvme/target/core.c +27 -0
index 3a67e244e568..593cd919bd95 100644
@@ -620,6 +620,33 @@ void nvmet_ns_disable(struct nvmet_ns *ns)
 	mutex_unlock(&subsys->lock);
 }
 
+int nvmet_ns_revalidate(struct nvmet_ns *ns)
+{
+	struct nvmet_subsys *subsys = ns->subsys;
+	loff_t ns_size = ns->size;
+	u32 ns_blksize_shift = ns->blksize_shift;
+	int ret = 0;
+
+	mutex_lock(&subsys->lock);
+	if (!ns->enabled)
+		goto out_unlock;
+
+	if (ns->bdev)
+		ret = nvmet_bdev_ns_revalidate(ns);
+	else if (ns->file)
+		ret = nvmet_file_ns_revalidate(ns);
+
+	if (ret)
+		goto out_unlock;
+
+	if (ns->size != ns_size || ns->blksize_shift != ns_blksize_shift)
+		nvmet_ns_changed(subsys, ns->nsid);
+
+out_unlock:
+	mutex_unlock(&subsys->lock);
+	return ret;
+}
+
 void nvmet_ns_free(struct nvmet_ns *ns)
 {
 	nvmet_ns_disable(ns);
drivers/nvme/target/io-cmd-bdev.c +14 -2
index de0bff70ebb6..ea55bbe93813 100644
@@ -47,6 +47,12 @@ void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id)
 	id->nows = to0based(ql->io_opt / ql->logical_block_size);
 }
 
+static void nvmet_bdev_ns_read_size(struct nvmet_ns *ns)
+{
+	ns->size = i_size_read(ns->bdev->bd_inode);
+	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
+}
+
 int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
 {
 	int ret;
@@ -62,8 +68,8 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
 		ns->bdev = NULL;
 		return ret;
 	}
-	ns->size = i_size_read(ns->bdev->bd_inode);
-	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
+
+	nvmet_bdev_ns_read_size(ns);
 	return 0;
 }
 
@@ -75,6 +81,12 @@ void nvmet_bdev_ns_disable(struct nvmet_ns *ns)
 	}
 }
 
+int nvmet_bdev_ns_revalidate(struct nvmet_ns *ns)
+{
+	nvmet_bdev_ns_read_size(ns);
+	return 0;
+}
+
 static u16 blk_to_nvme_status(struct nvmet_req *req, blk_status_t blk_sts)
 {
 	u16 status = NVME_SC_SUCCESS;
drivers/nvme/target/io-cmd-file.c +28 -11
index 05453f5d1448..148b2343f9a1 100644
@@ -27,10 +27,30 @@ void nvmet_file_ns_disable(struct nvmet_ns *ns)
 	}
 }
 
+static int nvmet_file_ns_read_size(struct nvmet_ns *ns)
+{
+	int ret;
+	struct kstat stat;
+
+	ret = vfs_getattr(&ns->file->f_path,
+			&stat, STATX_SIZE, AT_STATX_FORCE_SYNC);
+	if (ret)
+		return ret;
+
+	ns->size = stat.size;
+	/*
+	 * i_blkbits can be greater than the universally accepted upper bound,
+	 * so make sure we export a sane namespace lba_shift.
+	 */
+	ns->blksize_shift = min_t(u8,
+			file_inode(ns->file)->i_blkbits, 12);
+
+	return 0;
+}
+
 int nvmet_file_ns_enable(struct nvmet_ns *ns)
 {
 	int flags = O_RDWR | O_LARGEFILE;
-	struct kstat stat;
 	int ret;
 
 	if (!ns->buffered_io)
@@ -43,19 +63,11 @@ int nvmet_file_ns_enable(struct nvmet_ns *ns)
 		return PTR_ERR(ns->file);
 	}
 
-	ret = vfs_getattr(&ns->file->f_path,
-			&stat, STATX_SIZE, AT_STATX_FORCE_SYNC);
+	ret = nvmet_file_ns_read_size(ns);
+
 	if (ret)
 		goto err;
 
-	ns->size = stat.size;
-	/*
-	 * i_blkbits can be greater than the universally accepted upper bound,
-	 * so make sure we export a sane namespace lba_shift.
-	 */
-	ns->blksize_shift = min_t(u8,
-			file_inode(ns->file)->i_blkbits, 12);
-
 	ns->bvec_cache = kmem_cache_create("nvmet-bvec",
 			NVMET_MAX_MPOOL_BVEC * sizeof(struct bio_vec),
 			0, SLAB_HWCACHE_ALIGN, NULL);
@@ -80,6 +92,11 @@ int nvmet_file_ns_enable(struct nvmet_ns *ns)
 	return ret;
 }
 
+int nvmet_file_ns_revalidate(struct nvmet_ns *ns)
+{
+	return nvmet_file_ns_read_size(ns);
+}
+
 static void nvmet_file_init_bvec(struct bio_vec *bv, struct scatterlist *sg)
 {
 	bv->bv_page = sg_page(sg);
drivers/nvme/target/nvmet.h +3 -0
index c51f8dd01dc4..a9453e63e5dd 100644
@@ -408,6 +408,7 @@ struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
 void nvmet_put_namespace(struct nvmet_ns *ns);
 int nvmet_ns_enable(struct nvmet_ns *ns);
 void nvmet_ns_disable(struct nvmet_ns *ns);
+int nvmet_ns_revalidate(struct nvmet_ns *ns);
 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
 void nvmet_ns_free(struct nvmet_ns *ns);
 
@@ -485,6 +486,8 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
 int nvmet_file_ns_enable(struct nvmet_ns *ns);
 void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
 void nvmet_file_ns_disable(struct nvmet_ns *ns);
+int nvmet_bdev_ns_revalidate(struct nvmet_ns *ns);
+int nvmet_file_ns_revalidate(struct nvmet_ns *ns);
 u16 nvmet_bdev_flush(struct nvmet_req *req);
 u16 nvmet_file_flush(struct nvmet_req *req);
 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
Signature · 4 lines
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#11 lore raw

drivers/nvme/target/configfs.c static ssize_t nvmet_ns_buffered_io_store(struct config_item *item, · line 556

Quoted 9 lines
drivers/nvme/target/configfs.c +9 -0
+static ssize_t nvmet_ns_revalidate_store(struct config_item *item,
+		const char *page, size_t count)
+{
+	struct nvmet_ns *ns = to_nvmet_ns(item);
+	int ret = 0;
+
+	ret = nvmet_ns_revalidate(ns);
+
+	return ret ? ret : count;

Nit: no need to initialize ret, and we can use a normal if here:

	ret = nvmet_ns_revalidate(ns);
	if (ret)
		return ret;
	return count;

drivers/nvme/target/io-cmd-bdev.c void nvmet_bdev_ns_disable(struct nvmet_ns *ns) · line 88

Quoted 5 lines
drivers/nvme/target/io-cmd-bdev.c +5 -0
+int nvmet_bdev_ns_revalidate(struct nvmet_ns *ns)
+{
+	nvmet_bdev_ns_read_size(ns);
+	return 0;
+}

I think we can just remove this wrapper.

drivers/nvme/target/io-cmd-file.c void nvmet_file_ns_disable(struct nvmet_ns *ns) · line 36

Quoted 7 lines
drivers/nvme/target/io-cmd-file.c +7 -0
+static int nvmet_file_ns_read_size(struct nvmet_ns *ns)
+{
+	int ret;
+	struct kstat stat;
+
+	ret = vfs_getattr(&ns->file->f_path,
+			&stat, STATX_SIZE, AT_STATX_FORCE_SYNC);

Nit: you can add more arguments to the first line:

	ret = vfs_getattr(&ns->file->f_path, &stat, STATX_SIZE,
			  AT_STATX_FORCE_SYNC);

drivers/nvme/target/io-cmd-file.c int nvmet_file_ns_enable(struct nvmet_ns *ns) · line 98

Quoted 4 lines
drivers/nvme/target/io-cmd-file.c +4 -0
+int nvmet_file_ns_revalidate(struct nvmet_ns *ns)
+{
+	return nvmet_file_ns_read_size(ns);
+}

And we can just remove this wrapper as well.

Or in fact rename the low-level functions to *_revalidate which might be a little more obvious. But either way I don't think we need small wrappers.

Otherwise this looks good to me, thanks for doing the work!

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#12 lore raw

Thanks for the detailed feedback! Posted an updated version to the ml.

Quoted 47 lines · Christoph Hellwig

drivers/nvme/target/configfs.c static ssize_t nvmet_ns_buffered_io_store(struct config_item *item, · line 556

Quoted 10 lines
drivers/nvme/target/configfs.c +9 -0
+static ssize_t nvmet_ns_revalidate_store(struct config_item *item,
+		const char *page, size_t count)
+{
+	struct nvmet_ns *ns = to_nvmet_ns(item);
+	int ret = 0;
+
+	ret = nvmet_ns_revalidate(ns);
+
+	return ret ? ret : count;

Nit: no need to initialize ret, and we can use a normal if here:

	ret = nvmet_ns_revalidate(ns);
	if (ret)
		return ret;
	return count;

drivers/nvme/target/io-cmd-bdev.c void nvmet_bdev_ns_disable(struct nvmet_ns *ns) · line 88

Quoted 5 lines
drivers/nvme/target/io-cmd-bdev.c +5 -0
+int nvmet_bdev_ns_revalidate(struct nvmet_ns *ns)
+{
+	nvmet_bdev_ns_read_size(ns);
+	return 0;
+}

I think we can just remove this wrapper.

drivers/nvme/target/io-cmd-file.c void nvmet_file_ns_disable(struct nvmet_ns *ns) · line 36

Quoted 7 lines
drivers/nvme/target/io-cmd-file.c +7 -0
+static int nvmet_file_ns_read_size(struct nvmet_ns *ns)
+{
+	int ret;
+	struct kstat stat;
+
+	ret = vfs_getattr(&ns->file->f_path,
+			&stat, STATX_SIZE, AT_STATX_FORCE_SYNC);

Nit: you can add more arguments to the first line:

	ret = vfs_getattr(&ns->file->f_path, &stat, STATX_SIZE,
			  AT_STATX_FORCE_SYNC);

drivers/nvme/target/io-cmd-file.c int nvmet_file_ns_enable(struct nvmet_ns *ns) · line 98

Quoted 4 lines
drivers/nvme/target/io-cmd-file.c +4 -0
+int nvmet_file_ns_revalidate(struct nvmet_ns *ns)
+{
+	return nvmet_file_ns_read_size(ns);
+}

And we can just remove this wrapper as well.

Quoted 5 lines

Or in fact rename the low-level functions to *_revalidate which might be a little more obvious. But either way I don't think we need small wrappers.

Otherwise this looks good to me, thanks for doing the work!

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#13 lore raw

PATCH v4 1/1 nvmet: add revalidate ns sysfs attribute to handle device resize

In reply to Mikhail Malygin

From: Mikhail Malygin <[email protected]>

Currently device size is cached in ns->size field on namespace enable, so any device size change after that can't bee seen by initiator. This patch adds revalidate namespace attribute. Once it is written, target refreshes ns->size property and calls nvmet_ns_changed so initator may perform namespace rescan

---
 drivers/nvme/target/configfs.c    | 16 +++++++++++++++
 drivers/nvme/target/core.c        | 27 ++++++++++++++++++++++++
 drivers/nvme/target/io-cmd-bdev.c | 10 +++++++--
 drivers/nvme/target/io-cmd-file.c | 34 +++++++++++++++++++++----------
 drivers/nvme/target/nvmet.h       |  3 +++
 5 files changed, 77 insertions(+), 13 deletions(-)
drivers/nvme/target/configfs.c +16 -0
index 98613a45bd3b..337e967190c5 100644
@@ -545,6 +545,21 @@ static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
 
 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
 
+static ssize_t nvmet_ns_revalidate_store(struct config_item *item,
+		const char *page, size_t count)
+{
+	struct nvmet_ns *ns = to_nvmet_ns(item);
+	int ret;
+
+	ret = nvmet_ns_revalidate(ns);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+CONFIGFS_ATTR_WO(nvmet_ns_, revalidate);
+
 static struct configfs_attribute *nvmet_ns_attrs[] = {
 	&nvmet_ns_attr_device_path,
 	&nvmet_ns_attr_device_nguid,
@@ -552,6 +567,7 @@ static struct configfs_attribute *nvmet_ns_attrs[] = {
 	&nvmet_ns_attr_ana_grpid,
 	&nvmet_ns_attr_enable,
 	&nvmet_ns_attr_buffered_io,
+	&nvmet_ns_attr_revalidate,
 #ifdef CONFIG_PCI_P2PDMA
 	&nvmet_ns_attr_p2pmem,
 #endif
drivers/nvme/target/core.c +27 -0
index 3a67e244e568..122bcbdb5c05 100644
@@ -620,6 +620,33 @@ void nvmet_ns_disable(struct nvmet_ns *ns)
 	mutex_unlock(&subsys->lock);
 }
 
+int nvmet_ns_revalidate(struct nvmet_ns *ns)
+{
+	struct nvmet_subsys *subsys = ns->subsys;
+	loff_t ns_size = ns->size;
+	u32 ns_blksize_shift = ns->blksize_shift;
+	int ret = 0;
+
+	mutex_lock(&subsys->lock);
+	if (!ns->enabled)
+		goto out_unlock;
+
+	if (ns->bdev)
+		nvmet_bdev_ns_read_size(ns);
+	else if (ns->file)
+		ret = nvmet_file_ns_read_size(ns);
+
+	if (ret)
+		goto out_unlock;
+
+	if (ns->size != ns_size || ns->blksize_shift != ns_blksize_shift)
+		nvmet_ns_changed(subsys, ns->nsid);
+
+out_unlock:
+	mutex_unlock(&subsys->lock);
+	return ret;
+}
+
 void nvmet_ns_free(struct nvmet_ns *ns)
 {
 	nvmet_ns_disable(ns);
drivers/nvme/target/io-cmd-bdev.c +8 -2
index de0bff70ebb6..ac8229deeccc 100644
@@ -47,6 +47,12 @@ void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id)
 	id->nows = to0based(ql->io_opt / ql->logical_block_size);
 }
 
+void nvmet_bdev_ns_read_size(struct nvmet_ns *ns)
+{
+	ns->size = i_size_read(ns->bdev->bd_inode);
+	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
+}
+
 int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
 {
 	int ret;
@@ -62,8 +68,8 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
 		ns->bdev = NULL;
 		return ret;
 	}
-	ns->size = i_size_read(ns->bdev->bd_inode);
-	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
+
+	nvmet_bdev_ns_read_size(ns);
 	return 0;
 }
 
drivers/nvme/target/io-cmd-file.c +23 -11
index 05453f5d1448..cff39fea6d85 100644
@@ -27,10 +27,30 @@ void nvmet_file_ns_disable(struct nvmet_ns *ns)
 	}
 }
 
+int nvmet_file_ns_read_size(struct nvmet_ns *ns)
+{
+	int ret;
+	struct kstat stat;
+
+	ret = vfs_getattr(&ns->file->f_path, &stat, STATX_SIZE,
+				AT_STATX_FORCE_SYNC);
+	if (ret)
+		return ret;
+
+	ns->size = stat.size;
+	/*
+	 * i_blkbits can be greater than the universally accepted upper bound,
+	 * so make sure we export a sane namespace lba_shift.
+	 */
+	ns->blksize_shift = min_t(u8,
+			file_inode(ns->file)->i_blkbits, 12);
+
+	return 0;
+}
+
 int nvmet_file_ns_enable(struct nvmet_ns *ns)
 {
 	int flags = O_RDWR | O_LARGEFILE;
-	struct kstat stat;
 	int ret;
 
 	if (!ns->buffered_io)
@@ -43,19 +63,11 @@ int nvmet_file_ns_enable(struct nvmet_ns *ns)
 		return PTR_ERR(ns->file);
 	}
 
-	ret = vfs_getattr(&ns->file->f_path,
-			&stat, STATX_SIZE, AT_STATX_FORCE_SYNC);
+	ret = nvmet_file_ns_read_size(ns);
+
 	if (ret)
 		goto err;
 
-	ns->size = stat.size;
-	/*
-	 * i_blkbits can be greater than the universally accepted upper bound,
-	 * so make sure we export a sane namespace lba_shift.
-	 */
-	ns->blksize_shift = min_t(u8,
-			file_inode(ns->file)->i_blkbits, 12);
-
 	ns->bvec_cache = kmem_cache_create("nvmet-bvec",
 			NVMET_MAX_MPOOL_BVEC * sizeof(struct bio_vec),
 			0, SLAB_HWCACHE_ALIGN, NULL);
drivers/nvme/target/nvmet.h +3 -0
index c51f8dd01dc4..ccdfdcfce65b 100644
@@ -408,6 +408,7 @@ struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
 void nvmet_put_namespace(struct nvmet_ns *ns);
 int nvmet_ns_enable(struct nvmet_ns *ns);
 void nvmet_ns_disable(struct nvmet_ns *ns);
+int nvmet_ns_revalidate(struct nvmet_ns *ns);
 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
 void nvmet_ns_free(struct nvmet_ns *ns);
 
@@ -485,6 +486,8 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
 int nvmet_file_ns_enable(struct nvmet_ns *ns);
 void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
 void nvmet_file_ns_disable(struct nvmet_ns *ns);
+void nvmet_bdev_ns_read_size(struct nvmet_ns *ns);
+int nvmet_file_ns_read_size(struct nvmet_ns *ns);
 u16 nvmet_bdev_flush(struct nvmet_req *req);
 u16 nvmet_file_flush(struct nvmet_req *req);
 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
Signature · 4 lines
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
#14 lore raw

Hi Mikhail,

Please some inline nit comments.

Quoted 4 lines · On 10/7/19 12:57 PM, [email protected] wrote:

From: Mikhail Malygin <[email protected]>
nit :-
Currently device size is cached in ns->size field on namespace enable, so any device size change after that can't bee seen by initiator.

nit :- any device size change after that can't bee seen by the initiator.

Quoted 3 lines

This patch adds revalidate namespace attribute. Once it is written, target refreshes ns->size property and calls nvmet_ns_changed so initator may perform namespace rescan

nit :- so initiator may perform namespace rescan.

Quoted 49 lines
---
  drivers/nvme/target/configfs.c    | 16 +++++++++++++++
  drivers/nvme/target/core.c        | 27 ++++++++++++++++++++++++
  drivers/nvme/target/io-cmd-bdev.c | 10 +++++++--
  drivers/nvme/target/io-cmd-file.c | 34 +++++++++++++++++++++----------
  drivers/nvme/target/nvmet.h       |  3 +++
  5 files changed, 77 insertions(+), 13 deletions(-)
drivers/nvme/target/configfs.c +16 -0
index 98613a45bd3b..337e967190c5 100644
@@ -545,6 +545,21 @@ static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
 
  CONFIGFS_ATTR(nvmet_ns_, buffered_io);
 
+static ssize_t nvmet_ns_revalidate_store(struct config_item *item,
+		const char *page, size_t count)
+{
+	struct nvmet_ns *ns = to_nvmet_ns(item);
+	int ret;
+
+	ret = nvmet_ns_revalidate(ns);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+CONFIGFS_ATTR_WO(nvmet_ns_, revalidate);
+
  static struct configfs_attribute *nvmet_ns_attrs[] = {
  	&nvmet_ns_attr_device_path,
  	&nvmet_ns_attr_device_nguid,
@@ -552,6 +567,7 @@ static struct configfs_attribute *nvmet_ns_attrs[] = {
  	&nvmet_ns_attr_ana_grpid,
  	&nvmet_ns_attr_enable,
  	&nvmet_ns_attr_buffered_io,
+	&nvmet_ns_attr_revalidate,
  #ifdef CONFIG_PCI_P2PDMA
  	&nvmet_ns_attr_p2pmem,
  #endif
drivers/nvme/target/core.c +0 -0
index 3a67e244e568..122bcbdb5c05 100644

drivers/nvme/target/core.c void nvmet_ns_disable(struct nvmet_ns *ns) · line 628

Quoted 9 lines
drivers/nvme/target/core.c +6 -0
@@ -620,6 +620,33 @@ void nvmet_ns_disable(struct nvmet_ns *ns)
  	mutex_unlock(&subsys->lock);
  }
 
+int nvmet_ns_revalidate(struct nvmet_ns *ns)
+{
+	struct nvmet_subsys *subsys = ns->subsys;
+	loff_t ns_size = ns->size;
+	u32 ns_blksize_shift = ns->blksize_shift;
+	int ret = 0;

nit:- Please consider :-

	struct nvmet_subsys *subsys = ns->subsys;
	u32 ns_blksize_shift = ns->blksize_shift;
	loff_t ns_size = ns->size;
	int ret = 0;

drivers/nvme/target/core.c void nvmet_ns_disable(struct nvmet_ns *ns) · line 640

Quoted 12 lines
drivers/nvme/target/core.c +12 -0
+
+	mutex_lock(&subsys->lock);
+	if (!ns->enabled)
+		goto out_unlock;
+
+	if (ns->bdev)
+		nvmet_bdev_ns_read_size(ns);
+	else if (ns->file)
+		ret = nvmet_file_ns_read_size(ns);
+
+	if (ret)
+		goto out_unlock;

Nit:- since ret gets initialized as a part of the ns->file == true why not ?

	if (ns->bdev)
		nvmet_bdev_ns_read_size(ns);
	else if (ns->file) {
		ret = nvmet_file_ns_read_size(ns);
		if (ret)
			goto out_unlock
	}
Quoted 45 lines
drivers/nvme/target/core.c +9 -0
+
+	if (ns->size != ns_size || ns->blksize_shift != ns_blksize_shift)
+		nvmet_ns_changed(subsys, ns->nsid);
+
+out_unlock:
+	mutex_unlock(&subsys->lock);
+	return ret;
+}
+
  void nvmet_ns_free(struct nvmet_ns *ns)
  {
  	nvmet_ns_disable(ns);
drivers/nvme/target/io-cmd-bdev.c +8 -2
index de0bff70ebb6..ac8229deeccc 100644
@@ -47,6 +47,12 @@ void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id)
  	id->nows = to0based(ql->io_opt / ql->logical_block_size);
  }
 
+void nvmet_bdev_ns_read_size(struct nvmet_ns *ns)
+{
+	ns->size = i_size_read(ns->bdev->bd_inode);
+	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
+}
+
  int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
  {
  	int ret;
@@ -62,8 +68,8 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
  		ns->bdev = NULL;
  		return ret;
  	}
-	ns->size = i_size_read(ns->bdev->bd_inode);
-	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
+
+	nvmet_bdev_ns_read_size(ns);
  	return 0;
  }
 
drivers/nvme/target/io-cmd-file.c +0 -0
index 05453f5d1448..cff39fea6d85 100644

drivers/nvme/target/io-cmd-file.c void nvmet_file_ns_disable(struct nvmet_ns *ns) · line 33

Quoted 7 lines
drivers/nvme/target/io-cmd-file.c +4 -0
@@ -27,10 +27,30 @@ void nvmet_file_ns_disable(struct nvmet_ns *ns)
  	}
  }
 
+int nvmet_file_ns_read_size(struct nvmet_ns *ns)
+{
+	int ret;
+	struct kstat stat;

nit again :-

	struct kstat stat;
	int ret;
Quoted 25 lines
drivers/nvme/target/io-cmd-file.c +17 -1
+
+	ret = vfs_getattr(&ns->file->f_path, &stat, STATX_SIZE,
+				AT_STATX_FORCE_SYNC);
+	if (ret)
+		return ret;
+
+	ns->size = stat.size;
+	/*
+	 * i_blkbits can be greater than the universally accepted upper bound,
+	 * so make sure we export a sane namespace lba_shift.
+	 */
+	ns->blksize_shift = min_t(u8,
+			file_inode(ns->file)->i_blkbits, 12);
+
+	return 0;
+}
+
  int nvmet_file_ns_enable(struct nvmet_ns *ns)
  {
  	int flags = O_RDWR | O_LARGEFILE;
-	struct kstat stat;
  	int ret;
 
  	if (!ns->buffered_io)

drivers/nvme/target/io-cmd-file.c int nvmet_file_ns_enable(struct nvmet_ns *ns) · line 66

Quoted 6 lines
drivers/nvme/target/io-cmd-file.c +1 -2
@@ -43,19 +63,11 @@ int nvmet_file_ns_enable(struct nvmet_ns *ns)
  		return PTR_ERR(ns->file);
  	}
 
-	ret = vfs_getattr(&ns->file->f_path,
-			&stat, STATX_SIZE, AT_STATX_FORCE_SYNC);
+	ret = nvmet_file_ns_read_size(ns);

nit:- no need for following new line.

Quoted 29 lines
drivers/nvme/target/io-cmd-file.c +1 -8
+
  	if (ret)
  		goto err;
 
-	ns->size = stat.size;
-	/*
-	 * i_blkbits can be greater than the universally accepted upper bound,
-	 * so make sure we export a sane namespace lba_shift.
-	 */
-	ns->blksize_shift = min_t(u8,
-			file_inode(ns->file)->i_blkbits, 12);
-
  	ns->bvec_cache = kmem_cache_create("nvmet-bvec",
  			NVMET_MAX_MPOOL_BVEC * sizeof(struct bio_vec),
  			0, SLAB_HWCACHE_ALIGN, NULL);
drivers/nvme/target/nvmet.h +1 -0
index c51f8dd01dc4..ccdfdcfce65b 100644
@@ -408,6 +408,7 @@ struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
  void nvmet_put_namespace(struct nvmet_ns *ns);
  int nvmet_ns_enable(struct nvmet_ns *ns);
  void nvmet_ns_disable(struct nvmet_ns *ns);
+int nvmet_ns_revalidate(struct nvmet_ns *ns);
  struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
  void nvmet_ns_free(struct nvmet_ns *ns);
 

drivers/nvme/target/nvmet.h int nvmet_bdev_ns_enable(struct nvmet_ns *ns); · line 493

Quoted 8 lines
drivers/nvme/target/nvmet.h +2 -0
@@ -485,6 +486,8 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
  int nvmet_file_ns_enable(struct nvmet_ns *ns);
  void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
  void nvmet_file_ns_disable(struct nvmet_ns *ns);
+void nvmet_bdev_ns_read_size(struct nvmet_ns *ns);
+int nvmet_file_ns_read_size(struct nvmet_ns *ns);
  u16 nvmet_bdev_flush(struct nvmet_req *req);
  u16 nvmet_file_flush(struct nvmet_req *req);
  void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#15 lore raw

Thanks, posted an updated version.

Quoted 218 lines · Chaitanya Kulkarni

Hi Mikhail,

Please some inline nit comments.

Quoted 4 lines · On 10/7/19 12:57 PM, [email protected] wrote:

From: Mikhail Malygin <[email protected]>
nit :-
Currently device size is cached in ns->size field on namespace enable, so any device size change after that can't bee seen by initiator.

nit :- any device size change after that can't bee seen by the initiator.

Quoted 3 lines

This patch adds revalidate namespace attribute. Once it is written, target refreshes ns->size property and calls nvmet_ns_changed so initator may perform namespace rescan

nit :- so initiator may perform namespace rescan.

Quoted 49 lines
---
 drivers/nvme/target/configfs.c    | 16 +++++++++++++++
 drivers/nvme/target/core.c        | 27 ++++++++++++++++++++++++
 drivers/nvme/target/io-cmd-bdev.c | 10 +++++++--
 drivers/nvme/target/io-cmd-file.c | 34 +++++++++++++++++++++----------
 drivers/nvme/target/nvmet.h       |  3 +++
 5 files changed, 77 insertions(+), 13 deletions(-)
drivers/nvme/target/configfs.c +16 -0
index 98613a45bd3b..337e967190c5 100644
@@ -545,6 +545,21 @@ static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
 
 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
 
+static ssize_t nvmet_ns_revalidate_store(struct config_item *item,
+		const char *page, size_t count)
+{
+	struct nvmet_ns *ns = to_nvmet_ns(item);
+	int ret;
+
+	ret = nvmet_ns_revalidate(ns);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+CONFIGFS_ATTR_WO(nvmet_ns_, revalidate);
+
 static struct configfs_attribute *nvmet_ns_attrs[] = {
 	&nvmet_ns_attr_device_path,
 	&nvmet_ns_attr_device_nguid,
@@ -552,6 +567,7 @@ static struct configfs_attribute *nvmet_ns_attrs[] = {
 	&nvmet_ns_attr_ana_grpid,
 	&nvmet_ns_attr_enable,
 	&nvmet_ns_attr_buffered_io,
+	&nvmet_ns_attr_revalidate,
 #ifdef CONFIG_PCI_P2PDMA
 	&nvmet_ns_attr_p2pmem,
 #endif
drivers/nvme/target/core.c +0 -0
index 3a67e244e568..122bcbdb5c05 100644

drivers/nvme/target/core.c void nvmet_ns_disable(struct nvmet_ns *ns) · line 628

Quoted 9 lines
drivers/nvme/target/core.c +6 -0
@@ -620,6 +620,33 @@ void nvmet_ns_disable(struct nvmet_ns *ns)
 	mutex_unlock(&subsys->lock);
 }
 
+int nvmet_ns_revalidate(struct nvmet_ns *ns)
+{
+	struct nvmet_subsys *subsys = ns->subsys;
+	loff_t ns_size = ns->size;
+	u32 ns_blksize_shift = ns->blksize_shift;
+	int ret = 0;

nit:- Please consider :-

	struct nvmet_subsys *subsys = ns->subsys;
	u32 ns_blksize_shift = ns->blksize_shift;
	loff_t ns_size = ns->size;
	int ret = 0;

drivers/nvme/target/core.c void nvmet_ns_disable(struct nvmet_ns *ns) · line 640

Quoted 12 lines
drivers/nvme/target/core.c +12 -0
+
+	mutex_lock(&subsys->lock);
+	if (!ns->enabled)
+		goto out_unlock;
+
+	if (ns->bdev)
+		nvmet_bdev_ns_read_size(ns);
+	else if (ns->file)
+		ret = nvmet_file_ns_read_size(ns);
+
+	if (ret)
+		goto out_unlock;

Nit:- since ret gets initialized as a part of the ns->file == true why not ?

	if (ns->bdev)
		nvmet_bdev_ns_read_size(ns);
	else if (ns->file) {
		ret = nvmet_file_ns_read_size(ns);
		if (ret)
			goto out_unlock
	}
Quoted 45 lines
drivers/nvme/target/core.c +9 -0
+
+	if (ns->size != ns_size || ns->blksize_shift != ns_blksize_shift)
+		nvmet_ns_changed(subsys, ns->nsid);
+
+out_unlock:
+	mutex_unlock(&subsys->lock);
+	return ret;
+}
+
 void nvmet_ns_free(struct nvmet_ns *ns)
 {
 	nvmet_ns_disable(ns);
drivers/nvme/target/io-cmd-bdev.c +8 -2
index de0bff70ebb6..ac8229deeccc 100644
@@ -47,6 +47,12 @@ void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id)
 	id->nows = to0based(ql->io_opt / ql->logical_block_size);
 }
 
+void nvmet_bdev_ns_read_size(struct nvmet_ns *ns)
+{
+	ns->size = i_size_read(ns->bdev->bd_inode);
+	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
+}
+
 int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
 {
 	int ret;
@@ -62,8 +68,8 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
 		ns->bdev = NULL;
 		return ret;
 	}
-	ns->size = i_size_read(ns->bdev->bd_inode);
-	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
+
+	nvmet_bdev_ns_read_size(ns);
 	return 0;
 }
 
drivers/nvme/target/io-cmd-file.c +0 -0
index 05453f5d1448..cff39fea6d85 100644

drivers/nvme/target/io-cmd-file.c void nvmet_file_ns_disable(struct nvmet_ns *ns) · line 33

Quoted 7 lines
drivers/nvme/target/io-cmd-file.c +4 -0
@@ -27,10 +27,30 @@ void nvmet_file_ns_disable(struct nvmet_ns *ns)
 	}
 }
 
+int nvmet_file_ns_read_size(struct nvmet_ns *ns)
+{
+	int ret;
+	struct kstat stat;

nit again :-

	struct kstat stat;
	int ret;
Quoted 25 lines
drivers/nvme/target/io-cmd-file.c +17 -1
+
+	ret = vfs_getattr(&ns->file->f_path, &stat, STATX_SIZE,
+				AT_STATX_FORCE_SYNC);
+	if (ret)
+		return ret;
+
+	ns->size = stat.size;
+	/*
+	 * i_blkbits can be greater than the universally accepted upper bound,
+	 * so make sure we export a sane namespace lba_shift.
+	 */
+	ns->blksize_shift = min_t(u8,
+			file_inode(ns->file)->i_blkbits, 12);
+
+	return 0;
+}
+
 int nvmet_file_ns_enable(struct nvmet_ns *ns)
 {
 	int flags = O_RDWR | O_LARGEFILE;
-	struct kstat stat;
 	int ret;
 
 	if (!ns->buffered_io)

drivers/nvme/target/io-cmd-file.c int nvmet_file_ns_enable(struct nvmet_ns *ns) · line 66

Quoted 6 lines
drivers/nvme/target/io-cmd-file.c +1 -2
@@ -43,19 +63,11 @@ int nvmet_file_ns_enable(struct nvmet_ns *ns)
 		return PTR_ERR(ns->file);
 	}
 
-	ret = vfs_getattr(&ns->file->f_path,
-			&stat, STATX_SIZE, AT_STATX_FORCE_SYNC);
+	ret = nvmet_file_ns_read_size(ns);

nit:- no need for following new line.

Quoted 27 lines
drivers/nvme/target/io-cmd-file.c +1 -8
+
 	if (ret)
 		goto err;
 
-	ns->size = stat.size;
-	/*
-	 * i_blkbits can be greater than the universally accepted upper bound,
-	 * so make sure we export a sane namespace lba_shift.
-	 */
-	ns->blksize_shift = min_t(u8,
-			file_inode(ns->file)->i_blkbits, 12);
-
 	ns->bvec_cache = kmem_cache_create("nvmet-bvec",
 			NVMET_MAX_MPOOL_BVEC * sizeof(struct bio_vec),
 			0, SLAB_HWCACHE_ALIGN, NULL);
drivers/nvme/target/nvmet.h +1 -0
index c51f8dd01dc4..ccdfdcfce65b 100644
@@ -408,6 +408,7 @@ struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
 void nvmet_put_namespace(struct nvmet_ns *ns);
 int nvmet_ns_enable(struct nvmet_ns *ns);
 void nvmet_ns_disable(struct nvmet_ns *ns);
+int nvmet_ns_revalidate(struct nvmet_ns *ns);
 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
 void nvmet_ns_free(struct nvmet_ns *ns);
 
Quoted 11 lines

drivers/nvme/target/nvmet.h int nvmet_bdev_ns_enable(struct nvmet_ns *ns); · line 493

Quoted 10 lines
drivers/nvme/target/nvmet.h +2 -0
@@ -485,6 +486,8 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
 int nvmet_file_ns_enable(struct nvmet_ns *ns);
 void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
 void nvmet_file_ns_disable(struct nvmet_ns *ns);
+void nvmet_bdev_ns_read_size(struct nvmet_ns *ns);
+int nvmet_file_ns_read_size(struct nvmet_ns *ns);
 u16 nvmet_bdev_flush(struct nvmet_req *req);
 u16 nvmet_file_flush(struct nvmet_req *req);
 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

Reviewed-by on v4 1/1
#16 lore raw
In reply to [email protected]
Quoted 9 lines · On Mon, Oct 07, 2019 at 10:57:09PM +0300, [email protected] wrote:

From: Mikhail Malygin <[email protected]>

Currently device size is cached in ns->size field on namespace enable, so any device size change after that can't bee seen by initiator. This patch adds revalidate namespace attribute. Once it is written, target refreshes ns->size property and calls nvmet_ns_changed so initator may perform namespace rescan

Looks good,

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme

#17 lore raw

PATCH v5 1/1 nvmet: add revalidate ns sysfs attribute to handle device resize

In reply to Mikhail Malygin

From: Mikhail Malygin <[email protected]>

Currently device size is cached in ns->size field on namespace enable, so any device size change after that can't bee seen by the initiator. This patch adds revalidate namespace attribute. Once it is written, target refreshes ns->size property and calls nvmet_ns_changed so initiator may perform namespace rescan

---
 drivers/nvme/target/configfs.c    | 16 +++++++++++++++
 drivers/nvme/target/core.c        | 27 +++++++++++++++++++++++++
 drivers/nvme/target/io-cmd-bdev.c | 10 ++++++++--
 drivers/nvme/target/io-cmd-file.c | 33 ++++++++++++++++++++-----------
 drivers/nvme/target/nvmet.h       |  3 +++
 5 files changed, 76 insertions(+), 13 deletions(-)
drivers/nvme/target/configfs.c +16 -0
index 98613a45bd3b..337e967190c5 100644
@@ -545,6 +545,21 @@ static ssize_t nvmet_ns_buffered_io_store(struct config_item *item,
 
 CONFIGFS_ATTR(nvmet_ns_, buffered_io);
 
+static ssize_t nvmet_ns_revalidate_store(struct config_item *item,
+		const char *page, size_t count)
+{
+	struct nvmet_ns *ns = to_nvmet_ns(item);
+	int ret;
+
+	ret = nvmet_ns_revalidate(ns);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+CONFIGFS_ATTR_WO(nvmet_ns_, revalidate);
+
 static struct configfs_attribute *nvmet_ns_attrs[] = {
 	&nvmet_ns_attr_device_path,
 	&nvmet_ns_attr_device_nguid,
@@ -552,6 +567,7 @@ static struct configfs_attribute *nvmet_ns_attrs[] = {
 	&nvmet_ns_attr_ana_grpid,
 	&nvmet_ns_attr_enable,
 	&nvmet_ns_attr_buffered_io,
+	&nvmet_ns_attr_revalidate,
 #ifdef CONFIG_PCI_P2PDMA
 	&nvmet_ns_attr_p2pmem,
 #endif
drivers/nvme/target/core.c +27 -0
index 3a67e244e568..67c42465fd1e 100644
@@ -620,6 +620,33 @@ void nvmet_ns_disable(struct nvmet_ns *ns)
 	mutex_unlock(&subsys->lock);
 }
 
+int nvmet_ns_revalidate(struct nvmet_ns *ns)
+{
+	struct nvmet_subsys *subsys = ns->subsys;
+	u32 ns_blksize_shift = ns->blksize_shift;
+	loff_t ns_size = ns->size;
+	int ret = 0;
+
+	mutex_lock(&subsys->lock);
+	if (!ns->enabled)
+		goto out_unlock;
+
+	if (ns->bdev)
+		nvmet_bdev_ns_read_size(ns);
+	else if (ns->file) {
+		ret = nvmet_file_ns_read_size(ns);
+		if (ret)
+			goto out_unlock;
+	}
+
+	if (ns->size != ns_size || ns->blksize_shift != ns_blksize_shift)
+		nvmet_ns_changed(subsys, ns->nsid);
+
+out_unlock:
+	mutex_unlock(&subsys->lock);
+	return ret;
+}
+
 void nvmet_ns_free(struct nvmet_ns *ns)
 {
 	nvmet_ns_disable(ns);
drivers/nvme/target/io-cmd-bdev.c +8 -2
index 32008d85172b..0dcb1450df24 100644
@@ -47,6 +47,12 @@ void nvmet_bdev_set_limits(struct block_device *bdev, struct nvme_id_ns *id)
 	id->nows = to0based(ql->io_opt / ql->logical_block_size);
 }
 
+void nvmet_bdev_ns_read_size(struct nvmet_ns *ns)
+{
+	ns->size = i_size_read(ns->bdev->bd_inode);
+	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
+}
+
 int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
 {
 	int ret;
@@ -62,8 +68,8 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns)
 		ns->bdev = NULL;
 		return ret;
 	}
-	ns->size = i_size_read(ns->bdev->bd_inode);
-	ns->blksize_shift = blksize_bits(bdev_logical_block_size(ns->bdev));
+
+	nvmet_bdev_ns_read_size(ns);
 	return 0;
 }
 
drivers/nvme/target/io-cmd-file.c +22 -11
index 05453f5d1448..fea4176b8921 100644
@@ -27,10 +27,30 @@ void nvmet_file_ns_disable(struct nvmet_ns *ns)
 	}
 }
 
+int nvmet_file_ns_read_size(struct nvmet_ns *ns)
+{
+	struct kstat stat;
+	int ret;
+
+	ret = vfs_getattr(&ns->file->f_path, &stat, STATX_SIZE,
+				AT_STATX_FORCE_SYNC);
+	if (ret)
+		return ret;
+
+	ns->size = stat.size;
+	/*
+	 * i_blkbits can be greater than the universally accepted upper bound,
+	 * so make sure we export a sane namespace lba_shift.
+	 */
+	ns->blksize_shift = min_t(u8,
+			file_inode(ns->file)->i_blkbits, 12);
+
+	return 0;
+}
+
 int nvmet_file_ns_enable(struct nvmet_ns *ns)
 {
 	int flags = O_RDWR | O_LARGEFILE;
-	struct kstat stat;
 	int ret;
 
 	if (!ns->buffered_io)
@@ -43,19 +63,10 @@ int nvmet_file_ns_enable(struct nvmet_ns *ns)
 		return PTR_ERR(ns->file);
 	}
 
-	ret = vfs_getattr(&ns->file->f_path,
-			&stat, STATX_SIZE, AT_STATX_FORCE_SYNC);
+	ret = nvmet_file_ns_read_size(ns);
 	if (ret)
 		goto err;
 
-	ns->size = stat.size;
-	/*
-	 * i_blkbits can be greater than the universally accepted upper bound,
-	 * so make sure we export a sane namespace lba_shift.
-	 */
-	ns->blksize_shift = min_t(u8,
-			file_inode(ns->file)->i_blkbits, 12);
-
 	ns->bvec_cache = kmem_cache_create("nvmet-bvec",
 			NVMET_MAX_MPOOL_BVEC * sizeof(struct bio_vec),
 			0, SLAB_HWCACHE_ALIGN, NULL);
drivers/nvme/target/nvmet.h +3 -0
index c51f8dd01dc4..ccdfdcfce65b 100644
@@ -408,6 +408,7 @@ struct nvmet_ns *nvmet_find_namespace(struct nvmet_ctrl *ctrl, __le32 nsid);
 void nvmet_put_namespace(struct nvmet_ns *ns);
 int nvmet_ns_enable(struct nvmet_ns *ns);
 void nvmet_ns_disable(struct nvmet_ns *ns);
+int nvmet_ns_revalidate(struct nvmet_ns *ns);
 struct nvmet_ns *nvmet_ns_alloc(struct nvmet_subsys *subsys, u32 nsid);
 void nvmet_ns_free(struct nvmet_ns *ns);
 
@@ -485,6 +486,8 @@ int nvmet_bdev_ns_enable(struct nvmet_ns *ns);
 int nvmet_file_ns_enable(struct nvmet_ns *ns);
 void nvmet_bdev_ns_disable(struct nvmet_ns *ns);
 void nvmet_file_ns_disable(struct nvmet_ns *ns);
+void nvmet_bdev_ns_read_size(struct nvmet_ns *ns);
+int nvmet_file_ns_read_size(struct nvmet_ns *ns);
 u16 nvmet_bdev_flush(struct nvmet_req *req);
 u16 nvmet_file_flush(struct nvmet_req *req);
 void nvmet_ns_changed(struct nvmet_subsys *subsys, u32 nsid);
Signature · 4 lines
_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme
Reviewed-by on v5 1/1
#18 lore raw

Thanks foe this patch Mikhail.

Looks good.

Quoted 7 lines · On 10/8/19 5:29 AM, [email protected] wrote:

From: Mikhail Malygin<[email protected]>

Currently device size is cached in ns->size field on namespace enable, so any device size change after that can't bee seen by the initiator. This patch adds revalidate namespace attribute. Once it is written, target refreshes ns->size property and calls nvmet_ns_changed so initiator may perform namespace rescan

_______________________________________________
Linux-nvme mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-nvme