Skip to contentSkip to conversation

NVMe: Add revalidate_disk callback

PATCH New Keith Busch 2 participants 3 messages Sep 10, 2014 – Sep 11, 2014

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

PATCH 1/1 NVMe: Add revalidate_disk callback

This adds a callback to revalidate the disk and change its block size and capacity if needed. Before, a user would have to remove + rescan an entire device if they changed the logical block size using an NVMe Format or other vendor specific command; now they can just run something that issues the BLKRRPART IOCTL, like

 # hdparm -z /dev/nvmeXnY

This can also be used in response to the 1.2 Spec's Namespace Attribute Change asynchronous event.

Signed-off-by Keith Busch <keith.busch at intel.com>
---
 drivers/block/nvme-core.c |   30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
drivers/block/nvme-core.c +30 -0
index 1b9cd27..2e0f95f 100644
@@ -1833,6 +1833,35 @@ static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo)
 	return 0;
 }
 
+static int nvme_revalidate_disk(struct gendisk *disk)
+{
+	struct nvme_ns *ns = disk->private_data;
+	struct nvme_dev *dev = ns->dev;
+	struct nvme_id_ns *id;
+	dma_addr_t dma_addr;
+	int lbaf;
+
+	id = dma_alloc_coherent(&dev->pci_dev->dev, 4096, &dma_addr,
+								GFP_KERNEL);
+	if (!id) {
+		dev_warn(&dev->pci_dev->dev, "%s: Memory alocation failure\n",
+								__func__);
+		return 0;
+	}
+
+	if (nvme_identify(dev, ns->ns_id, 0, dma_addr))
+		goto free;
+
+	lbaf = id->flbas & 0xf;
+	ns->lba_shift = id->lbaf[lbaf].ds;
+
+	blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
+	set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift - 9));
+ free:
+	dma_free_coherent(&dev->pci_dev->dev, 4091, id, dma_addr);
+	return 0;
+}
+
 static const struct block_device_operations nvme_fops = {
 	.owner		= THIS_MODULE,
 	.ioctl		= nvme_ioctl,
@@ -1840,6 +1869,7 @@ static const struct block_device_operations nvme_fops = {
 	.open		= nvme_open,
 	.release	= nvme_release,
 	.getgeo		= nvme_getgeo,
+	.revalidate_disk= nvme_revalidate_disk,
 };
 
 static void nvme_resubmit_iods(struct nvme_queue *nvmeq)
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH 1/1 NVMe: Add revalidate_disk callback

Quoted 54 lines

-----Original Message-----
From: Linux-nvme [mailto:linux-nvme-bounces at lists.infradead.org] On Behalf Of Keith Busch
Sent: Wednesday, September 10, 2014 4:21 PM
To: linux-nvme at lists.infradead.org
Cc: Keith Busch
Subject: [PATCH] NVMe: Add revalidate_disk callback

This adds a callback to revalidate the disk and change its block size and capacity if needed. Before, a user would have to remove + rescan an entire device if they changed the logical block size using an NVMe Format or other vendor specific command; now they can just run something
that issues the BLKRRPART IOCTL, like

 # hdparm -z /dev/nvmeXnY

This can also be used in response to the 1.2 Spec's Namespace Attribute Change asynchronous event.

Signed-off-by Keith Busch <keith.busch at intel.com>
---
 drivers/block/nvme-core.c |   30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
drivers/block/nvme-core.c +17 -0
index 1b9cd27..2e0f95f 100644
@@ -1833,6 +1833,35 @@ static int nvme_getgeo(struct block_device *bd,
 struct hd_geometry *geo)
 	return 0;
 }
 
+static int nvme_revalidate_disk(struct gendisk *disk)
+{
+	struct nvme_ns *ns = disk->private_data;
+	struct nvme_dev *dev = ns->dev;
+	struct nvme_id_ns *id;
+	dma_addr_t dma_addr;
+	int lbaf;
+
+	id = dma_alloc_coherent(&dev->pci_dev->dev, 4096, &dma_addr,
+								GFP_KERNEL);
+	if (!id) {
+		dev_warn(&dev->pci_dev->dev, "%s: Memory alocation
 failure\n",
+								__func__);
+		return 0;
+	}
+
+	if (nvme_identify(dev, ns->ns_id, 0, dma_addr))

drivers/block/nvme-core.c static int nvme_getgeo(struct block_device *bd, · line 1864

Quoted 10 lines
drivers/block/nvme-core.c +9 -0
+		goto free;
+
+	lbaf = id->flbas & 0xf;
+	ns->lba_shift = id->lbaf[lbaf].ds;
+
+	blk_queue_logical_block_size(ns->queue, 1 << ns->lba_shift);
+	set_capacity(disk, le64_to_cpup(&id->nsze) << (ns->lba_shift -
 9));
+ free:
+	dma_free_coherent(&dev->pci_dev->dev, 4091, id, dma_addr);

4096, right?

#2 lore raw

PATCH 1/1 NVMe: Add revalidate_disk callback

Quoted 4 lines · Sam Bradshaw (sbradshaw)

drivers/block/nvme-core.c static int nvme_getgeo(struct block_device *bd, struct hd_geometry *geo) · line 1861

Quoted 2 lines

+ free:

drivers/block/nvme-core.c +1 -0
+	dma_free_coherent(&dev->pci_dev->dev, 4091, id, dma_addr);

4096, right?

Oops, you're right. Thanks for the catch.