diff options
author | Paul Clements <paul.clements@steeleye.com> | 2012-10-04 20:16:18 -0400 |
---|---|---|
committer | Linus Torvalds <torvalds@linux-foundation.org> | 2012-10-05 14:05:24 -0400 |
commit | a336d29870f8a1f8e5f10d9f1aa95531c4edeabe (patch) | |
tree | 9957b7a2dbdc5047fe1f2be7907f90f5bdb6fbf0 | |
parent | 2f012508880f8037590372c24ca6e8b6af8fffb6 (diff) |
nbd: handle discard requests
Add discard support to nbd. If the nbd-server supports discard, it will
send NBD_FLAG_SEND_TRIM to the client. The client will then set the flag
in the kernel via NBD_SET_FLAGS, which tells the kernel to enable discards
for the device (QUEUE_FLAG_DISCARD).
If discard support is enabled, then when the nbd client system receives a
discard request, this will be passed along to the nbd-server. When the
discard request is received by the nbd-server, it will perform:
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>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
-rw-r--r-- | drivers/block/nbd.c | 15 | ||||
-rw-r--r-- | include/linux/nbd.h | 6 |
2 files changed, 19 insertions, 2 deletions
diff --git a/drivers/block/nbd.c b/drivers/block/nbd.c index 68fe7514413d..043ddcca4abf 100644 --- a/drivers/block/nbd.c +++ b/drivers/block/nbd.c | |||
@@ -98,6 +98,7 @@ static const char *nbdcmd_to_ascii(int cmd) | |||
98 | case NBD_CMD_READ: return "read"; | 98 | case NBD_CMD_READ: return "read"; |
99 | case NBD_CMD_WRITE: return "write"; | 99 | case NBD_CMD_WRITE: return "write"; |
100 | case NBD_CMD_DISC: return "disconnect"; | 100 | case NBD_CMD_DISC: return "disconnect"; |
101 | case NBD_CMD_TRIM: return "trim/discard"; | ||
101 | } | 102 | } |
102 | return "invalid"; | 103 | return "invalid"; |
103 | } | 104 | } |
@@ -469,7 +470,11 @@ static void nbd_handle_req(struct nbd_device *nbd, struct request *req) | |||
469 | 470 | ||
470 | nbd_cmd(req) = NBD_CMD_READ; | 471 | nbd_cmd(req) = NBD_CMD_READ; |
471 | if (rq_data_dir(req) == WRITE) { | 472 | if (rq_data_dir(req) == WRITE) { |
472 | nbd_cmd(req) = NBD_CMD_WRITE; | 473 | if ((req->cmd_flags & REQ_DISCARD)) { |
474 | WARN_ON(!(nbd->flags & NBD_FLAG_SEND_TRIM)); | ||
475 | nbd_cmd(req) = NBD_CMD_TRIM; | ||
476 | } else | ||
477 | nbd_cmd(req) = NBD_CMD_WRITE; | ||
473 | if (nbd->flags & NBD_FLAG_READ_ONLY) { | 478 | if (nbd->flags & NBD_FLAG_READ_ONLY) { |
474 | dev_err(disk_to_dev(nbd->disk), | 479 | dev_err(disk_to_dev(nbd->disk), |
475 | "Write on read-only\n"); | 480 | "Write on read-only\n"); |
@@ -676,6 +681,10 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, | |||
676 | 681 | ||
677 | mutex_unlock(&nbd->tx_lock); | 682 | mutex_unlock(&nbd->tx_lock); |
678 | 683 | ||
684 | if (nbd->flags & NBD_FLAG_SEND_TRIM) | ||
685 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, | ||
686 | nbd->disk->queue); | ||
687 | |||
679 | thread = kthread_create(nbd_thread, nbd, nbd->disk->disk_name); | 688 | thread = kthread_create(nbd_thread, nbd, nbd->disk->disk_name); |
680 | if (IS_ERR(thread)) { | 689 | if (IS_ERR(thread)) { |
681 | mutex_lock(&nbd->tx_lock); | 690 | mutex_lock(&nbd->tx_lock); |
@@ -693,6 +702,7 @@ static int __nbd_ioctl(struct block_device *bdev, struct nbd_device *nbd, | |||
693 | nbd->file = NULL; | 702 | nbd->file = NULL; |
694 | nbd_clear_que(nbd); | 703 | nbd_clear_que(nbd); |
695 | dev_warn(disk_to_dev(nbd->disk), "queue cleared\n"); | 704 | dev_warn(disk_to_dev(nbd->disk), "queue cleared\n"); |
705 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, nbd->disk->queue); | ||
696 | if (file) | 706 | if (file) |
697 | fput(file); | 707 | fput(file); |
698 | nbd->bytesize = 0; | 708 | nbd->bytesize = 0; |
@@ -811,6 +821,9 @@ static int __init nbd_init(void) | |||
811 | * Tell the block layer that we are not a rotational device | 821 | * Tell the block layer that we are not a rotational device |
812 | */ | 822 | */ |
813 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue); | 823 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, disk->queue); |
824 | disk->queue->limits.discard_granularity = 512; | ||
825 | disk->queue->limits.max_discard_sectors = UINT_MAX; | ||
826 | disk->queue->limits.discard_zeroes_data = 0; | ||
814 | } | 827 | } |
815 | 828 | ||
816 | if (register_blkdev(NBD_MAJOR, "nbd")) { | 829 | if (register_blkdev(NBD_MAJOR, "nbd")) { |
diff --git a/include/linux/nbd.h b/include/linux/nbd.h index 4b8ed5a1cfb0..5c86e2b33e2d 100644 --- a/include/linux/nbd.h +++ b/include/linux/nbd.h | |||
@@ -32,12 +32,16 @@ | |||
32 | enum { | 32 | enum { |
33 | NBD_CMD_READ = 0, | 33 | NBD_CMD_READ = 0, |
34 | NBD_CMD_WRITE = 1, | 34 | NBD_CMD_WRITE = 1, |
35 | NBD_CMD_DISC = 2 | 35 | NBD_CMD_DISC = 2, |
36 | /* there is a gap here to match userspace */ | ||
37 | NBD_CMD_TRIM = 4 | ||
36 | }; | 38 | }; |
37 | 39 | ||
38 | /* values for flags field */ | 40 | /* values for flags field */ |
39 | #define NBD_FLAG_HAS_FLAGS (1 << 0) /* nbd-server supports flags */ | 41 | #define NBD_FLAG_HAS_FLAGS (1 << 0) /* nbd-server supports flags */ |
40 | #define NBD_FLAG_READ_ONLY (1 << 1) /* device is read-only */ | 42 | #define NBD_FLAG_READ_ONLY (1 << 1) /* device is read-only */ |
43 | /* there is a gap here to match userspace */ | ||
44 | #define NBD_FLAG_SEND_TRIM (1 << 5) /* send trim/discard */ | ||
41 | 45 | ||
42 | #define nbd_cmd(req) ((req)->cmd[0]) | 46 | #define nbd_cmd(req) ((req)->cmd[0]) |
43 | 47 | ||