From patchwork Mon Nov 26 03:57:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: zhenwei pi X-Patchwork-Id: 10697535 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9540E15A8 for ; Mon, 26 Nov 2018 03:58:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 77A142963C for ; Mon, 26 Nov 2018 03:58:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 54F4F29651; Mon, 26 Nov 2018 03:58:56 +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=-7.7 required=2.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI 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 DA48D2963C for ; Mon, 26 Nov 2018 03:58:54 +0000 (UTC) Received: from localhost ([::1]:34063 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gR82v-0008BW-8t for patchwork-qemu-devel@patchwork.kernel.org; Sun, 25 Nov 2018 22:58:53 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54801) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gR82E-0007sZ-5h for qemu-devel@nongnu.org; Sun, 25 Nov 2018 22:58:10 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gR829-0003OJ-DW for qemu-devel@nongnu.org; Sun, 25 Nov 2018 22:58:10 -0500 Received: from mail-pl1-x644.google.com ([2607:f8b0:4864:20::644]:43927) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1gR829-0003NG-4l for qemu-devel@nongnu.org; Sun, 25 Nov 2018 22:58:05 -0500 Received: by mail-pl1-x644.google.com with SMTP id gn14so13196949plb.10 for ; Sun, 25 Nov 2018 19:58:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=LSqAYm3xy7e9HPD8V5YZ05O3Qxj0CwEVPOLtScNxvPU=; b=ebXbpmXTKSTExT0Iv26B4HO0pbjAEcCRuB3H7xUwNc15Ogj/BY0MjS3nfBojix/YIq EoOptIGKZuk4Vrbka1UIL4Epk3EJy7Sd14+3Cy6k5Ko4aZc2XnKAAnqLsLpRQy48SAOW F1J84aWA1q9GytEMO/4+h/3lG2loP5rZPw6z3oH4UbqBfiP0yfzmx+CvN3kSvLiw/k5M arOoslggxSlZHstFcQPQfHUBQUOrI7Idcvd1DfGMbLHTtrSUZeg8NBcSBz00SukcMyhP 5aj1XsehZ9HfGgRedJ86lx+BTQvkE7grGz5FDDXeT6YeljYjWtbtVz4LNUlpa0WaZvJf gMFg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=LSqAYm3xy7e9HPD8V5YZ05O3Qxj0CwEVPOLtScNxvPU=; b=onu7GgaTqXrQmpo9+eCKoIMH8hF559wEZItRyajt0RkEfo8NxxFmrTYJbOXPs9ACH3 XJq/EHIUKGX+DZQGuBajrK91JXeboNYwJN6UDcNI0VM3+LZvA6V99Pm2YA645ClWuzbn raB6V+q03du1qjC0JCFtUczOSdRQ4C+uG4mGwXdnXCOQOHcfWA/DKdCq+zGLfASyOLfu hUrYrwCCTB1+RekSfo8S+x8arWc9DQbWhNri+MDwbmNtt5ajEEclxoS7FIdCrO/w4QvE tv3xld+uNHT/G02SJBHyPoYYDE2953h1hqOw02H41tMj+ryZgLNCld5L7dyLtyjopEt3 ZpuQ== X-Gm-Message-State: AA+aEWZWNJ4y32TQeM1EzguxpXB91YAfq2HtdlBYoFXNhrGX6cP6Oex0 wjkKQMjGU5Do4p2haMiGAChIIQ== X-Google-Smtp-Source: AFSGD/V+so8infUP0bcrvxCJUTcWFVSzkSMGgDVpOm4wJ+UMf8FSm4u2TBau6T+181IPRPfHov/XAQ== X-Received: by 2002:a17:902:8541:: with SMTP id d1mr1679511plo.205.1543204683617; Sun, 25 Nov 2018 19:58:03 -0800 (PST) Received: from always-ThinkPad-T480.bytedance.net ([61.120.150.66]) by smtp.gmail.com with ESMTPSA id j6sm38867766pfg.126.2018.11.25.19.58.01 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 25 Nov 2018 19:58:02 -0800 (PST) From: zhenwei pi To: kwolf@redhat.com, mreitz@redhat.com Date: Mon, 26 Nov 2018 11:57:58 +0800 Message-Id: <1543204678-26914-1-git-send-email-pizhenwei@bytedance.com> X-Mailer: git-send-email 2.7.4 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::644 Subject: [Qemu-devel] [PATCH] file-posix: Check effective size in truncate operation 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: qemu-block@nongnu.org, pizhenwei@bytedance.com, qemu-devel@nongnu.org Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Function raw_co_truncate does not check effective size for BLK device file, and QEMU may notify guest without any size changing. Two cases can be reproduced easily by qmp command: CASE 1: 1, create a logical volume(12M) by LVM, and guest uses this volume as "vdb" 2, run qmp command : virsh qemu-monitor-command INSTANCE '{"execute": "block_resize", "arguments":{"device":"drive-virtio-disk1","size":12582912}}' The effective size(12M) is equal to the argument(12M) and the real device file size(12M). QEMU should ignore this command and has no need to notify guest. CASE 2: 1, create a logical volume(12M) by LVM, and guest uses this volume as "vdb" 2, resize LV to 16M by lvresize command 3, run qmp command : virsh qemu-monitor-command INSTANCE '{"execute": "block_resize", "arguments":{"device":"drive-virtio-disk1","size":10485760}}' The device file size actually grew, but the argument(10M) is less than the effective size(12M). This command should fail, but QEMU still report success. Signed-off-by: zhenwei pi --- block/file-posix.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/block/file-posix.c b/block/file-posix.c index 07bbdab953..951d910b0b 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -1991,6 +1991,7 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, BDRVRawState *s = bs->opaque; struct stat st; int ret; + int64_t sectors; if (fstat(s->fd, &st)) { ret = -errno; @@ -2013,6 +2014,20 @@ static int coroutine_fn raw_co_truncate(BlockDriverState *bs, int64_t offset, error_setg(errp, "Cannot grow device files"); return -EINVAL; } + + sectors = raw_getlength(bs) >> BDRV_SECTOR_BITS; + if (sectors > bs->total_sectors) { + /* device size actually grew */ + if (offset <= bs->total_sectors * BDRV_SECTOR_SIZE) { + error_setg(errp, "The effective size of this device is " + "greater than or equal to the argument"); + return -EINVAL; + } + } else if (sectors == bs->total_sectors) { + /* device size actually not changed */ + error_setg(errp, "Detect device file size not changing"); + return -EINVAL; + } } else { error_setg(errp, "Resizing this file is not supported"); return -ENOTSUP;