From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:43933) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y4U6v-0001Ii-PE for qemu-devel@nongnu.org; Fri, 26 Dec 2014 07:35:18 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Y4U6q-0001r3-K1 for qemu-devel@nongnu.org; Fri, 26 Dec 2014 07:35:17 -0500 Received: from mailhub.sw.ru ([195.214.232.25]:22196 helo=relay.sw.ru) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y4U6q-0001qL-8i for qemu-devel@nongnu.org; Fri, 26 Dec 2014 07:35:12 -0500 From: "Denis V. Lunev" Date: Fri, 26 Dec 2014 15:35:12 +0300 Message-Id: <1419597313-20514-7-git-send-email-den@openvz.org> In-Reply-To: <1419597313-20514-1-git-send-email-den@openvz.org> References: <1419597313-20514-1-git-send-email-den@openvz.org> Subject: [Qemu-devel] [PATCH 6/7] block: use fallocate(FALLOC_FL_PUNCH_HOLE) & fallocate(0) to write zeroes List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , "Denis V. Lunev" , qemu-devel@nongnu.org, Stefan Hajnoczi This sequence works efficiently if FALLOC_FL_ZERO_RANGE is not supported. Simple fallocate(0) will extend file with zeroes when appropriate in the middle of the file if there is a hole there and at the end of the file. Unfortunately fallocate(0) does not drop the content of the file if there is a data on this offset. Therefore to make the situation consistent we should drop the data beforehand. This is done using FALLOC_FL_PUNCH_HOLE This should increase the performance a bit for not-so-modern kernels or for filesystems which do not support FALLOC_FL_ZERO_RANGE. Signed-off-by: Denis V. Lunev CC: Kevin Wolf CC: Stefan Hajnoczi --- block/raw-posix.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/block/raw-posix.c b/block/raw-posix.c index 7866d31..96a8678 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -968,6 +968,23 @@ static ssize_t handle_aiocb_write_zeroes(RawPosixAIOData *aiocb) #endif s->has_write_zeroes = false; + +#ifdef CONFIG_FALLOCATE_PUNCH_HOLE + if (s->has_discard) { + int ret; + ret = do_fallocate(s->fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, + aiocb->aio_offset, aiocb->aio_nbytes); + if (ret < 0) { + if (ret == -ENOTSUP) { + s->has_discard = false; + } + return ret; + } + return do_fallocate(s->fd, 0, aiocb->aio_offset, aiocb->aio_nbytes); + } +#endif + + s->has_discard = false; return -ENOTSUP; } -- 1.9.1