linux-block.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] nbd: implement the WRITE_ZEROES command
@ 2024-08-03 13:04 Wouter Verhelst
  2024-08-03 13:04 ` [PATCH 2/2] nbd: nbd_bg_flags_show: add NBD_FLAG_ROTATIONAL Wouter Verhelst
                   ` (3 more replies)
  0 siblings, 4 replies; 16+ messages in thread
From: Wouter Verhelst @ 2024-08-03 13:04 UTC (permalink / raw)
  To: Josef Bacik, Jens Axboe; +Cc: Wouter Verhelst, linux-block, nbd, linux-kernel

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

Add support to the kernel driver for that message.

Signed-off-by: Wouter Verhelst <w@uter.be>
---
 drivers/block/nbd.c      | 8 ++++++++
 include/uapi/linux/nbd.h | 5 ++++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 5b1811b1ba5f..215e7ea9a3c3 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -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;
 
 	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;
 }
diff --git a/include/uapi/linux/nbd.h b/include/uapi/linux/nbd.h
index d75215f2c675..f1d468acfb25 100644
--- a/include/uapi/linux/nbd.h
+++ b/include/uapi/linux/nbd.h
@@ -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
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH 2/2] nbd: nbd_bg_flags_show: add NBD_FLAG_ROTATIONAL
  2024-08-03 13:04 [PATCH 1/2] nbd: implement the WRITE_ZEROES command Wouter Verhelst
@ 2024-08-03 13:04 ` Wouter Verhelst
  2024-08-05 12:52 ` [PATCH 1/2] nbd: implement the WRITE_ZEROES command Eric Blake
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 16+ messages in thread
From: Wouter Verhelst @ 2024-08-03 13:04 UTC (permalink / raw)
  To: Josef Bacik, Jens Axboe; +Cc: Wouter Verhelst, linux-block, nbd, linux-kernel

Also handle NBD_FLAG_ROTATIONAL in our debug helper function

Signed-off-by: Wouter Verhelst <w@uter.be>
---
 drivers/block/nbd.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 215e7ea9a3c3..f65df8a1602d 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -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;
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/2] nbd: implement the WRITE_ZEROES command
  2024-08-03 13:04 [PATCH 1/2] nbd: implement the WRITE_ZEROES command Wouter Verhelst
  2024-08-03 13:04 ` [PATCH 2/2] nbd: nbd_bg_flags_show: add NBD_FLAG_ROTATIONAL Wouter Verhelst
@ 2024-08-05 12:52 ` Eric Blake
  2024-08-06  7:49   ` Wouter Verhelst
  2024-08-06 13:30 ` [PATCH v2 1/3] " Wouter Verhelst
  2024-08-08  7:06 ` [PATCH v3 1/3] nbd: implement the WRITE_ZEROES command Wouter Verhelst
  3 siblings, 1 reply; 16+ messages in thread
From: Eric Blake @ 2024-08-05 12:52 UTC (permalink / raw)
  To: Wouter Verhelst; +Cc: Josef Bacik, Jens Axboe, linux-block, nbd, linux-kernel

On Sat, Aug 03, 2024 at 03:04:30PM GMT, Wouter Verhelst wrote:
> The NBD protocol defines a message for zeroing out a region of an export
> 
> Add support to the kernel driver for that message.
> 
> Signed-off-by: Wouter Verhelst <w@uter.be>
> ---
>  drivers/block/nbd.c      | 8 ++++++++
>  include/uapi/linux/nbd.h | 5 ++++-
>  2 files changed, 12 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
> index 5b1811b1ba5f..215e7ea9a3c3 100644
> --- a/drivers/block/nbd.c
> +++ b/drivers/block/nbd.c
> @@ -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;

Is that number accurate, when the kernel has not yet been taught to
use 64-bit transactions and can therefore only request a 32-bit byte
length on any one transaction?  Would a better limit be
UINT_MAX/blksize?

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.
Virtualization:  qemu.org | libguestfs.org


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 1/2] nbd: implement the WRITE_ZEROES command
  2024-08-05 12:52 ` [PATCH 1/2] nbd: implement the WRITE_ZEROES command Eric Blake
