Skip to contentSkip to conversation

nbd: WRITE_ZEROES and a few fixes

PATCH Applied Wouter Verhelst 2 participants 6 messages Aug 12, 2024 – Sep 6, 2024

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

v4 cover nbd: WRITE_ZEROES and a few fixes

Implement the WRITE_ZEROES command for the NBD kernel driver. While here, add NBD_FLAG_ROTATIONAL to the function that decodes our flags for debugfs.

---
v2: Divide UINT_MAX by blksize for the maximum number of sectors supported by trim/discard and write_zeroes commands, as NBD uses 32-bit values for byte lengths, and otherwise that wouldn't fit.
v3: Use the SECTOR_SHIFT value as a right-shift operand, rather than the blksize value, to get the correct operand.
v4: Fix commit messages to use correct style. Thanks, Jens.

---
 drivers/block/nbd.c      |   12 +++++++++++-
 include/uapi/linux/nbd.h |    5 ++++-
 2 files changed, 15 insertions(+), 2 deletions(-)
#1 lore raw

PATCH v4 1/3 nbd: implement the WRITE_ZEROES command

The NBD protocol defines a message for zeroing out a region of an export

Add support to the kernel driver for that message.

---
 drivers/block/nbd.c      | 8 ++++++++
 include/uapi/linux/nbd.h | 5 ++++-
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/block/nbd.c +8 -0
index 5b1811b1ba5f..b2b69cc5ca23 100644
@@ -352,6 +352,8 @@ static int __nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
 	}
 	if (nbd->config->flags & NBD_FLAG_ROTATIONAL)
 		lim.features |= BLK_FEAT_ROTATIONAL;
+	if (nbd->config->flags & NBD_FLAG_SEND_WRITE_ZEROES)
+		lim.max_write_zeroes_sectors = UINT_MAX >> SECTOR_SHIFT;
 
 	lim.logical_block_size = blksize;
 	lim.physical_block_size = blksize;
@@ -421,6 +423,8 @@ static u32 req_to_nbd_cmd_type(struct request *req)
 		return NBD_CMD_WRITE;
 	case REQ_OP_READ:
 		return NBD_CMD_READ;
+	case REQ_OP_WRITE_ZEROES:
+		return NBD_CMD_WRITE_ZEROES;
 	default:
 		return U32_MAX;
 	}
@@ -637,6 +641,8 @@ static blk_status_t nbd_send_cmd(struct nbd_device *nbd, struct nbd_cmd *cmd,
 
 	if (req->cmd_flags & REQ_FUA)
 		nbd_cmd_flags |= NBD_CMD_FLAG_FUA;
+	if ((req->cmd_flags & REQ_NOUNMAP) && (type == NBD_CMD_WRITE_ZEROES))
+		nbd_cmd_flags |= NBD_CMD_FLAG_NO_HOLE;
 
 	/* We did a partial send previously, and we at least sent the whole
 	 * request struct, so just go and send the rest of the pages in the
@@ -1706,6 +1712,8 @@ static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
 		seq_puts(s, "NBD_FLAG_SEND_FUA\n");
 	if (flags & NBD_FLAG_SEND_TRIM)
 		seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
+	if (flags & NBD_FLAG_SEND_WRITE_ZEROES)
+		seq_puts(s, "NBD_FLAG_SEND_WRITE_ZEROES\n");
 
 	return 0;
 }
include/uapi/linux/nbd.h +4 -1
index d75215f2c675..f1d468acfb25 100644
@@ -42,8 +42,9 @@ enum {
 	NBD_CMD_WRITE = 1,
 	NBD_CMD_DISC = 2,
 	NBD_CMD_FLUSH = 3,
-	NBD_CMD_TRIM = 4
+	NBD_CMD_TRIM = 4,
 	/* userspace defines additional extension commands */
+	NBD_CMD_WRITE_ZEROES = 6,
 };
 
 /* values for flags field, these are server interaction specific. */
