From patchwork Thu Apr 27 08:06:34 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ashijeet Acharya X-Patchwork-Id: 9702481 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 788F0602CC for ; Thu, 27 Apr 2017 08:07:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6D23620952 for ; Thu, 27 Apr 2017 08:07:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 604752654B; Thu, 27 Apr 2017 08:07:31 +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=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID 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 0BE3E20952 for ; Thu, 27 Apr 2017 08:07:31 +0000 (UTC) Received: from localhost ([::1]:59286 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d3eSY-0003So-5w for patchwork-qemu-devel@patchwork.kernel.org; Thu, 27 Apr 2017 04:07:30 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56470) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d3eRT-0003Oa-8W for qemu-devel@nongnu.org; Thu, 27 Apr 2017 04:06:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d3eRS-0008On-6t for qemu-devel@nongnu.org; Thu, 27 Apr 2017 04:06:23 -0400 Received: from mail-pf0-x241.google.com ([2607:f8b0:400e:c00::241]:35614) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d3eRS-0008Nt-0G; Thu, 27 Apr 2017 04:06:22 -0400 Received: by mail-pf0-x241.google.com with SMTP id a188so7473429pfa.2; Thu, 27 Apr 2017 01:06:21 -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:in-reply-to:references; bh=VpBxeQqhMPqGtrAXYs1LE5RxBfl20UsTTF4j9evfuAY=; b=fXrcF6WDjlv3fCktR0mJNH/r5YstBjsTqU2a5udDHw1fIHBcS3RwPtyQD5ONKlZW8S ak/5Rh40veUXqf6mXk5z2PrSsxNVBF3ILwcNOIE6P2zmm9rAQ2ALONesftToAIGny1cT pp0cXDEqv2nNhpVzBQu6yL2AlOwYuenUEWFvZKx3HCbju7JlxsliH7zhJ5YyImQOOpUw 8sMGHrhB0m9Ss2fhTCXIHMyt1aY09CGvkpkMAiKeK9MZDe2ZyGxm8ioeq8AL/zYKg0y+ K67x7JnpWK/1RV3PuVbtDAjQ9/y4beH6i8BY7qSqbwbo04Nwh9if9zriscVU+GgQV6u+ u1gA== 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:in-reply-to :references; bh=VpBxeQqhMPqGtrAXYs1LE5RxBfl20UsTTF4j9evfuAY=; b=V9eudIaULp6JYNsyDC+89YJbZ82KbBtVD3N04Qwz2FgFnf0Qr/rsnxwAgPGvrgwti9 RhbWFAWe4fSjryLiCnDNzpQiJo+SPvtw6J0AqHwhqrShwtsuefZW5eVqmQDcVjkU3U4k /WpRUGgrulRHR1hPYXrDjpj2yCLFTVmADDPwYfjgMh9JPn3P0EJ0w+Vt6ar5he/HNGDw XHViS2UwmxB83HfUQKHmuIqCuLgsWk2yyMdy7UfoY9w79OeIz7La0KzKPxxaeCxsW4Zl Rc7Kce3MR/K1yOeAhpFchY4AGRe1ky4UdUjK80Ziz4gfHlitT/cKsgyFi720qbHDoT22 CTJg== X-Gm-Message-State: AN3rC/5+EEuqbTr8hGGfK/go7S6QLtB3YrjqNwWPbVAB5R6IGa+8/zFl Q+wv+vZs/lU38g== X-Received: by 10.99.125.18 with SMTP id y18mr4444850pgc.32.1493280381107; Thu, 27 Apr 2017 01:06:21 -0700 (PDT) Received: from linux.local ([157.51.104.251]) by smtp.gmail.com with ESMTPSA id 12sm3295277pgb.35.2017.04.27.01.06.17 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 27 Apr 2017 01:06:20 -0700 (PDT) From: Ashijeet Acharya To: stefanha@gmail.com Date: Thu, 27 Apr 2017 13:36:34 +0530 Message-Id: <1493280397-9622-5-git-send-email-ashijeetacharya@gmail.com> X-Mailer: git-send-email 2.6.2 In-Reply-To: <1493280397-9622-1-git-send-email-ashijeetacharya@gmail.com> References: <1493280397-9622-1-git-send-email-ashijeetacharya@gmail.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400e:c00::241 Subject: [Qemu-devel] [PATCH v2 4/7] dmg: Handle zlib compressed chunks 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: kwolf@redhat.com, famz@redhat.com, qemu-block@nongnu.org, qemu-devel@nongnu.org, mreitz@redhat.com, peter@lekensteyn.nl, Ashijeet Acharya , jsnow@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Set the output buffer size to be equal to the size of number of sectors stored in @sectors_read. Start inflating to a max output buffer size of 2MB and cache our access point to aid random access later if required. Signed-off-by: Ashijeet Acharya --- block/dmg.c | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/block/dmg.c b/block/dmg.c index 32623e2..073e864 100644 --- a/block/dmg.c +++ b/block/dmg.c @@ -621,27 +621,47 @@ static inline int dmg_read_chunk(BlockDriverState *bs, uint64_t sector_num, switch (s->types[chunk]) { /* block entry type */ case 0x80000005: { /* zlib compressed */ - /* we need to buffer, because only the chunk as whole can be - * inflated. */ - ret = bdrv_pread(bs->file, s->offsets[chunk], - s->compressed_chunk, s->lengths[chunk]); - if (ret != s->lengths[chunk]) { - return -1; + /* check for cached random access point */ + if (drs->saved_next_in == NULL) { + /* we need to buffer, because only the chunk as whole can be + * inflated. */ + ret = bdrv_pread(bs->file, s->offsets[chunk], + s->compressed_chunk, s->lengths[chunk]); + if (ret != s->lengths[chunk]) { + return -1; + } + + s->zstream.next_in = s->compressed_chunk; + s->zstream.avail_in = s->lengths[chunk]; + } else { + s->zstream.next_in = drs->saved_next_in; + s->zstream.avail_in = drs->saved_avail_in; } - s->zstream.next_in = s->compressed_chunk; - s->zstream.avail_in = s->lengths[chunk]; s->zstream.next_out = s->uncompressed_chunk; - s->zstream.avail_out = 512 * s->sectorcounts[chunk]; - ret = inflateReset(&s->zstream); - if (ret != Z_OK) { - return -1; + + s->zstream.avail_out = sectors_read * BDRV_SECTOR_SIZE; + + if (drs->saved_next_in == NULL) { + ret = inflateReset(&s->zstream); + if (ret != Z_OK) { + return -1; + } + } + /* reset total_out for each successive call */ + s->zstream.total_out = 0; + ret = inflate(&s->zstream, Z_SYNC_FLUSH); + if (ret == Z_OK && + s->zstream.total_out == 512 * sectors_read) { + goto update; } - ret = inflate(&s->zstream, Z_FINISH); if (ret != Z_STREAM_END || - s->zstream.total_out != 512 * s->sectorcounts[chunk]) { + s->zstream.total_out != 512 * sectors_read) { return -1; } +update: + cache_access_point(drs, s->zstream.next_in, s->zstream.avail_in, + chunk, sectors_read, sector_offset); break; } case 0x80000006: /* bzip2 compressed */ if (!dmg_uncompress_bz2) {