From patchwork Wed Nov 27 02:15:41 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hu Tao X-Patchwork-Id: 294465 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 474642C00A6 for ; Wed, 27 Nov 2013 13:18:49 +1100 (EST) Received: from localhost ([::1]:33542 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VlUiE-0003q4-EW for incoming@patchwork.ozlabs.org; Tue, 26 Nov 2013 21:18:46 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53129) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VlUhe-0003jv-KP for qemu-devel@nongnu.org; Tue, 26 Nov 2013 21:18:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VlUhZ-00042K-0q for qemu-devel@nongnu.org; Tue, 26 Nov 2013 21:18:10 -0500 Received: from [222.73.24.84] (port=24396 helo=song.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VlUhY-000423-KH for qemu-devel@nongnu.org; Tue, 26 Nov 2013 21:18:04 -0500 X-IronPort-AV: E=Sophos;i="4.93,779,1378828800"; d="scan'208";a="9133759" Received: from unknown (HELO tang.cn.fujitsu.com) ([10.167.250.3]) by song.cn.fujitsu.com with ESMTP; 27 Nov 2013 10:14:23 +0800 Received: from fnstmail02.fnst.cn.fujitsu.com (tang.cn.fujitsu.com [127.0.0.1]) by tang.cn.fujitsu.com (8.14.3/8.13.1) with ESMTP id rAR2HnQl008384; Wed, 27 Nov 2013 10:17:49 +0800 Received: from G08FNSTD100614.fnst.cn.fujitsu.com ([10.167.226.102]) by fnstmail02.fnst.cn.fujitsu.com (Lotus Domino Release 8.5.3) with ESMTP id 2013112710154005-444151 ; Wed, 27 Nov 2013 10:15:40 +0800 From: Hu Tao To: Kevin Wolf , "Daniel P. Berrange" , Peter Lieven Date: Wed, 27 Nov 2013 10:15:41 +0800 Message-Id: <46eda2920fc6169f16957756306acca4ed3bbe95.1385518315.git.hutao@cn.fujitsu.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: References: X-MIMETrack: Itemize by SMTP Server on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2013/11/27 10:15:40, Serialize by Router on mailserver/fnst(Release 8.5.3|September 15, 2011) at 2013/11/27 10:15:42, Serialize complete at 2013/11/27 10:15:42 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 222.73.24.84 Cc: qemu-devel@nongnu.org Subject: [Qemu-devel] [RFC PATCH v2 3/6] block/raw-posix: implement bdrv_preallocate X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Signed-off-by: Hu Tao --- block/raw-posix.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/block/raw-posix.c b/block/raw-posix.c index f836c8e..c563073 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -1050,6 +1050,39 @@ static int64_t raw_get_allocated_file_size(BlockDriverState *bs) return (int64_t)st.st_blocks * 512; } +#ifdef __linux__ +static int raw_preallocate2(int fd, int64_t offset, int64_t length) +{ + int ret = -1; + + ret = fallocate(fd, 0, offset, length); + + /* fallback to posix_fallocate() if fallocate() is not supported */ + if (ret < 0 && (errno == ENOSYS || errno == EOPNOTSUPP)) { + ret = posix_fallocate(fd, offset, length); + } + + return ret; +} +#else +static int raw_preallocate2(int fd, int64_t offset, int64_t length) +{ + return posix_fallocate(fd, offset, length); +} +#endif + +static int raw_preallocate(BlockDriverState *bs, int64_t offset, int64_t length) +{ + BDRVRawState *s = bs->opaque; + int64_t len = bdrv_getlength(bs); + + if (offset + length < 0 || offset + length > len) { + return -1; + } + + return raw_preallocate2(s->fd, offset, length); +} + static int raw_create(const char *filename, QEMUOptionParameter *options, Error **errp) { @@ -1221,6 +1254,7 @@ static BlockDriver bdrv_file = { .bdrv_close = raw_close, .bdrv_create = raw_create, .bdrv_has_zero_init = bdrv_has_zero_init_1, + .bdrv_preallocate = raw_preallocate, .bdrv_co_get_block_status = raw_co_get_block_status, .bdrv_aio_readv = raw_aio_readv,