2012-08-29 13:14:57

by Paul Clements

[permalink] [raw]
Subject: [PATCH 2/2] [RESEND] add discard support to nbd

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 <[email protected]>
---
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])


2012-08-30 22:03:50

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH 2/2] [RESEND] add discard support to nbd

On Wed, 29 Aug 2012 08:41:02 -0400
[email protected] wrote:

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

What happens if the user is running an older server?

I is it possible that because the old server didn't set
NBD_FLAG_SEND_TRIM, the user's screen gets filled with WARN_ONs?

Anyway, please make sure this combination was tested!

2012-08-31 15:24:34

by Paul Clements

[permalink] [raw]
Subject: Re: [PATCH 2/2] [RESEND] add discard support to nbd

On Thu, Aug 30, 2012 at 6:03 PM, Andrew Morton
<[email protected]> wrote:
> On Wed, 29 Aug 2012 08:41:02 -0400
> [email protected] wrote:
>
>> 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.
>>
>
> What happens if the user is running an older server?

In that case, the older server will not have set NBD_SEND_FLAG_TRIM,
so QUEUE_FLAG_DISCARD doesn't get set, so no discards will be sent to
the device.

> I is it possible that because the old server didn't set
> NBD_FLAG_SEND_TRIM, the user's screen gets filled with WARN_ONs?

No.

> Anyway, please make sure this combination was tested!

It was.

Thanks for the feedback.

I will resend v2 of the patches shortly.

--
Paul