Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

raw: support discard on block devices

Block devices use a ioctl instead of fallocate, so add a separate
implementation.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information...
commit 0f6342c81218646c31afb61b189e9044404ea3e7 1 parent 721d330
@bonzini authored
Showing with 32 additions and 0 deletions.
  1. +32 −0 block/raw-posix.c
View
32 block/raw-posix.c
@@ -1215,6 +1215,36 @@ static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
return paio_ioctl(bs, s->fd, req, buf, cb, opaque);
}
+static coroutine_fn int hdev_co_discard(BlockDriverState *bs,
+ int64_t sector_num, int nb_sectors)
+{
+ BDRVRawState *s = bs->opaque;
+ int retval;
+
+ if (s->has_discard == 0) {
+ return 0;
+ }
+ retval = fd_open(bs);
+ if (retval < 0) {
+ return retval;
+ }
+
+#ifdef BLKDISCARD
+ {
+ uint64_t range[2] = { sector_num * 512, (uint64_t)nb_sectors * 512 };
+ retval = ioctl(s->fd, BLKDISCARD, range);
+ }
+ if (retval == -1 && (errno == ENOTTY || errno == EOPNOTSUPP)) {
+ s->has_discard = 0;
+ retval = 0;
+ }
+#else
+ retval = 0;
+ s->has_discard = 0;
+#endif
+ return retval;
+}
+
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
static int fd_open(BlockDriverState *bs)
{
@@ -1280,6 +1310,8 @@ static BlockDriver bdrv_host_device = {
.create_options = raw_create_options,
.bdrv_has_zero_init = hdev_has_zero_init,
+ .bdrv_co_discard = hdev_co_discard,
+
.bdrv_aio_readv = raw_aio_readv,
.bdrv_aio_writev = raw_aio_writev,
.bdrv_aio_flush = raw_aio_flush,
Please sign in to comment.
Something went wrong with that request. Please try again.