@ 2024-08-06  7:49   ` Wouter Verhelst
  0 siblings, 0 replies; 16+ messages in thread
From: Wouter Verhelst @ 2024-08-06  7:49 UTC (permalink / raw)
  To: Eric Blake; +Cc: Josef Bacik, Jens Axboe, linux-block, nbd, linux-kernel

On Mon, Aug 05, 2024 at 07:52:42AM -0500, Eric Blake wrote:
> On Sat, Aug 03, 2024 at 03:04:30PM GMT, Wouter Verhelst wrote:
> > The NBD protocol defines a message for zeroing out a region of an export
> > 
> > Add support to the kernel driver for that message.
> > 
> > Signed-off-by: Wouter Verhelst <w@uter.be>
> > ---
> >  drivers/block/nbd.c      | 8 ++++++++
> >  include/uapi/linux/nbd.h | 5 ++++-
> >  2 files changed, 12 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
> > index 5b1811b1ba5f..215e7ea9a3c3 100644
> > --- a/drivers/block/nbd.c
> > +++ b/drivers/block/nbd.c
> > @@ -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;
> 
> Is that number accurate, when the kernel has not yet been taught to
> use 64-bit transactions and can therefore only request a 32-bit byte
> length on any one transaction?  Would a better limit be
> UINT_MAX/blksize?

Thanks, good catch.

I copied the logic from the handling of the TRIM command (i.e., the
discard logic), which has the same flawed UINT_MAX behavior. I will fix
this in v2 and add a fix for the discard code.

-- 
     w@uter.{be,co.za}
wouter@{grep.be,fosdem.org,debian.org}

I will have a Tin-Actinium-Potassium mixture, thanks.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v2 1/3] nbd: implement the WRITE_ZEROES command
  2024-08-03 13:04 [PATCH 1/2] nbd: implement the WRITE_ZEROES command Wouter Verhelst
  2024-08-03 13:04 ` [PATCH 2/2] nbd: nbd_bg_flags_show: add NBD_FLAG_ROTATIONAL Wouter Verhelst
  2024-08-05 12:52 ` [PATCH 1/2] nbd: implement the WRITE_ZEROES command Eric Blake
@ 2024-08-06 13:30 ` Wouter Verhelst
  2024-08-06 13:30   ` [PATCH v2 2/3] nbd: nbd_bg_flags_show: add NBD_FLAG_ROTATIONAL Wouter Verhelst
  2024-08-06 13:30   ` [PATCH v2 3/3] nbd: correct the maximum value for discard sectors Wouter Verhelst
  2024-08-08  7:06 ` [PATCH v3 1/3] nbd: implement the WRITE_ZEROES command Wouter Verhelst
  3 siblings, 2 replies; 16+ messages in thread
From: Wouter Verhelst @ 2024-08-06 13:30 UTC (permalink / raw)
  To: Josef Bacik, Jens Axboe; +Cc: Wouter Verhelst, linux-block, nbd, linux-kernel

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

Add support to the kernel driver for that message.

Signed-off-by: Wouter Verhelst <w@uter.be>
---
 drivers/block/nbd.c      | 8 ++++++++
 include/uapi/linux/nbd.h | 5 ++++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 5b1811b1ba5f..58221b89965d 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -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 / blksize;
 
 	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;
 }
diff --git a/include/uapi/linux/nbd.h b/include/uapi/linux/nbd.h
index d75215f2c675..f1d468acfb25 100644
--- a/include/uapi/linux/nbd.h
+++ b/include/uapi/linux/nbd.h
@@ -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
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v2 2/3] nbd: nbd_bg_flags_show: add NBD_FLAG_ROTATIONAL
  2024-08-06 13:30 ` [PATCH v2 1/3] " Wouter Verhelst
