From nobody Wed Oct 29 09:04:55 2025 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=gmail.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1525506670523295.7332349511429; Sat, 5 May 2018 00:51:10 -0700 (PDT) Received: from localhost ([::1]:37757 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fEryA-0005Ay-2V for importer@patchew.org; Sat, 05 May 2018 03:51:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57516) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fErxH-0004kz-R8 for qemu-devel@nongnu.org; Sat, 05 May 2018 03:50:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fErxD-00036S-Tm for qemu-devel@nongnu.org; Sat, 05 May 2018 03:50:07 -0400 Received: from mail-pf0-x242.google.com ([2607:f8b0:400e:c00::242]:45194) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fErxD-00036H-NI; Sat, 05 May 2018 03:50:03 -0400 Received: by mail-pf0-x242.google.com with SMTP id c10so19117349pfi.12; Sat, 05 May 2018 00:50:03 -0700 (PDT) Received: from VM_88_33_centos.localdomain ([119.28.139.217]) by smtp.gmail.com with ESMTPSA id e87sm46049087pfl.65.2018.05.05.00.50.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 05 May 2018 00:50:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=/IAcpV2cArH90Xs1l/0SXXStG3NJZTHAngR4D+u1wqE=; b=op2gvSX/sssrloXktVUZzI/PVJixz6qu2EXrMCYOGSYhXptLKL8CG7qj3Zjxswlx/+ spO53iT0loX73HIa4SVYYbJiyI+pR3cna64kUJPhVABbROmqgrRX9ehaovNnPkz3oXji Yv7WAAWTHvurXrAOxYA7sv6BE4MLcFtNzFvewvry6vXG39IwXyZjqDH9aiXIoHGtkHd/ UAfXBSKM9Vxr9udR/E+EUqGycTIwGi0CzoPo08VioQEqhi+dPn/EbwNQWywnn5xr6h1Z 9hedYdX9W3GGVBL2N3GntV2Pg4irz7r03F1Sxz8dQdCbg4zzvcx60S8l0eKZSwCb51Et YoHg== 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=/IAcpV2cArH90Xs1l/0SXXStG3NJZTHAngR4D+u1wqE=; b=FvdzJslJ43Dj70k4ANpd4swc/1qJ7jUrRiq73qUL2L23NPyKXPRhAVV1B6geeL3N5d D/6m95bXTSAyEfWRMAwYVzqGB2xq5196BsKkvydrIGfU+JAd9NGz9+8fMuFVmnPsUZW0 HLBm54a1D19lOLmiogfjMxf3QILkhFtbGmGNywWp+QvV5v9CqXUSx1i9dwbFHbECAjHN HZt6YG3M75gIgn6uxxN0TshZksb3JuvW+q0SzJ6RvzBwnlE3jExLQuCh4Qt30kqH7Yob HzJ+JlZp4W+6u3BGzgfbqNQG4uBh3cGIl7y9Kzxf3w9WDpU8ISPNG2ieL/U/jW04fDrr zZfA== X-Gm-Message-State: ALQs6tCpaMmb77kCH/s9yOql0LywthKlPN3iv4eGu9lDEePoS/6mHewI zCC3kf+Njwf0Y3twYgz8l64= X-Google-Smtp-Source: AB8JxZo1HBYQSfJDVDdour2LZtlMJrnBIa7FVO+Q6A79Y6sUOxkAxdnOHo/MZnmYEj/Aa68XZii0Aw== X-Received: by 10.98.245.91 with SMTP id n88mr22128817pfh.208.1525506602639; Sat, 05 May 2018 00:50:02 -0700 (PDT) From: Ivan Ren X-Google-Original-From: Ivan Ren To: kwolf@redhat.com, mreitz@redhat.com Date: Sat, 5 May 2018 15:49:37 +0800 Message-Id: <1525506577-1946-1-git-send-email-ivanren@tencent.com> X-Mailer: git-send-email 1.8.3.1 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::242 Subject: [Qemu-devel] [PATCH v2] qemu-img: return allocated size for block device with qcow2 format 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-devel@nongnu.org, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" qemu-img info with a block device which has a qcow2 format always return 0 for disk size, and this can not reflect the qcow2 size and the used space of the block device. This patch return the allocated size of qcow2 as the disk size. Signed-off-by: Ivan Ren --- block/qcow2.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) This version differs from the previous: (1).Accumulate all non-0 refcount cluster as the allocated size instead the highest offset. (2).Simplify the implementation by just check the refcount info. diff --git a/block/qcow2.c b/block/qcow2.c index ef68772..e64469a 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -4493,6 +4493,59 @@ static QemuOptsList qcow2_create_opts =3D { } }; =20 +/* Get allocated space of qcow2, aligned to cluster_size */ +static int64_t qcow2_get_allocated_size(BlockDriverState *bs) +{ + int64_t allocated_cluster_count =3D 0; + uint64_t refcount; + int64_t i, j, begin_index, end_index; + + BDRVQcow2State *s =3D bs->opaque; + + /* Traverse refcount table */ + for (i =3D 0; i < s->refcount_table_size; i++) { + if (s->refcount_table[i] & REFT_OFFSET_MASK) { + begin_index =3D i * s->refcount_block_size; + end_index =3D begin_index + s->refcount_block_size; + for (j =3D begin_index; j < end_index; j++) { + if (qcow2_get_refcount(bs, j, &refcount) < 0) { + continue; + } + if (refcount > 0) { + allocated_cluster_count++; + } + } + } + + } + + return allocated_cluster_count * s->cluster_size; +} + +static int64_t qcow2_get_allocated_file_size(BlockDriverState *bs) +{ + int64_t ret =3D 0; + + /* Get through bs->file first */ + if (bs->file) { + ret =3D bdrv_get_allocated_file_size(bs->file->bs); + } + + /* + * If ret < 0, some error may happen to the underlying media, return t= he + * error directly. + * If ret =3D=3D 0, means length get from bs->file is 0, which is impo= ssible + * for a normal file because of the qcow2 header. In this case, the + * underlying media may be a block device or anything else that can't + * return a suitable size. So we get the allocated size of qcow2 inste= ad. + */ + if (!ret) { + ret =3D qcow2_get_allocated_size(bs); + } + + return ret; +} + BlockDriver bdrv_qcow2 =3D { .format_name =3D "qcow2", .instance_size =3D sizeof(BDRVQcow2State), @@ -4516,6 +4569,7 @@ BlockDriver bdrv_qcow2 =3D { .bdrv_co_pwrite_zeroes =3D qcow2_co_pwrite_zeroes, .bdrv_co_pdiscard =3D qcow2_co_pdiscard, .bdrv_truncate =3D qcow2_truncate, + .bdrv_get_allocated_file_size =3D qcow2_get_allocated_file_size, .bdrv_co_pwritev_compressed =3D qcow2_co_pwritev_compressed, .bdrv_make_empty =3D qcow2_make_empty, =20 --=20 1.8.3.1