From nobody Mon Apr 29 08:15:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=bytedance.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1543204760747321.96776175579066; Sun, 25 Nov 2018 19:59:20 -0800 (PST) Received: from localhost ([::1]:34063 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gR83B-0008BW-CK for importer@patchew.org; Sun, 25 Nov 2018 22:59:09 -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) 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) 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) 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 f= ile 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 succes= s. 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(BlockDriverSt= ate *bs, int64_t offset, BDRVRawState *s =3D bs->opaque; struct stat st; int ret; + int64_t sectors; =20 if (fstat(s->fd, &st)) { ret =3D -errno; @@ -2013,6 +2014,20 @@ static int coroutine_fn raw_co_truncate(BlockDriverS= tate *bs, int64_t offset, error_setg(errp, "Cannot grow device files"); return -EINVAL; } + + sectors =3D raw_getlength(bs) >> BDRV_SECTOR_BITS; + if (sectors > bs->total_sectors) { + /* device size actually grew */ + if (offset <=3D 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 =3D=3D 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; --=20 2.11.0