From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932263Ab1IHHpR (ORCPT ); Thu, 8 Sep 2011 03:45:17 -0400 Received: from mail-vw0-f43.google.com ([209.85.212.43]:50614 "EHLO mail-vw0-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932140Ab1IHHpO (ORCPT ); Thu, 8 Sep 2011 03:45:14 -0400 From: Paolo Bonzini To: linux-kernel@vger.kernel.org Cc: Paul Clements , Subject: [PATCH v2 2/2] nbd: map DISCARD requests to a new nbd request type Date: Thu, 8 Sep 2011 09:44:58 +0200 Message-Id: <1315467898-23984-3-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.6 In-Reply-To: <1315467898-23984-1-git-send-email-pbonzini@redhat.com> References: <1315467898-23984-1-git-send-email-pbonzini@redhat.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Cc: Reviewed-by: Paul Clements Signed-off-by: Paolo Bonzini --- 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