linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] add discard support to nbd
@ 2012-08-28 21:42 Paul Clements
  2012-08-28 21:43 ` [PATCH 1/2] " Paul Clements
  0 siblings, 1 reply; 3+ messages in thread
From: Paul Clements @ 2012-08-28 21:42 UTC (permalink / raw)
  To: kernel list; +Cc: andrew morton

This patchset adds discard request support to nbd. This should be good
for inclusion in next.

The first patch adds a set-flags ioctl, allowing various option flags
to be set on an nbd device. One of the new flags tells the nbd client
to send discard requests to the server.
The second patch adds handling of discard requests to nbd when
NBD_FLAG_SEND_TRIM is set.

Thanks,
Paul

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

* [PATCH 1/2] add discard support to nbd
  2012-08-28 21:42 [PATCH 0/2] add discard support to nbd Paul Clements
@ 2012-08-28 21:43 ` Paul Clements
  2012-08-28 21:44   ` [PATCH 2/2] " Paul Clements
  0 siblings, 1 reply; 3+ messages in thread
From: Paul Clements @ 2012-08-28 21:43 UTC (permalink / raw)
  To: kernel list; +Cc: andrew morton

[-- Attachment #1: Type: text/plain, Size: 1 bytes --]



[-- Attachment #2: nbd-set-flags-ioctl.diff --]
[-- Type: application/octet-stream, Size: 2194 bytes --]

Description: This patch adds a set-flags ioctl, allowing various option
flags to be set on an nbd device.

Signed-off-by: Paul Clements <paul.clements@steeleye.com>
---
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index d07c9f7..c544bb4 100644
--- a/drivers/block/nbd.c
+++ b/drivers/block/nbd.c
@@ -78,6 +78,8 @@ static const char *ioctl_cmd_to_ascii(int cmd)
 	case NBD_SET_SOCK: return "set-sock";
 	case NBD_SET_BLKSIZE: return "set-blksize";
 	case NBD_SET_SIZE: return "set-size";
+	case NBD_SET_TIMEOUT: return "set-timeout";
+	case NBD_SET_FLAGS: return "set-flags";
 	case NBD_DO_IT: return "do-it";
 	case NBD_CLEAR_SOCK: return "clear-sock";
 	case NBD_CLEAR_QUE: return "clear-que";
@@ -460,7 +462,7 @@ static void nbd_handle_req(struct nbd_device *nbd, struct request *req)
 	nbd_cmd(req) = NBD_CMD_READ;
 	if (rq_data_dir(req) == WRITE) {
 		nbd_cmd(req) = NBD_CMD_WRITE;
-		if (nbd->flags & NBD_READ_ONLY) {
+		if (nbd->flags & NBD_FLAG_READ_ONLY) {
 			dev_err(disk_to_dev(nbd->disk),
 				"Write on read-only\n");
 			goto error_out;
@@ -642,6 +644,10 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
 		nbd->xmit_timeout = arg * HZ;
 		return 0;
 
+	case NBD_SET_FLAGS:
+		nbd->flags = arg;
+		return 0;
+
 	case NBD_SET_SIZE_BLOCKS:
 		nbd->bytesize = ((u64) arg) * nbd->blksize;
 		bdev->bd_inode->i_size = nbd->bytesize;
diff --git a/include/linux/nbd.h b/include/linux/nbd.h
index d146ca1..bb349be 100644
--- a/include/linux/nbd.h
+++ b/include/linux/nbd.h
@@ -27,6 +27,7 @@
 #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)
 
 enum {
 	NBD_CMD_READ = 0,
@@ -34,6 +35,10 @@ enum {
 	NBD_CMD_DISC = 2
 };
 
+/* values for flags field */
+#define NBD_FLAG_HAS_FLAGS	(1 << 0)
+#define NBD_FLAG_READ_ONLY	(1 << 1)
+
 #define nbd_cmd(req) ((req)->cmd[0])
 
 /* userspace doesn't need the nbd_device structure */
@@ -42,10 +47,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 {

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

* [PATCH 2/2] add discard support to nbd
  2012-08-28 21:43 ` [PATCH 1/2] " Paul Clements
@ 2012-08-28 21:44   ` Paul Clements
  0 siblings, 0 replies; 3+ messages in thread
From: Paul Clements @ 2012-08-28 21:44 UTC (permalink / raw)
  To: kernel list; +Cc: andrew morton

[-- Attachment #1: Type: text/plain, Size: 1 bytes --]



[-- Attachment #2: nbd-trim-discard-support.diff --]
[-- Type: application/octet-stream, Size: 2873 bytes --]

Description: This patch adds discard support to nbd. When the nbd client
system receives a discard request, this will be passed along to the nbd
server system, where the nbd-server will respond by performing:
	fallocate(.. FALLOC_FL_PUNCH_HOLE ..)

To punch a hole in the backend storage, which is no longer needed.

Signed-off-by: Paul Clements <paul.clements@steeleye.com>
---
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c
index c544bb4..a014169 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";
 }
@@ -461,7 +462,11 @@ static void nbd_handle_req(struct nbd_device *nbd, struct request *req)
 
 	nbd_cmd(req) = NBD_CMD_READ;
 	if (rq_data_dir(req) == WRITE) {
-		nbd_cmd(req) = NBD_CMD_WRITE;
+		if ((req->cmd_flags & REQ_DISCARD)) {
+			WARN_ON(!(nbd->flags & NBD_FLAG_SEND_TRIM));
+			nbd_cmd(req) = NBD_CMD_TRIM;
+		} else
+			nbd_cmd(req) = NBD_CMD_WRITE;
 		if (nbd->flags & NBD_FLAG_READ_ONLY) {
 			dev_err(disk_to_dev(nbd->disk),
 				"Write on read-only\n");
@@ -667,6 +672,10 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
 
 		mutex_unlock(&nbd->tx_lock);
 
+		if (nbd->flags & NBD_FLAG_SEND_TRIM)
+			queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
+				nbd->disk->queue);
+
 		thread = kthread_create(nbd_thread, nbd, nbd->disk->disk_name);
 		if (IS_ERR(thread)) {
 			mutex_lock(&nbd->tx_lock);
@@ -684,6 +693,7 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd,
 		nbd->file = NULL;
 		nbd_clear_que(nbd);
 		dev_warn(disk_to_dev(nbd->disk), "queue cleared\n");
+		queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue);
 		if (file)
 			fput(file);
 		nbd->bytesize = 0;
@@ -802,6 +812,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 bb349be..3b49a63 100644
--- a/include/linux/nbd.h
+++ b/include/linux/nbd.h
@@ -32,12 +32,16 @@
 enum {
 	NBD_CMD_READ = 0,
 	NBD_CMD_WRITE = 1,
-	NBD_CMD_DISC = 2
+	NBD_CMD_DISC = 2,
+	/* there is a gap here to match userspace */
+	NBD_CMD_TRIM = 4
 };
 
 /* values for flags field */
 #define NBD_FLAG_HAS_FLAGS	(1 << 0)
 #define NBD_FLAG_READ_ONLY	(1 << 1)
+/* there is a gap here to match userspace */
+#define NBD_FLAG_SEND_TRIM	(1 << 5) /* send trim/discard */
 
 #define nbd_cmd(req) ((req)->cmd[0])
 

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

end of thread, other threads:[~2012-08-28 21:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-28 21:42 [PATCH 0/2] add discard support to nbd Paul Clements
2012-08-28 21:43 ` [PATCH 1/2] " Paul Clements
2012-08-28 21:44   ` [PATCH 2/2] " Paul Clements

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).