@ 2024-08-06 13:30   ` Wouter Verhelst
  2024-08-06 13:30   ` [PATCH v2 3/3] nbd: correct the maximum value for discard sectors Wouter Verhelst
  1 sibling, 0 replies; 16+ messages in thread
From: Wouter Verhelst @ 2024-08-06 13:30 UTC (permalink / raw)
  To: Josef Bacik, Jens Axboe; +Cc: Wouter Verhelst, linux-block, nbd, linux-kernel

Also handle NBD_FLAG_ROTATIONAL in our debug helper function

Signed-off-by: Wouter Verhelst <w@uter.be>
---
 drivers/block/nbd.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 58221b89965d..20e9f9fdeaae 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -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;
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v2 3/3] nbd: correct the maximum value for discard sectors
  2024-08-06 13:30 ` [PATCH v2 1/3] " Wouter Verhelst
  2024-08-06 13:30   ` [PATCH v2 2/3] nbd: nbd_bg_flags_show: add NBD_FLAG_ROTATIONAL Wouter Verhelst
@ 2024-08-06 13:30   ` Wouter Verhelst
  2024-08-07 13:56     ` Josef Bacik
  1 sibling, 1 reply; 16+ messages in thread
From: Wouter Verhelst @ 2024-08-06 13:30 UTC (permalink / raw)
  To: Josef Bacik, Jens Axboe; +Cc: Wouter Verhelst, linux-block, nbd, linux-kernel

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.

Signed-off-by: Wouter Verhelst <w@uter.be>
Fixes: 268283244c0f018dec8bf4a9c69ce50684561f46
---
 drivers/block/nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 20e9f9fdeaae..1457f0c8a4a4 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -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 / blksize;
 	else
 		lim.max_hw_discard_sectors = 0;
 	if (!(nbd->config->flags & NBD_FLAG_SEND_FLUSH)) {
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH v2 3/3] nbd: correct the maximum value for discard sectors
  2024-08-06 13:30   ` [PATCH v2 3/3] nbd: correct the maximum value for discard sectors Wouter Verhelst
@ 2024-08-07 13:56     ` Josef Bacik
  2024-08-07 17:20       ` Damien Le Moal
  2024-08-08  6:55       ` Wouter Verhelst
  0 siblings, 2 replies; 16+ messages in thread
From: Josef Bacik @ 2024-08-07 13:56 UTC (permalink / raw)
  To: Wouter Verhelst; +Cc: Jens Axboe, linux-block, nbd, linux-kernel

On Tue, Aug 06, 2024 at 03:30:56PM +0200, Wouter Verhelst wrote:
> 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.
> 
> Signed-off-by: Wouter Verhelst <w@uter.be>
> Fixes: 268283244c0f018dec8bf4a9c69ce50684561f46
> ---
>  drivers/block/nbd.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
> index 20e9f9fdeaae..1457f0c8a4a4 100644
> --- a/drivers/block/nbd.c
> +++ b/drivers/block/nbd.c
> @@ -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 / blksize;

We use 512 as the "sectors" measurement throughout the block layer, so our limit
is actually

UINT32_MAX >> 9

since we can only send at most UINT32_MAX as our length.  Fix it to be that for
both patches and you should be good.  Thanks,

