mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/2] nbd: add support for DISCARD
@ 2011-09-08  7:44 Paolo Bonzini
  2011-09-08  7:44 ` [PATCH v2 1/2] nbd: add support for feature negotiation Paolo Bonzini
  2011-09-08  7:44 ` [PATCH v2 2/2] nbd: map DISCARD requests to a new nbd request type Paolo Bonzini
  0 siblings, 2 replies; 5+ messages in thread
From: Paolo Bonzini @ 2011-09-08  7:44 UTC (permalink / raw)
  To: linux-kernel; +Cc: Paul Clements, nbd-general

This patch series adds support for a DISCARD command in the nbd
protocol.  qemu-nbd will be the first user of the feature.

Thanks!

v1->v2:
        Change ioctl name to NBD_SET_FLAGS and allow passing all
        flags in the network protocol.

Cc: Paul Clements <Paul.Clements@steeleye.com>
Cc: <nbd-general@lists.sf.net>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Paolo Bonzini (2):
  nbd: add support for feature negotiation
  nbd: map DISCARD requests to a new nbd request type

 drivers/block/nbd.c |   10 ++++++++++++++++++++++--
 include/linux/nbd.h |   28 +++++++++++++++++---------------
 2 files changed, 39 insertions(+), 17 deletions(-)

-- 
1.7.6


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

* [PATCH v2 1/2] nbd: add support for feature negotiation
  2011-09-08  7:44 [PATCH v2 0/2] nbd: add support for DISCARD Paolo Bonzini
@ 2011-09-08  7:44 ` Paolo Bonzini
  2011-09-08  7:44 ` [PATCH v2 2/2] nbd: map DISCARD requests to a new nbd request type Paolo Bonzini
  1 sibling, 0 replies; 5+ messages in thread
From: Paolo Bonzini @ 2011-09-08  7:44 UTC (permalink / raw)
  To: linux-kernel; +Cc: Paul Clements, nbd-general

Add a IOCTL to negotiate features between the server and the kernel
module, with mediation from the userspace client.  Features are stored
in the pre-existing (but so far never written to) flags field; bits for
the flags field are adjusted to actually match the network protocol.

Also, zero out the flags when NBD_DO_IT completes.

Cc: Paul Clements <Paul.Clements@steeleye.com>
Cc: <nbd-general@lists.sf.net>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 drivers/block/nbd.c |   10 +++++++++-
 include/linux/nbd.h |   28 ++++++++++++++--------------
 2 files changed, 23 insertions(+), 15 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index be23aec..c4fe9c8 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -85,6 +85,7 @@ static const char *ioctl_cmd_to_ascii(int cmd)
 	case NBD_PRINT_DEBUG: return "print-debug";
 	case NBD_SET_SIZE_BLOCKS: return "set-size-blocks";
 	case NBD_DISCONNECT: return "disconnect";
+	case NBD_SET_FLAGS: return "set-flags";
 	case BLKROSET: return "set-read-only";
 	case BLKFLSBUF: return "flush-buffer-cache";
 	}