@@ -53,11 +54,13 @@ enum {
 #define NBD_FLAG_SEND_FUA	(1 << 3) /* send FUA (forced unit access) */
 #define NBD_FLAG_ROTATIONAL	(1 << 4) /* device is rotational */
 #define NBD_FLAG_SEND_TRIM	(1 << 5) /* send trim/discard */
+#define NBD_FLAG_SEND_WRITE_ZEROES (1 << 6) /* supports WRITE_ZEROES */
 /* there is a gap here to match userspace */
 #define NBD_FLAG_CAN_MULTI_CONN	(1 << 8)	/* Server supports multiple connections per export. */
 
 /* values for cmd flags in the upper 16 bits of request type */
 #define NBD_CMD_FLAG_FUA	(1 << 16) /* FUA (forced unit access) op */
+#define NBD_CMD_FLAG_NO_HOLE	(1 << 17) /* Do not punch a hole for WRITE_ZEROES */
 
 /* These are client behavior specific flags. */
 #define NBD_CFLAG_DESTROY_ON_DISCONNECT	(1 << 0) /* delete the nbd device on
Signature · 1 line
2.43.0
#2 lore raw

PATCH v4 2/3 nbd: nbd_bg_flags_show: add NBD_FLAG_ROTATIONAL

In reply to Wouter Verhelst

Also handle NBD_FLAG_ROTATIONAL in our debug helper function

---
 drivers/block/nbd.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/block/nbd.c +2 -0
index b2b69cc5ca23..fdcf0bbedf3b 100644
@@ -1714,6 +1714,8 @@ static int nbd_dbg_flags_show(struct seq_file *s, void *unused)
 		seq_puts(s, "NBD_FLAG_SEND_TRIM\n");
 	if (flags & NBD_FLAG_SEND_WRITE_ZEROES)
 		seq_puts(s, "NBD_FLAG_SEND_WRITE_ZEROES\n");
+	if (flags & NBD_FLAG_ROTATIONAL)
+		seq_puts(s, "NBD_FLAG_ROTATIONAL\n");
 
 	return 0;
 }
Signature · 1 line
2.43.0
#3 lore raw

PATCH v4 3/3 nbd: correct the maximum value for discard sectors

In reply to Wouter Verhelst

The version of the NBD protocol implemented by the kernel driver currently has a 32 bit field for length values. As the NBD protocol uses bytes as a unit of length, length values larger than 2^32 bytes cannot be expressed.

Update the max_hw_discard_sectors field to match that.

Fixes 268283244c0f ("nbd: use the atomic queue limits API in nbd_set_size")
Cc Eric Blake <[email protected]>
---
 drivers/block/nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/block/nbd.c +1 -1
index fdcf0bbedf3b..235ab5f59608 100644
@@ -339,7 +339,7 @@ static int __nbd_set_size(struct nbd_device *nbd, loff_t bytesize,
 
 	lim = queue_limits_start_update(nbd->disk->queue);
 	if (nbd->config->flags & NBD_FLAG_SEND_TRIM)
-		lim.max_hw_discard_sectors = UINT_MAX;
+		lim.max_hw_discard_sectors = UINT_MAX >> SECTOR_SHIFT;
 	else
 		lim.max_hw_discard_sectors = 0;
 	if (!(nbd->config->flags & NBD_FLAG_SEND_FLUSH)) {
Signature · 1 line
2.43.0
#4 lore raw
In reply to Wouter Verhelst

Hi Jens,

Has been a few weeks since I sent this. Friendly ping?

Quoted 17 lines · Wouter Verhelst

Implement the WRITE_ZEROES command for the NBD kernel driver. While here, add NBD_FLAG_ROTATIONAL to the function that decodes our flags for debugfs.

---
v2: Divide UINT_MAX by blksize for the maximum number of sectors supported by trim/discard and write_zeroes commands, as NBD uses 32-bit values for byte lengths, and otherwise that wouldn't fit.
v3: Use the SECTOR_SHIFT value as a right-shift operand, rather than the blksize value, to get the correct operand.
v4: Fix commit messages to use correct style. Thanks, Jens.

---
 drivers/block/nbd.c      |   12 +++++++++++-
 include/uapi/linux/nbd.h |    5 ++++-
 2 files changed, 15 insertions(+), 2 deletions(-)
Signature · 4 lines
     w@uter.{be,co.za}
wouter@{grep.be,fosdem.org,debian.org}

I will have a Tin-Actinium-Potassium mixture, thanks.
maintainer Applied
#5 lore raw
In reply to Wouter Verhelst
Quoted 4 lines · Wouter Verhelst

Implement the WRITE_ZEROES command for the NBD kernel driver. While here, add NBD_FLAG_ROTATIONAL to the function that decodes our flags for debugfs.

Applied, thanks!

[1/3] nbd: implement the WRITE_ZEROES command

      commit: e49dacc71ec2621ce4c422cd5605d4d06f7807b0
[2/3] nbd: nbd_bg_flags_show: add NBD_FLAG_ROTATIONAL
      commit: 41372f5c9a866365e212809b3543ae8cb5b2542b
[3/3] nbd: correct the maximum value for discard sectors
      commit: 296dbc72d29085d5fc34430d0760423071e9e81d

Best regards,

Signature · 1 line
Jens Axboe