From patchwork Fri Jul 5 09:32:58 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stefano Garzarella X-Patchwork-Id: 11032281 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 9B6C3138D for ; Fri, 5 Jul 2019 09:34:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8663E28A70 for ; Fri, 5 Jul 2019 09:34:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7769628A7E; Fri, 5 Jul 2019 09:34:35 +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.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1F70A28A70 for ; Fri, 5 Jul 2019 09:34:35 +0000 (UTC) Received: from localhost ([::1]:50990 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hjKby-0003VT-GX for patchwork-qemu-devel@patchwork.kernel.org; Fri, 05 Jul 2019 05:34:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41453) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hjKb0-00021i-Sn for qemu-devel@nongnu.org; Fri, 05 Jul 2019 05:33:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hjKaz-0002tf-9v for qemu-devel@nongnu.org; Fri, 05 Jul 2019 05:33:34 -0400 Received: from mx1.redhat.com ([209.132.183.28]:39554) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hjKan-0002U5-Lv; Fri, 05 Jul 2019 05:33:22 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4371759451; Fri, 5 Jul 2019 09:33:10 +0000 (UTC) Received: from steredhat.redhat.com (ovpn-117-149.ams2.redhat.com [10.36.117.149]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2285B68C6B; Fri, 5 Jul 2019 09:32:58 +0000 (UTC) From: Stefano Garzarella To: qemu-devel@nongnu.org Date: Fri, 5 Jul 2019 11:32:58 +0200 Message-Id: <20190705093258.47856-1-sgarzare@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Fri, 05 Jul 2019 09:33:10 +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 v3] block/rbd: implement .bdrv_get_allocated_file_size callback X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Josh Durgin , qemu-block@nongnu.org, Jason Dillaman , Max Reitz , John Snow 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 --- v3: - return -ENOTSUP instead of -1 when fast-diff is not available [John, Jason] 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 59757b3120..b6bed683e5 100644 --- a/block/rbd.c +++ b/block/rbd.c @@ -1084,6 +1084,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 -ENOTSUP; + } + + /* + * 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, @@ -1291,6 +1344,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",