Josef

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v2 3/3] nbd: correct the maximum value for discard sectors
  2024-08-07 13:56     ` Josef Bacik
@ 2024-08-07 17:20       ` Damien Le Moal
  2024-08-08  6:55       ` Wouter Verhelst
  1 sibling, 0 replies; 16+ messages in thread
From: Damien Le Moal @ 2024-08-07 17:20 UTC (permalink / raw)
  To: Josef Bacik, Wouter Verhelst; +Cc: Jens Axboe, linux-block, nbd, linux-kernel

On 2024/08/07 6:56, Josef Bacik wrote:
> On Tue, Aug 06, 2024 at 03:30:56PM +0200, Wouter Verhelst wrote:
>> 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.
>>
>> Signed-off-by: Wouter Verhelst <w@uter.be>
>> Fixes: 268283244c0f018dec8bf4a9c69ce50684561f46
>> ---
>>  drivers/block/nbd.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
>> index 20e9f9fdeaae..1457f0c8a4a4 100644
>> --- a/drivers/block/nbd.c
>> +++ b/drivers/block/nbd.c
>> @@ -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 / blksize;
> 
> We use 512 as the "sectors" measurement throughout the block layer, so our limit
> is actually
> 
> UINT32_MAX >> 9

UINT_MAX >> SECTOR_SHIFT

would be better.

> 
> since we can only send at most UINT32_MAX as our length.  Fix it to be that for
> both patches and you should be good.  Thanks,
> 
> Josef
> 

-- 
Damien Le Moal
Western Digital Research


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v2 3/3] nbd: correct the maximum value for discard sectors
  2024-08-07 13:56     ` Josef Bacik
  2024-08-07 17:20       ` Damien Le Moal
@ 2024-08-08  6:55       ` Wouter Verhelst
  1 sibling, 0 replies; 16+ messages in thread
From: Wouter Verhelst @ 2024-08-08  6:55 UTC (permalink / raw)
  To: Josef Bacik; +Cc: Jens Axboe, linux-block, nbd, linux-kernel

Hi Josef,

On Wed, Aug 07, 2024 at 09:56:25AM -0400, Josef Bacik wrote:
> We use 512 as the "sectors" measurement throughout the block layer, so our limit
> is actually
> 
> UINT32_MAX >> 9
> 
> since we can only send at most UINT32_MAX as our length.  Fix it to be that for
> both patches and you should be good.  Thanks,

My first stab actually used UINT32_MAX, but that didn't compile.

I investigated and found that for the kernel, UINT32_MAX and UINT_MAX
are actually the same, but in order for me to be able to use UINT32_MAX
(or U32_MAX, which is also defined to the same value), I would need
extra includes.

So I'll stick with the UINT_MAX >> SECTOR_SHIFT definition that Damien
suggested.

Thanks,

-- 
     w@uter.{be,co.za}
wouter@{grep.be,fosdem.org,debian.org}

I will have a Tin-Actinium-Potassium mixture, thanks.

^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH v3 1/3] nbd: implement the WRITE_ZEROES command
  2024-08-03 13:04 [PATCH 1/2] nbd: implement the WRITE_ZEROES command Wouter Verhelst
                   ` (2 preceding siblings ...)
  2024-08-06 13:30 ` [PATCH v2 1/3] " Wouter Verhelst
@ 2024-08-08  7:06 ` Wouter Verhelst
  2024-08-08  7:06   ` [PATCH v3 2/3] nbd: nbd_bg_flags_show: add NBD_FLAG_ROTATIONAL Wouter Verhelst
  2024-08-08  7:06   ` [PATCH v3 3/3] nbd: correct the maximum value for discard sectors Wouter Verhelst
  3 siblings, 2 replies; 16+ messages in thread
From: Wouter Verhelst @ 2024-08-08  7:06 UTC (permalink / raw)
  To: Josef Bacik, Jens Axboe
  Cc: Wouter Verhelst, Eric Blake, Damien Le Moal, linux-block, nbd,
	linux-kernel

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

Add support to the kernel driver for that message.

Signed-off-by: Wouter Verhelst <w@uter.be>
Cc: Eric Blake <eblake@redhat.com>
Cc: Damien Le Moal <dlemoal@kernel.org>
---
 drivers/block/nbd.c      | 8 ++++++++
 include/uapi/linux/nbd.h | 5 ++++-
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index 5b1811b1ba5f..b2b69cc5ca23 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -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;
 }
diff --git a/include/uapi/linux/nbd.h b/include/uapi/linux/nbd.h
index d75215f2c675..f1d468acfb25 100644
--- a/include/uapi/linux/nbd.h
+++ b/include/uapi/linux/nbd.h
@@ -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
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v3 2/3] nbd: nbd_bg_flags_show: add NBD_FLAG_ROTATIONAL
  2024-08-08  7:06 ` [PATCH v3 1/3] nbd: implement the WRITE_ZEROES command Wouter Verhelst
