Patchwork [1/5] block: add discard support

login
register
mail settings
Submitter Christoph Hellwig
Date Nov. 25, 2010, 1:57 p.m.
Message ID <20101125135709.GA3034@lst.de>
Download mbox | patch
Permalink /patch/73061/
State New
Headers show

Comments

Christoph Hellwig - Nov. 25, 2010, 1:57 p.m.
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 <hch@lst.de>
malc - Nov. 25, 2010, 2:09 p.m.
On Thu, 25 Nov 2010, Christoph Hellwig wrote:

> 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 <hch@lst.de>
> 
> 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)

Tab here.

>  
>  #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,
>
Stefan Hajnoczi - Nov. 25, 2010, 2:45 p.m.
On Thu, Nov 25, 2010 at 1:57 PM, Christoph Hellwig <hch@lst.de> wrote:
> +int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
> +{
> +    if (!bs->drv || !bs->drv->bdrv_discard)
> +        return 0;

!bs->drv is normally -ENOMEDIUM.  Perhaps we shouldn't lump it in with
!bs->drv->bdrv_discard.

Stefan

Patch

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,