Skip to content

Commit

Permalink
raw: support discard on block devices
Browse files Browse the repository at this point in the history
Block devices use a ioctl instead of fallocate, so add a separate
implementation.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
  • Loading branch information
bonzini committed Aug 20, 2012
1 parent 721d330 commit 0f6342c
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions block/raw-posix.c
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -1215,6 +1215,36 @@ static BlockDriverAIOCB *hdev_aio_ioctl(BlockDriverState *bs,
return paio_ioctl(bs, s->fd, req, buf, cb, opaque); 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__) #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
static int fd_open(BlockDriverState *bs) static int fd_open(BlockDriverState *bs)
{ {
Expand Down Expand Up @@ -1280,6 +1310,8 @@ static BlockDriver bdrv_host_device = {
.create_options = raw_create_options, .create_options = raw_create_options,
.bdrv_has_zero_init = hdev_has_zero_init, .bdrv_has_zero_init = hdev_has_zero_init,


.bdrv_co_discard = hdev_co_discard,

.bdrv_aio_readv = raw_aio_readv, .bdrv_aio_readv = raw_aio_readv,
.bdrv_aio_writev = raw_aio_writev, .bdrv_aio_writev = raw_aio_writev,
.bdrv_aio_flush = raw_aio_flush, .bdrv_aio_flush = raw_aio_flush,
Expand Down

0 comments on commit 0f6342c

Please sign in to comment.