@ 2024-08-08  7:06   ` Wouter Verhelst
  2024-08-08  7:06   ` [PATCH v3 3/3] nbd: correct the maximum value for discard sectors Wouter Verhelst
  1 sibling, 0 replies; 16+ messages in thread
From: Wouter Verhelst @ 2024-08-08  7:06 UTC (permalink / raw)
  To: Josef Bacik, Jens Axboe; +Cc: Wouter Verhelst, linux-block, nbd, linux-kernel

Also handle NBD_FLAG_ROTATIONAL in our debug helper function

Signed-off-by: Wouter Verhelst <w@uter.be>
---
 drivers/block/nbd.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index b2b69cc5ca23..fdcf0bbedf3b 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -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;
 }
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* [PATCH v3 3/3] nbd: correct the maximum value for discard sectors
  2024-08-08  7:06 ` [PATCH v3 1/3] nbd: implement the WRITE_ZEROES command Wouter Verhelst
  2024-08-08  7:06   ` [PATCH v3 2/3] nbd: nbd_bg_flags_show: add NBD_FLAG_ROTATIONAL Wouter Verhelst
@ 2024-08-08  7:06   ` Wouter Verhelst
  2024-08-09 15:22     ` Damien Le Moal
  2024-08-09 15:34     ` Jens Axboe
  1 sibling, 2 replies; 16+ messages in thread
From: Wouter Verhelst @ 2024-08-08  7:06 UTC (permalink / raw)
  To: Josef Bacik, Jens Axboe
  Cc: Wouter Verhelst, Damien Le Moal, linux-block, nbd, linux-kernel

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.

Signed-off-by: Wouter Verhelst <w@uter.be>
Fixes: 268283244c0f018dec8bf4a9c69ce50684561f46
Cc: Damien Le Moal <dlemoal@kernel.org>
---
 drivers/block/nbd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index fdcf0bbedf3b..235ab5f59608 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -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)) {
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 16+ messages in thread

* Re: [PATCH v3 3/3] nbd: correct the maximum value for discard sectors
  2024-08-08  7:06   ` [PATCH v3 3/3] nbd: correct the maximum value for discard sectors Wouter Verhelst
@ 2024-08-09 15:22     ` Damien Le Moal
  2024-08-09 15:34     ` Jens Axboe
  1 sibling, 0 replies; 16+ messages in thread
From: Damien Le Moal @ 2024-08-09 15:22 UTC (permalink / raw)
  To: Wouter Verhelst, Josef Bacik, Jens Axboe; +Cc: linux-block, nbd, linux-kernel

On 2024/08/08 0:06, Wouter Verhelst wrote:
> 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.
> 
> Signed-off-by: Wouter Verhelst <w@uter.be>
> Fixes: 268283244c0f018dec8bf4a9c69ce50684561f46
> Cc: Damien Le Moal <dlemoal@kernel.org>
> ---
>  drivers/block/nbd.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
> index fdcf0bbedf3b..235ab5f59608 100644
> --- a/drivers/block/nbd.c
> +++ b/drivers/block/nbd.c
> @@ -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)) {

Looks good to me.

Reviewed-by: Damien Le Moal <dlemoal@kernel.org>

-- 
Damien Le Moal
Western Digital Research


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3 3/3] nbd: correct the maximum value for discard sectors
  2024-08-08  7:06   ` [PATCH v3 3/3] nbd: correct the maximum value for discard sectors Wouter Verhelst
  2024-08-09 15:22     ` Damien Le Moal
