From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60332) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VtTNF-0004KU-1w for qemu-devel@nongnu.org; Wed, 18 Dec 2013 21:30:09 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1VtTN7-0007mW-J6 for qemu-devel@nongnu.org; Wed, 18 Dec 2013 21:30:05 -0500 Received: from [222.73.24.84] (port=44631 helo=song.cn.fujitsu.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1VtTN7-0007l3-7o for qemu-devel@nongnu.org; Wed, 18 Dec 2013 21:29:57 -0500 From: Hu Tao Date: Thu, 19 Dec 2013 10:27:38 +0800 Message-Id: In-Reply-To: References: Subject: [Qemu-devel] [RFC PATCH v3 3/6] block/raw-posix: implement bdrv_preallocate List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Kevin Wolf , Fam Zheng , Peter Lieven , hutao@cn.fujitsu.com 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 10c6b34..19181f2 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -1160,6 +1160,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 -EINVAL; + } + + return raw_preallocate2(s->fd, offset, length); +} + static int raw_create(const char *filename, QEMUOptionParameter *options, Error **errp) { @@ -1356,6 +1389,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_co_write_zeroes = raw_co_write_zeroes, -- 1.7.11.7