From patchwork Fri Jan 31 17:44:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 11360345 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4D5FD159A for ; Fri, 31 Jan 2020 17:51:17 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 242992082E for ; Fri, 31 Jan 2020 17:51:17 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Rhha1wMP" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 242992082E Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:57296 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixaRo-0004ob-8S for patchwork-qemu-devel@patchwork.kernel.org; Fri, 31 Jan 2020 12:51:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:41548) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ixaLk-0003Au-KC for qemu-devel@nongnu.org; Fri, 31 Jan 2020 12:45:01 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ixaLj-0004h3-Fr for qemu-devel@nongnu.org; Fri, 31 Jan 2020 12:45:00 -0500 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:34405 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ixaLj-0004gT-BY for qemu-devel@nongnu.org; Fri, 31 Jan 2020 12:44:59 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580492699; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Q5LRfQ5sKsOuscvW/woHxAejuTxhwKv2TY1mM/n+QBw=; b=Rhha1wMPG5U2jvSJGV7WlRNu3vtP/ysGFxBifZ9ordAb/dFLwgqZV9EB8HZdcmkRdX8DEw hEBPVLVIL1bRy2KTzHaV0ULTMMLVsFHYrnS4fRswJAj1qht/0EMp0QlIPzAgWy67gnzGVE EtJ2s7nJJLCKtYicaSIRxjc/MHVUW/U= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-31-7Hu66hmNPLCyKYsJr_Ghvg-1; Fri, 31 Jan 2020 12:44:55 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EEA6A107ACC4; Fri, 31 Jan 2020 17:44:53 +0000 (UTC) Received: from blue.redhat.com (ovpn-116-181.phx2.redhat.com [10.3.116.181]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7D2E97FB60; Fri, 31 Jan 2020 17:44:53 +0000 (UTC) From: Eric Blake To: qemu-devel@nongnu.org Subject: [PATCH 11/17] file-posix: Support BDRV_ZERO_OPEN Date: Fri, 31 Jan 2020 11:44:30 -0600 Message-Id: <20200131174436.2961874-12-eblake@redhat.com> In-Reply-To: <20200131174436.2961874-1-eblake@redhat.com> References: <20200131174436.2961874-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-MC-Unique: 7Hu66hmNPLCyKYsJr_Ghvg-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 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: david.edmondson@oracle.com, Kevin Wolf , qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" A single lseek(SEEK_DATA) is sufficient to tell us if a raw file is completely sparse, in which case it reads as all zeroes. Not done here, but possible extension for the future: when working with block devices instead of files, there may be various ways with ioctl or similar to quickly probe if a given block device is known to be completely unmapped where unmapped regions read as zero. But for now, block devices remain without a .bdrv_known_zeroes, because most block devices have random content without an explicit pre-zeroing pass. Signed-off-by: Eric Blake --- block/file-posix.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/block/file-posix.c b/block/file-posix.c index ff9e39ab882f..b4d73dd0363b 100644 --- a/block/file-posix.c +++ b/block/file-posix.c @@ -2541,6 +2541,19 @@ static int find_allocation(BlockDriverState *bs, off_t start, #endif } +static int raw_known_zeroes(BlockDriverState *bs) +{ + /* This callback is only installed for files, not block devices. */ + int r = BDRV_ZERO_CREATE | BDRV_ZERO_TRUNCATE; + off_t data, hole; + + if (find_allocation(bs, 0, &data, &hole) == -ENXIO) { + r |= BDRV_ZERO_OPEN; + } + + return r; +} + /* * Returns the allocation status of the specified offset. * @@ -3071,7 +3084,7 @@ BlockDriver bdrv_file = { .bdrv_close = raw_close, .bdrv_co_create = raw_co_create, .bdrv_co_create_opts = raw_co_create_opts, - .bdrv_known_zeroes = bdrv_known_zeroes_truncate, + .bdrv_known_zeroes = raw_known_zeroes, .bdrv_co_block_status = raw_co_block_status, .bdrv_co_invalidate_cache = raw_co_invalidate_cache, .bdrv_co_pwrite_zeroes = raw_co_pwrite_zeroes,