@@ -453,7 +453,7 @@ static void nbd_handle_req(struct nbd_device *lo, struct request *req)
 	nbd_cmd(req) = NBD_CMD_READ;
 	if (rq_data_dir(req) == WRITE) {
 		nbd_cmd(req) = NBD_CMD_WRITE;
-		if (lo->flags & NBD_READ_ONLY) {
+		if (lo->flags & NBD_FLAG_READ_ONLY) {
 			printk(KERN_ERR "%s: Write on read-only\n",
 					lo->disk->disk_name);
 			goto error_out;
@@ -634,6 +635,12 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *lo,
 		lo->xmit_timeout = arg * HZ;
 		return 0;
 
+	case NBD_SET_FLAGS:
+		if (((u64) arg) & (-1ULL << 32))
+			return -EINVAL;
+		lo->flags = arg;
+		return 0;
+
 	case NBD_SET_SIZE_BLOCKS:
 		lo->bytesize = ((u64) arg) * lo->blksize;
 		bdev->bd_inode->i_size = lo->bytesize;
@@ -672,6 +679,7 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *lo,
 		printk(KERN_WARNING "%s: queue cleared\n", lo->disk->disk_name);
 		if (file)
 			fput(file);
+		lo->flags = 0;
 		lo->bytesize = 0;
 		bdev->bd_inode->i_size = 0;
 		set_capacity(lo->disk, 0);
diff --git a/include/linux/nbd.h b/include/linux/nbd.h
index 0582054..926f19d 100644
--- a/include/linux/nbd.h
+++ b/include/linux/nbd.h
@@ -17,16 +17,20 @@
 
 #include <linux/types.h>
 
-#define NBD_SET_SOCK	_IO( 0xab, 0 )
-#define NBD_SET_BLKSIZE	_IO( 0xab, 1 )
-#define NBD_SET_SIZE	_IO( 0xab, 2 )
-#define NBD_DO_IT	_IO( 0xab, 3 )
-#define NBD_CLEAR_SOCK	_IO( 0xab, 4 )
-#define NBD_CLEAR_QUE	_IO( 0xab, 5 )
-#define NBD_PRINT_DEBUG	_IO( 0xab, 6 )
-#define NBD_SET_SIZE_BLOCKS	_IO( 0xab, 7 )
-#define NBD_DISCONNECT  _IO( 0xab, 8 )
-#define NBD_SET_TIMEOUT _IO( 0xab, 9 )
+#define NBD_SET_SOCK		_IO(0xab, 0)
+#define NBD_SET_BLKSIZE		_IO(0xab, 1)
+#define NBD_SET_SIZE		_IO(0xab, 2)
+#define NBD_DO_IT		_IO(0xab, 3)
+#define NBD_CLEAR_SOCK		_IO(0xab, 4)
+#define NBD_CLEAR_QUE		_IO(0xab, 5)
+#define NBD_PRINT_DEBUG		_IO(0xab, 6)
+#define NBD_SET_SIZE_BLOCKS	_IO(0xab, 7)
+#define NBD_DISCONNECT		_IO(0xab, 8)
+#define NBD_SET_TIMEOUT		_IO(0xab, 9)
+#define NBD_SET_FLAGS		_IO(0xab, 10)
+
+/* values for flags field */
+#define NBD_FLAG_READ_ONLY	(1 << 1)
 
 enum {
 	NBD_CMD_READ = 0,
@@ -42,10 +46,6 @@ enum {
 #include <linux/wait.h>
 #include <linux/mutex.h>
 
-/* values for flags field */
-#define NBD_READ_ONLY 0x0001
-#define NBD_WRITE_NOCHK 0x0002
-
 struct request;
 
 struct nbd_device {
-- 
1.7.6



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

* [PATCH v2 2/2] nbd: map DISCARD requests to a new nbd request type
  2011-09-08  7:44 [PATCH v2 0/2] nbd: add support for DISCARD Paolo Bonzini
  2011-09-08  7:44 ` [PATCH v2 1/2] nbd: add support for feature negotiation Paolo Bonzini
@ 2011-09-08  7:44 ` Paolo Bonzini
  2011-09-08  8:42   ` [Nbd] " Alex Bligh
  1 sibling, 1 reply; 5+ messages in thread
From: Paolo Bonzini @ 2011-09-08  7:44 UTC (permalink / raw)
  To: linux-kernel; +Cc: Paul Clements, nbd-general

When the feature is enabled, set QUEUE_FLAG_DISCARD and transmit those as
NBD_CMD_TRIM requests.

I used "trim" instead of "discard" to avoid confusion with the existing
NBD_CMD_DISC that is used for disconnect.

Cc: Paul Clements <Paul.Clements@steeleye.com>
Cc: <nbd-general@lists.sf.net>
Reviewed-by: Paul Clements <Paul.Clements@steeleye.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
        This patch is the same as in v1.

 drivers/block/nbd.c |   14 +++++++++++++-
 include/linux/nbd.h |    4 +++-
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index c4fe9c8..71ebaab 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -98,6 +98,7 @@ static const char *nbdcmd_to_ascii(int cmd)
 	case  NBD_CMD_READ: return "read";
 	case NBD_CMD_WRITE: return "write";
 	case  NBD_CMD_DISC: return "disconnect";
+	case  NBD_CMD_TRIM: return "trim (discard)";
 	}
 	return "invalid";
 }
@@ -453,12 +453,16 @@ static void nbd_handle_req(struct nbd_device *lo, struct request *req)
 
 	nbd_cmd(req) = NBD_CMD_READ;
 	if (rq_data_dir(req) == WRITE) {
-		nbd_cmd(req) = NBD_CMD_WRITE;
 		if (lo->flags & NBD_FLAG_READ_ONLY) {
 			printk(KERN_ERR "%s: Write on read-only\n",
 					lo->disk->disk_name);
 			goto error_out;
 		}
+		if ((req->cmd_flags & REQ_DISCARD)) {
+			WARN_ON(!(lo->flags & NBD_FLAG_HAS_TRIM));
+			nbd_cmd(req) = NBD_CMD_TRIM;
+		} else
+			nbd_cmd(req) = NBD_CMD_WRITE;
 	}
 
 	req->errors = 0;
@@ -659,6 +664,9 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *lo,
 			return -EINVAL;
 
 		mutex_unlock(&lo->tx_lock);
+		if (lo->flags & NBD_FLAG_HAS_TRIM)
+			queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
+						lo->disk->queue);
 
 		thread = kthread_create(nbd_thread, lo, lo->disk->disk_name);
 		if (IS_ERR(thread)) {
@@ -677,6 +685,7 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *lo,
 		lo->file = NULL;
 		nbd_clear_que(lo);
 		printk(KERN_WARNING "%s: queue cleared\n", lo->disk->disk_name);
+		queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, lo->disk->queue);
 		if (file)
 			fput(file);
 		lo->flags = 0;
@@ -796,6 +805,9 @@ static int __init nbd_init(void)
 		 * Tell the block layer that we are not a rotational device
 		 */
 		queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue);
+		disk->queue->limits.discard_granularity = 512;
+		disk->queue->limits.max_discard_sectors = UINT_MAX;
+		disk->queue->limits.discard_zeroes_data = 0;
 	}
 
 	if (register_blkdev(NBD_MAJOR, "nbd")) {
diff --git a/include/linux/nbd.h b/include/linux/nbd.h
index 926f19d..7048463 100644
--- a/include/linux/nbd.h
+++ b/include/linux/nbd.h
@@ -33,11 +33,13 @@
 
 /* values for flags field */
 #define NBD_FLAG_READ_ONLY	(1 << 1)
+#define NBD_FLAG_HAS_TRIM	(1 << 2)
 
 enum {
 	NBD_CMD_READ = 0,
 	NBD_CMD_WRITE = 1,
-	NBD_CMD_DISC = 2
+	NBD_CMD_DISC = 2,
+	NBD_CMD_TRIM = 3
 };
 
 #define nbd_cmd(req) ((req)->cmd[0])
-- 
1.7.6


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

* Re: [Nbd] [PATCH v2 2/2] nbd: map DISCARD requests to a new nbd request type
  2011-09-08  7:44 ` [PATCH v2 2/2] nbd: map DISCARD requests to a new nbd request type Paolo Bonzini
@ 2011-09-08  8:42   ` Alex Bligh
  2011-09-08  9:41     ` Paolo Bonzini
  0 siblings, 1 reply; 5+ messages in thread
From: Alex Bligh @ 2011-09-08  8:42 UTC (permalink / raw)
  To: Paolo Bonzini, linux-kernel; +Cc: nbd-general, Paul Clements, Alex Bligh

Paolo,

--On 8 September 2011 09:44:58 +0200 Paolo Bonzini <pbonzini@redhat.com> 
wrote:

> When the feature is enabled, set QUEUE_FLAG_DISCARD and transmit those as
> NBD_CMD_TRIM requests.
>
> I used "trim" instead of "discard" to avoid confusion with the existing
> NBD_CMD_DISC that is used for disconnect.
>
> Cc: Paul Clements <Paul.Clements@steeleye.com>
> Cc: <nbd-general@lists.sf.net>
> Reviewed-by: Paul Clements <Paul.Clements@steeleye.com>
> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Do you have a patch for doc/proto.txt in the NBD protocol, or indeed
a patch for the server to implement this?

-- 
Alex Bligh

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

* Re: [Nbd] [PATCH v2 2/2] nbd: map DISCARD requests to a new nbd request type
  2011-09-08  8:42   ` [Nbd] " Alex Bligh
@ 2011-09-08  9:41     ` Paolo Bonzini
  0 siblings, 0 replies; 5+ messages in thread
From: Paolo Bonzini @ 2011-09-08  9:41 UTC (permalink / raw)
  To: Alex Bligh; +Cc: nbd-general, Paul Clements, linux-kernel

> Do you have a patch for doc/proto.txt in the NBD protocol, or indeed
> a patch for the server to implement this?

I have a patch for the client to invoke the new ioctl, and a patch for
qemu-nbd to both invoke the new ioctl (client-side) and implement the
command (server-side).

I'll look at doc/proto.txt and propose a patch, that can be done off
LKML if the series is accepted.

Paolo

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

end of thread, other threads:[~2011-09-08  9:41 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-08  7:44 [PATCH v2 0/2] nbd: add support for DISCARD Paolo Bonzini
2011-09-08  7:44 ` [PATCH v2 1/2] nbd: add support for feature negotiation Paolo Bonzini
2011-09-08  7:44 ` [PATCH v2 2/2] nbd: map DISCARD requests to a new nbd request type Paolo Bonzini
2011-09-08  8:42   ` [Nbd] " Alex Bligh
2011-09-08  9:41     ` Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®