From patchwork Thu Dec 16 18:36:31 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christoph Hellwig X-Patchwork-Id: 75787 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 64E71B6EE8 for ; Fri, 17 Dec 2010 05:38:02 +1100 (EST) Received: from localhost ([127.0.0.1]:42942 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PTIhe-0000dm-JR for incoming@patchwork.ozlabs.org; Thu, 16 Dec 2010 13:37:22 -0500 Received: from [140.186.70.92] (port=58206 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PTIgs-0000bO-70 for qemu-devel@nongnu.org; Thu, 16 Dec 2010 13:36:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PTIgq-0003ov-RQ for qemu-devel@nongnu.org; Thu, 16 Dec 2010 13:36:33 -0500 Received: from verein.lst.de ([213.95.11.210]:32878) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PTIgq-0003oi-JG for qemu-devel@nongnu.org; Thu, 16 Dec 2010 13:36:32 -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 oBGIaV88017545 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Thu, 16 Dec 2010 19:36:31 +0100 Received: (from hch@localhost) by verein.lst.de (8.12.3/8.12.3/Debian-7.2) id oBGIaVip017544 for qemu-devel@nongnu.org; Thu, 16 Dec 2010 19:36:31 +0100 Date: Thu, 16 Dec 2010 19:36:31 +0100 From: Christoph Hellwig To: qemu-devel@nongnu.org Message-ID: <20101216183631.GA17521@lst.de> References: <20101210150038.GA30990@lst.de> <20101216183614.GA17260@lst.de> Mime-Version: 1.0 Content-Disposition: inline In-Reply-To: <20101216183614.GA17260@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/3] 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-12-16 16:51:43.000000000 +0100 +++ qemu/block.c 2010-12-16 16:52:05.875253180 +0100 @@ -1515,6 +1515,17 @@ int bdrv_has_zero_init(BlockDriverState return 1; } +int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) +{ + if (!bs->drv) { + return -ENOMEDIUM; + } + if (!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-12-16 16:51:43.000000000 +0100 +++ qemu/block.h 2010-12-16 16:52:05.875253180 +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-12-16 16:51:43.000000000 +0100 +++ qemu/block_int.h 2010-12-16 16:52:38.542254787 +0100 @@ -72,6 +72,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 min_io_size; uint32_t opt_io_size; int32_t bootindex; + uint32_t discard_granularity; } BlockConf; static inline unsigned int get_physical_block_exp(BlockConf *conf) @@ -250,6 +253,8 @@ static inline unsigned int get_physical_ _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_INT32("bootindex", _state, _conf.bootindex, -1) \ + DEFINE_PROP_INT32("bootindex", _state, _conf.bootindex, -1), \ + 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-12-15 10:05:38.000000000 +0100 +++ qemu/block/raw.c 2010-12-16 16:52:05.882253111 +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,