From patchwork Wed Oct 5 07:17:42 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 117752 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9C635B6F97 for ; Wed, 5 Oct 2011 18:19:19 +1100 (EST) Received: from localhost ([::1]:49856 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RBLl7-00024r-4C for incoming@patchwork.ozlabs.org; Wed, 05 Oct 2011 03:19:17 -0400 Received: from eggs.gnu.org ([140.186.70.92]:33226) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RBLju-0007WU-7R for qemu-devel@nongnu.org; Wed, 05 Oct 2011 03:18:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RBLjs-0006H7-CQ for qemu-devel@nongnu.org; Wed, 05 Oct 2011 03:18:02 -0400 Received: from mail-ww0-f53.google.com ([74.125.82.53]:42322) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RBLjs-0006EG-1I for qemu-devel@nongnu.org; Wed, 05 Oct 2011 03:18:00 -0400 Received: by mail-ww0-f53.google.com with SMTP id 14so1759680wwg.10 for ; Wed, 05 Oct 2011 00:17:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:from:to:subject:date:message-id:x-mailer:in-reply-to :references; bh=Yj9dBxSHrp84/fkSB0Mhy6EwIIzRe3Tuo1IXn6Oey3o=; b=w2OeppjHXkHsf6WKA/URTINXNOnVK6m+UTBPKnzAmoUBlHh5tPHbivYaUn/78Cmly3 E3FArlt/Xtwf0+WJguvgsOj7BYF3MpV4IJ24BaSYacKYqSLXDijmSqXndj9GNdwyku2p AasiKOsV1mi+GYy9VEMUpp/pq2dkEl7n11fQY= Received: by 10.216.230.17 with SMTP id i17mr5992830weq.88.1317799079504; Wed, 05 Oct 2011 00:17:59 -0700 (PDT) Received: from localhost.localdomain (93-34-218-143.ip51.fastwebnet.it. [93.34.218.143]) by mx.google.com with ESMTPS id p2sm1192306wbo.17.2011.10.05.00.17.58 (version=TLSv1/SSLv3 cipher=OTHER); Wed, 05 Oct 2011 00:17:59 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Wed, 5 Oct 2011 09:17:42 +0200 Message-Id: <1317799065-29668-13-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.6 In-Reply-To: <1317799065-29668-1-git-send-email-pbonzini@redhat.com> References: <1317799065-29668-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 74.125.82.53 Subject: [Qemu-devel] [PATCH v3 12/15] nbd: add support for NBD_CMD_TRIM X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Map it to bdrv_discard. The server can also expose NBD_FLAG_SEND_TRIM. Signed-off-by: Paolo Bonzini --- block/nbd.c | 31 +++++++++++++++++++++++++++++++ nbd.c | 13 ++++++++++++- 2 files changed, 43 insertions(+), 1 deletions(-) diff --git a/block/nbd.c b/block/nbd.c index 23f83d4..35c15c8 100644 --- a/block/nbd.c +++ b/block/nbd.c @@ -276,6 +276,36 @@ static int nbd_flush(BlockDriverState *bs) return 0; } +static int nbd_discard(BlockDriverState *bs, int64_t sector_num, + int nb_sectors) +{ + BDRVNBDState *s = bs->opaque; + struct nbd_request request; + struct nbd_reply reply; + + if (!(s->nbdflags & NBD_FLAG_SEND_TRIM)) { + return 0; + } + request.type = NBD_CMD_TRIM; + request.handle = (uint64_t)(intptr_t)bs; + request.from = sector_num * 512;; + request.len = nb_sectors * 512; + + if (nbd_send_request(s->sock, &request) == -1) + return -errno; + + if (nbd_receive_reply(s->sock, &reply) == -1) + return -errno; + + if (reply.error !=0) + return -reply.error; + + if (reply.handle != request.handle) + return -EIO; + + return 0; +} + static void nbd_close(BlockDriverState *bs) { BDRVNBDState *s = bs->opaque; @@ -300,6 +330,7 @@ static BlockDriver bdrv_nbd = { .bdrv_write = nbd_write, .bdrv_close = nbd_close, .bdrv_flush = nbd_flush, + .bdrv_discard = nbd_discard, .bdrv_getlength = nbd_getlength, .protocol_name = "nbd", }; diff --git a/nbd.c b/nbd.c index 42ee2b5..418b020 100644 --- a/nbd.c +++ b/nbd.c @@ -195,7 +195,7 @@ int nbd_negotiate(int csock, off_t size, uint32_t flags) cpu_to_be64w((uint64_t*)(buf + 8), 0x00420281861253LL); cpu_to_be64w((uint64_t*)(buf + 16), size); cpu_to_be32w((uint32_t*)(buf + 24), - flags | NBD_FLAG_HAS_FLAGS | + flags | NBD_FLAG_HAS_FLAGS | NBD_FLAG_SEND_TRIM | NBD_FLAG_SEND_FLUSH | NBD_FLAG_SEND_FUA); memset(buf + 28, 0, 124); @@ -708,6 +708,17 @@ int nbd_trip(BlockDriverState *bs, int csock, off_t size, uint64_t dev_offset, if (nbd_send_reply(csock, &reply) == -1) return -1; break; + case NBD_CMD_TRIM: + TRACE("Request type is TRIM"); + ret = bdrv_discard(bs, (request.from + dev_offset) / 512, + request.len / 512); + if (ret < 0) { + LOG("discard failed"); + reply.error = -ret; + } + if (nbd_send_reply(csock, &reply) == -1) + return -1; + break; default: LOG("invalid request type (%u) received", request.type); errno = EINVAL;