Received: from eggs.gnu.org ([2001:4830:134:3::10]:57846) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1c6ai5-0003WN-M0 for qemu-devel@nongnu.org; Tue, 15 Nov 2016 05:11:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1c6ai3-0003cD-4v for qemu-devel@nongnu.org; Tue, 15 Nov 2016 05:11:25 -0500 From: Pavel Butsykin Date: Tue, 15 Nov 2016 09:37:07 +0300 Message-ID: <20161115063715.12561-11-pbutsykin@virtuozzo.com> In-Reply-To: <20161115063715.12561-1-pbutsykin@virtuozzo.com> References: <20161115063715.12561-1-pbutsykin@virtuozzo.com> MIME-Version: 1.0 Content-Type: text/plain Subject: [Qemu-devel] [PATCH v1 10/18] block/pcache: skip readahead for unallocated clusters List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org, qemu-block@nongnu.org Cc: den@openvz.org, famz@redhat.com, stefanha@redhat.com, mreitz@redhat.com, kwolf@redhat.com, eblake@redhat.com Typically, data for unallocated clusters is filled with zeros, so it makes no sense to store it in the cache. Signed-off-by: Pavel Butsykin --- block/pcache.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/block/pcache.c b/block/pcache.c index 3717037..1f3800d 100644 --- a/block/pcache.c +++ b/block/pcache.c @@ -223,6 +223,28 @@ static RBCacheNode *pcache_node_alloc(uint64_t offset, uint64_t bytes, return &node->common; } +static bool check_allocated_clusters(BlockDriverState *bs, uint64_t offset, + uint64_t bytes) +{ + int64_t sector_num = offset >> BDRV_SECTOR_BITS; + int32_t nb_sectors = bytes >> BDRV_SECTOR_BITS; + + assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0); + assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0); + + do { + int num, ret = bdrv_is_allocated(bs, sector_num, nb_sectors, &num); + if (ret <= 0) { + return false; + } + sector_num += num; + nb_sectors -= num; + + } while (nb_sectors); + + return true; +} + #define PCACHE_STEPS_FORWARD 2 static PCacheNode *get_readahead_node(BlockDriverState *bs, RBCache *rbcache, @@ -242,6 +264,10 @@ static PCacheNode *get_readahead_node(BlockDriverState *bs, RBCache *rbcache, break; } + if (!check_allocated_clusters(bs, offset, bytes)) { + break; + } + node = rbcache_search_and_insert(rbcache, offset, bytes); if (node->status == NODE_STATUS_NEW) { return node; -- 2.10.1