@ 2024-08-09 15:34     ` Jens Axboe
  2024-08-12 13:20       ` Wouter Verhelst
  1 sibling, 1 reply; 16+ messages in thread
From: Jens Axboe @ 2024-08-09 15:34 UTC (permalink / raw)
  To: Wouter Verhelst, Josef Bacik
  Cc: Damien Le Moal, linux-block, nbd, linux-kernel

On 8/8/24 1:06 AM, Wouter Verhelst wrote:
> 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.
> 
> Signed-off-by: Wouter Verhelst <w@uter.be>
> Fixes: 268283244c0f018dec8bf4a9c69ce50684561f46

This isn't the correct way to have a fixes line.

In general, please don't nest next versions under the previous posting,
and it's strongly recommended to have a cover letter that includes that
changed from version N to N+1. Otherwise we have to guess... So please
include that when posting v4.

-- 
Jens Axboe


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH v3 3/3] nbd: correct the maximum value for discard sectors
  2024-08-09 15:34     ` Jens Axboe
@ 2024-08-12 13:20       ` Wouter Verhelst
  0 siblings, 0 replies; 16+ messages in thread
From: Wouter Verhelst @ 2024-08-12 13:20 UTC (permalink / raw)
  To: Jens Axboe; +Cc: Josef Bacik, Damien Le Moal, linux-block, nbd, linux-kernel

On Fri, Aug 09, 2024 at 09:34:55AM -0600, Jens Axboe wrote:
> On 8/8/24 1:06 AM, Wouter Verhelst wrote:
> > Fixes: 268283244c0f018dec8bf4a9c69ce50684561f46
> 
> This isn't the correct way to have a fixes line.

Apologies Jens; beginner's mistake.

> In general, please don't nest next versions under the previous posting,
> and it's strongly recommended to have a cover letter that includes that
> changed from version N to N+1. Otherwise we have to guess... So please
> include that when posting v4.

Right, sorry. "git help send-email" gave an example of how to do
something like this for a "v2" patch series, which made me assume
(incorrectly) that this is what would be wanted. I should have checked
first, I guess.

Will send v4 (hopefully the last one) shortly with those fixes.

-- 
     w@uter.{be,co.za}
wouter@{grep.be,fosdem.org,debian.org}

I will have a Tin-Actinium-Potassium mixture, thanks.

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2024-08-12 13:53 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-03 13:04 [PATCH 1/2] nbd: implement the WRITE_ZEROES command Wouter Verhelst
2024-08-03 13:04 ` [PATCH 2/2] nbd: nbd_bg_flags_show: add NBD_FLAG_ROTATIONAL Wouter Verhelst
2024-08-05 12:52 ` [PATCH 1/2] nbd: implement the WRITE_ZEROES command Eric Blake
2024-08-06  7:49   ` Wouter Verhelst
2024-08-06 13:30 ` [PATCH v2 1/3] " Wouter Verhelst
2024-08-06 13:30   ` [PATCH v2 2/3] nbd: nbd_bg_flags_show: add NBD_FLAG_ROTATIONAL Wouter Verhelst
2024-08-06 13:30   ` [PATCH v2 3/3] nbd: correct the maximum value for discard sectors Wouter Verhelst
2024-08-07 13:56     ` Josef Bacik
2024-08-07 17:20       ` Damien Le Moal
2024-08-08  6:55       ` Wouter Verhelst
2024-08-08  7:06 ` [PATCH v3 1/3] nbd: implement the WRITE_ZEROES command Wouter Verhelst
2024-08-08  7:06   ` [PATCH v3 2/3] nbd: nbd_bg_flags_show: add NBD_FLAG_ROTATIONAL Wouter Verhelst
2024-08-08  7:06   ` [PATCH v3 3/3] nbd: correct the maximum value for discard sectors Wouter Verhelst
2024-08-09 15:22     ` Damien Le Moal
2024-08-09 15:34     ` Jens Axboe
2024-08-12 13:20       ` Wouter Verhelst

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).