From patchwork Fri May 10 15:33:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Garzarella X-Patchwork-Id: 10939097 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 019991390 for ; Fri, 10 May 2019 15:52:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E681628619 for ; Fri, 10 May 2019 15:52:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id DAC0B28BA8; Fri, 10 May 2019 15:52:47 +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=-5.2 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 88C3128619 for ; Fri, 10 May 2019 15:52:47 +0000 (UTC) Received: from localhost ([127.0.0.1]:45477 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hP7pG-0003qn-RR for patchwork-qemu-devel@patchwork.kernel.org; Fri, 10 May 2019 11:52:46 -0400 Received: from eggs.gnu.org ([209.51.188.92]:59559) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hP7X3-0004G1-Up for qemu-devel@nongnu.org; Fri, 10 May 2019 11:33:58 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hP7X2-0002vy-OA for qemu-devel@nongnu.org; Fri, 10 May 2019 11:33:57 -0400 Received: from mx1.redhat.com ([209.132.183.28]:48320) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hP7Ww-0002qQ-MT; Fri, 10 May 2019 11:33:50 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 88639308FBAF; Fri, 10 May 2019 15:33:49 +0000 (UTC) Received: from steredhat.redhat.com (ovpn-117-202.ams2.redhat.com [10.36.117.202]) by smtp.corp.redhat.com (Postfix) with ESMTP id 829E85D9D4; Fri, 10 May 2019 15:33:47 +0000 (UTC) From: Stefano Garzarella To: qemu-devel@nongnu.org Date: Fri, 10 May 2019 17:33:46 +0200 Message-Id: <20190510153346.238366-1-sgarzare@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Fri, 10 May 2019 15:33:49 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2] block/rbd: implement .bdrv_get_allocated_file_size callback 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: Kevin Wolf , Josh Durgin , Jason Dillaman , qemu-block@nongnu.org, Max Reitz Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP This patch allows 'qemu-img info' to show the 'disk size' for the RBD images that have the fast-diff feature enabled. If this feature is enabled, we use the rbd_diff_iterate2() API to calculate the allocated size for the image. Signed-off-by: Stefano Garzarella --- v2: - calculate the actual usage only if the fast-diff feature is enabled [Jason] --- block/rbd.c | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/block/rbd.c b/block/rbd.c index 0c549c9935..f1bc76ab80 100644 --- a/block/rbd.c +++ b/block/rbd.c @@ -1046,6 +1046,59 @@ static int64_t qemu_rbd_getlength(BlockDriverState *bs) return info.size; } +static int rbd_allocated_size_cb(uint64_t offset, size_t len, int exists, + void *arg) +{ + int64_t *alloc_size = (int64_t *) arg; + + if (exists) { + (*alloc_size) += len; + } + + return 0; +} + +static int64_t qemu_rbd_get_allocated_file_size(BlockDriverState *bs) +{ + BDRVRBDState *s = bs->opaque; + uint64_t flags, features; + int64_t alloc_size = 0; + int r; + + r = rbd_get_flags(s->image, &flags); + if (r < 0) { + return r; + } + + r = rbd_get_features(s->image, &features); + if (r < 0) { + return r; + } + + /* + * We use rbd_diff_iterate2() only if the RBD image have fast-diff + * feature enabled. If it is disabled, rbd_diff_iterate2() could be + * very slow on a big image. + */ + if (!(features & RBD_FEATURE_FAST_DIFF) || + (flags & RBD_FLAG_FAST_DIFF_INVALID)) { + return -1; + } + + /* + * rbd_diff_iterate2(), if the source snapshot name is NULL, invokes + * the callback on all allocated regions of the image. + */ + r = rbd_diff_iterate2(s->image, NULL, 0, + bs->total_sectors * BDRV_SECTOR_SIZE, 0, 1, + &rbd_allocated_size_cb, &alloc_size); + if (r < 0) { + return r; + } + + return alloc_size; +} + static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs, int64_t offset, PreallocMode prealloc, @@ -1254,6 +1307,7 @@ static BlockDriver bdrv_rbd = { .bdrv_get_info = qemu_rbd_getinfo, .create_opts = &qemu_rbd_create_opts, .bdrv_getlength = qemu_rbd_getlength, + .bdrv_get_allocated_file_size = qemu_rbd_get_allocated_file_size, .bdrv_co_truncate = qemu_rbd_co_truncate, .protocol_name = "rbd",