From patchwork Wed May 25 10:59:25 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 9135187 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id D5938607D7 for ; Wed, 25 May 2016 11:20:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C89EC28164 for ; Wed, 25 May 2016 11:20:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id BD817282D4; Wed, 25 May 2016 11:20:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 36DCF28164 for ; Wed, 25 May 2016 11:20:37 +0000 (UTC) Received: from localhost ([::1]:59049 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5Wrc-0002oS-7i for patchwork-qemu-devel@patchwork.kernel.org; Wed, 25 May 2016 07:20:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:52344) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5WXP-00019u-CB for qemu-devel@nongnu.org; Wed, 25 May 2016 06:59:46 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b5WXE-0002WU-RO for qemu-devel@nongnu.org; Wed, 25 May 2016 06:59:40 -0400 Received: from resqmta-po-05v.sys.comcast.net ([2001:558:fe16:19:96:114:154:164]:59989) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b5WXD-0002VO-Gh for qemu-devel@nongnu.org; Wed, 25 May 2016 06:59:32 -0400 Received: from resomta-po-13v.sys.comcast.net ([96.114.154.237]) by comcast with SMTP id 5WX1bGSHImXdb5WXCbFFjW; Wed, 25 May 2016 10:59:30 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=comcast.net; s=q20140121; t=1464173970; bh=cI46U+AwI6Ci9IaJRN4lIyzFt6Z4mGtoEYLquemxaeI=; h=Received:Received:From:To:Subject:Date:Message-Id; b=WWbHVkqFb/qy1RfrDJ3lepR+5JHx3yKxC71wV39uu1x0Nmh8PFPcl6+aXNWagfJ5z b4aXnExPgVn/sjG6rqzZ0xobe2WJ9RlksF93uT+pIvuN4x4p79BnkofTygVVHKW+KD qixYoWwRjGJSyTZIN9A/FNQBclW1ATrrkCuq0PecNpzcML3kXlTCC+1cj7ppcRvwJv tv1sA0a/oIgNgz4lH7cSjTGxCjX76MGh2zRDUJ+tC5RobMmmRGVSyNywJLWVjuJn2j TOR8jXjlznXynpKkcVN5NPs/SLQEDsN9cni+J45aFhps6LaD32rbNTRyOAcaq0NMQh xxzXPi1gh6pBQ== Received: from red.redhat.com ([24.10.254.122]) by resomta-po-13v.sys.comcast.net with comcast id yazT1s0062fD5rL01azWM9; Wed, 25 May 2016 10:59:30 +0000 From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 25 May 2016 04:59:25 -0600 Message-Id: <1464173965-9694-1-git-send-email-eblake@redhat.com> X-Mailer: git-send-email 2.5.5 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2001:558:fe16:19:96:114:154:164 Subject: [Qemu-devel] [PATCH v2] nbd: Don't trim unrequested bytes X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, pbonzini@redhat.com, qemu-stable@nongnu.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Similar to commit df7b97ff, we are mishandling clients that give an unaligned NBD_CMD_TRIM request, and potentially trimming bytes that occur before their request; which in turn can cause potential unintended data loss (unlikely in practice, since most clients are sane and issue aligned trim requests). However, while we fixed read and write by switching to the byte interfaces of blk_, we don't yet have a byte interface for discard. On the other hand, trim is advisory, so rounding the user's request to simply ignore the first and last unaligned sectors (or the entire request, if it is sub-sector in length) is just fine. CC: qemu-stable@nongnu.org Signed-off-by: Eric Blake --- v2: don't underflow request.len on sub-sector requests nbd/server.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/nbd/server.c b/nbd/server.c index fa862cd..b2cfeb9 100644 --- a/nbd/server.c +++ b/nbd/server.c @@ -1153,12 +1153,20 @@ static void nbd_trip(void *opaque) break; case NBD_CMD_TRIM: TRACE("Request type is TRIM"); - ret = blk_co_discard(exp->blk, (request.from + exp->dev_offset) - / BDRV_SECTOR_SIZE, - request.len / BDRV_SECTOR_SIZE); - if (ret < 0) { - LOG("discard failed"); - reply.error = -ret; + /* Ignore unaligned head or tail, until block layer adds byte + * interface */ + if (request.len >= BDRV_SECTOR_SIZE) { + request.len -= (request.from + request.len) % BDRV_SECTOR_SIZE; + ret = blk_co_discard(exp->blk, + DIV_ROUND_UP(request.from + exp->dev_offset, + BDRV_SECTOR_SIZE), + request.len / BDRV_SECTOR_SIZE); + if (ret < 0) { + LOG("discard failed"); + reply.error = -ret; + } + } else { + TRACE("trim request too small, ignoring"); } if (nbd_co_send_reply(req, &reply, 0) < 0) { goto out;