Received: from eggs.gnu.org ([208.118.235.92]:40917) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S5gwh-0005LQ-Eu for qemu-devel@nongnu.org; Thu, 08 Mar 2012 12:16:27 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1S5gwZ-0004sP-QO for qemu-devel@nongnu.org; Thu, 08 Mar 2012 12:16:06 -0500 Received: from mail-gx0-f173.google.com ([209.85.161.173]:33562) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S5gwZ-0004mj-Jt for qemu-devel@nongnu.org; Thu, 08 Mar 2012 12:15:59 -0500 Received: by mail-gx0-f173.google.com with SMTP id j2so436427ggn.4 for ; Thu, 08 Mar 2012 09:15:59 -0800 (PST) Sender: Paolo Bonzini From: Paolo Bonzini Date: Thu, 8 Mar 2012 18:15:14 +0100 Message-Id: <1331226917-6658-15-git-send-email-pbonzini@redhat.com> In-Reply-To: <1331226917-6658-1-git-send-email-pbonzini@redhat.com> References: <1331226917-6658-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [RFC PATCH 14/17] block: support FALLOC_FL_PUNCH_HOLE trimming List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org This will enable discard on more filesystems, including ext4. Signed-off-by: Paolo Bonzini --- block/raw-posix.c | 29 ++++++++++++++++++++++++----- 1 files changed, 24 insertions(+), 5 deletions(-) diff --git a/block/raw-posix.c b/block/raw-posix.c index 2d1bc13..f23d92b 100644 --- a/block/raw-posix.c +++ b/block/raw-posix.c @@ -52,6 +52,7 @@ #include #include #include +#include #endif #if defined (__FreeBSD__) || defined(__FreeBSD_kernel__) #include @@ -131,6 +132,7 @@ typedef struct BDRVRawState { #endif uint8_t *aligned_buf; unsigned aligned_buf_size; + bool has_discard : 1; #ifdef CONFIG_XFS bool is_xfs : 1; #endif @@ -257,11 +259,9 @@ static int raw_open_common(BlockDriverState *bs, const char *filename, } #ifdef CONFIG_XFS - if (platform_test_xfs_fd(s->fd)) { - s->is_xfs = 1; - } + s->is_xfs = platform_test_xfs_fd(s->fd); #endif - + s->has_discard = 1; return 0; out_free_buf: @@ -605,15 +605,34 @@ static int xfs_discard(BDRVRawState *s, int64_t sector_num, int nb_sectors) static coroutine_fn int raw_co_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) { -#ifdef CONFIG_XFS BDRVRawState *s = bs->opaque; + int retval; + if (s->has_discard == 0) { + return -ENOTSUP; + } +#ifdef CONFIG_XFS if (s->is_xfs) { return xfs_discard(s, sector_num, nb_sectors); } #endif +#ifdef FALLOC_FL_PUNCH_HOLE + retval = fallocate(s->fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, + sector_num << 9, (int64_t)nb_sectors << 9); + if (retval == -1) { + if (errno == ENODEV || errno == ENOSYS || errno == EOPNOTSUPP) { + s->has_discard = 0; + return -ENOTSUP; + } else { + return -errno; + } + } return 0; +#else + s->has_discard = 0; + return -ENOTSUP; +#endif } static QEMUOptionParameter raw_create_options[] = { -- 1.7.7.6