From patchwork Thu Nov 25 13:57:09 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 73061 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [199.232.76.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id AB020B6F14 for ; Fri, 26 Nov 2010 00:58:11 +1100 (EST) Received: from localhost ([127.0.0.1]:35620 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PLcKu-00073j-Ua for incoming@patchwork.ozlabs.org; Thu, 25 Nov 2010 08:58:09 -0500 Received: from [140.186.70.92] (port=47428 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PLcK0-00072I-7i for qemu-devel@nongnu.org; Thu, 25 Nov 2010 08:57:13 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PLcJz-0001MW-1w for qemu-devel@nongnu.org; Thu, 25 Nov 2010 08:57:12 -0500 Received: from verein.lst.de ([213.95.11.210]:56211) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PLcJy-0001MG-Pi for qemu-devel@nongnu.org; Thu, 25 Nov 2010 08:57:11 -0500 Received: from verein.lst.de (localhost [127.0.0.1]) by verein.lst.de (8.12.3/8.12.3/Debian-7.1) with ESMTP id oAPDv988003055 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Thu, 25 Nov 2010 14:57:09 +0100 Received: (from hch@localhost) by verein.lst.de (8.12.3/8.12.3/Debian-7.2) id oAPDv9Hc003054 for qemu-devel@nongnu.org; Thu, 25 Nov 2010 14:57:09 +0100 Date: Thu, 25 Nov 2010 14:57:09 +0100 From: Christoph Hellwig To: qemu-devel@nongnu.org Message-ID: <20101125135709.GA3034@lst.de> References: <20101125135657.GA2814@lst.de> Mime-Version: 1.0 Content-Disposition: inline In-Reply-To: <20101125135657.GA2814@lst.de> User-Agent: Mutt/1.3.28i X-Scanned-By: MIMEDefang 2.39 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) Subject: [Qemu-devel] [PATCH 1/5] block: add discard support X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: qemu-devel.nongnu.org List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Add a new bdrv_discard method to free blocks in a mapping image, and a new drive property to set the granularity for these discard. If no discard granularity support is set discard support is disabled. Signed-off-by: Christoph Hellwig Index: qemu/block.c =================================================================== --- qemu.orig/block.c 2010-11-25 12:34:10.580256183 +0100 +++ qemu/block.c 2010-11-25 12:34:41.761254654 +0100 @@ -1499,6 +1499,13 @@ int bdrv_has_zero_init(BlockDriverState return 1; } +int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) +{ + if (!bs->drv || !bs->drv->bdrv_discard) + return 0; + return bs->drv->bdrv_discard(bs, sector_num, nb_sectors); +} + /* * Returns true iff the specified sector is present in the disk image. Drivers * not implementing the functionality are assumed to not support backing files, Index: qemu/block.h =================================================================== --- qemu.orig/block.h 2010-11-25 12:34:10.589253738 +0100 +++ qemu/block.h 2010-11-25 12:34:12.148012156 +0100 @@ -146,6 +146,7 @@ int bdrv_flush(BlockDriverState *bs); void bdrv_flush_all(void); void bdrv_close_all(void); +int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors); int bdrv_has_zero_init(BlockDriverState *bs); int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum); Index: qemu/block_int.h =================================================================== --- qemu.orig/block_int.h 2010-11-25 12:34:10.595254018 +0100 +++ qemu/block_int.h 2010-11-25 12:34:12.150013832 +0100 @@ -73,6 +73,8 @@ struct BlockDriver { BlockDriverCompletionFunc *cb, void *opaque); BlockDriverAIOCB *(*bdrv_aio_flush)(BlockDriverState *bs, BlockDriverCompletionFunc *cb, void *opaque); + int (*bdrv_discard)(BlockDriverState *bs, int64_t sector_num, + int nb_sectors); int (*bdrv_aio_multiwrite)(BlockDriverState *bs, BlockRequest *reqs, int num_reqs); @@ -227,6 +229,7 @@ typedef struct BlockConf { uint16_t logical_block_size; uint16_t min_io_size; uint32_t opt_io_size; + uint32_t discard_granularity; } BlockConf; static inline unsigned int get_physical_block_exp(BlockConf *conf) @@ -249,6 +252,8 @@ static inline unsigned int get_physical_ DEFINE_PROP_UINT16("physical_block_size", _state, \ _conf.physical_block_size, 512), \ DEFINE_PROP_UINT16("min_io_size", _state, _conf.min_io_size, 0), \ - DEFINE_PROP_UINT32("opt_io_size", _state, _conf.opt_io_size, 0) + DEFINE_PROP_UINT32("opt_io_size", _state, _conf.opt_io_size, 0), \ + DEFINE_PROP_UINT32("discard_granularity", _state, \ + _conf.discard_granularity, 0) #endif /* BLOCK_INT_H */ Index: qemu/block/raw.c =================================================================== --- qemu.orig/block/raw.c 2010-11-25 12:34:10.601259605 +0100 +++ qemu/block/raw.c 2010-11-25 12:34:12.155004124 +0100 @@ -65,6 +65,11 @@ static int raw_probe(const uint8_t *buf, return 1; /* everything can be opened as raw image */ } +static int raw_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) +{ + return bdrv_discard(bs->file, sector_num, nb_sectors); +} + static int raw_is_inserted(BlockDriverState *bs) { return bdrv_is_inserted(bs->file); @@ -130,6 +135,7 @@ static BlockDriver bdrv_raw = { .bdrv_aio_readv = raw_aio_readv, .bdrv_aio_writev = raw_aio_writev, .bdrv_aio_flush = raw_aio_flush, + .bdrv_discard = raw_discard, .bdrv_is_inserted = raw_is_inserted, .bdrv_eject = raw_eject,