[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 23/26] block: implement is_allocated for raw
From: |
Paolo Bonzini |
Subject: |
[Qemu-devel] [PATCH 23/26] block: implement is_allocated for raw |
Date: |
Thu, 12 Apr 2012 14:01:13 +0200 |
Either FIEMAP, or SEEK_DATA+SEEK_HOLE can be used to implement the
is_allocated callback for raw files. Ext4, btrfs and XFS all support
it.
Signed-off-by: Paolo Bonzini <address@hidden>
---
block/raw-posix.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++
block/raw.c | 8 +++++
2 files changed, 110 insertions(+)
diff --git a/block/raw-posix.c b/block/raw-posix.c
index 2d1bc13..2969e1e 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -52,6 +52,10 @@
#include <sys/param.h>
#include <linux/cdrom.h>
#include <linux/fd.h>
+#include <linux/fs.h>
+#endif
+#ifdef CONFIG_FIEMAP
+#include <linux/fiemap.h>
#endif
#if defined (__FreeBSD__) || defined(__FreeBSD_kernel__)
#include <sys/disk.h>
@@ -104,6 +108,13 @@
#define O_DIRECT O_DSYNC
#endif
+#ifndef SEEK_DATA
+#define SEEK_DATA 3
+#endif
+#ifndef SEEK_HOLE
+#define SEEK_HOLE 4
+#endif
+
#define FTYPE_FILE 0
#define FTYPE_CD 1
#define FTYPE_FD 2
@@ -583,6 +594,96 @@ static int raw_create(const char *filename,
QEMUOptionParameter *options)
return result;
}
+/*
+ * Returns true iff the specified sector is present in the disk image. Drivers
+ * not implementing the functionality are assumed to not support backing files,
+ * hence all their sectors are reported as allocated.
+ *
+ * If 'sector_num' is beyond the end of the disk image the return value is 0
+ * and 'pnum' is set to 0.
+ *
+ * 'pnum' is set to the number of sectors (including and immediately following
+ * the specified sector) that are known to be in the same
+ * allocated/unallocated state.
+ *
+ * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
+ * beyond the end of the disk image it will be clamped.
+ */
+static int coroutine_fn raw_co_is_allocated(BlockDriverState *bs,
+ int64_t sector_num,
+ int nb_sectors, int *pnum)
+{
+ BDRVRawState *s = bs->opaque;
+ off_t start, data, hole;
+ int ret;
+
+ ret = fd_open(bs);
+ if (ret < 0) {
+ return ret;
+ }
+
+ start = sector_num * BDRV_SECTOR_SIZE;
+#ifdef CONFIG_FIEMAP
+ struct {
+ struct fiemap fm;
+ struct fiemap_extent fe;
+ } f;
+ f.fm.fm_start = start;
+ f.fm.fm_length = (int64_t)nb_sectors * BDRV_SECTOR_SIZE;
+ f.fm.fm_flags = 0;
+ f.fm.fm_extent_count = 1;
+ f.fm.fm_reserved = 0;
+ if (ioctl(s->fd, FS_IOC_FIEMAP, &f) == -1) {
+ /* Assume everything is allocated. */
+ *pnum = nb_sectors;
+ return 1;
+ }
+
+ if (f.fm.fm_mapped_extents == 0) {
+ /* No extents found, data is beyond f.fm.fm_start + f.fm.fm_length.
+ * f.fm.fm_start + f.fm.fm_length must be clamped to the file size!
+ */
+ off_t length = lseek(s->fd, 0, SEEK_END);
+ hole = f.fm.fm_start;
+ data = MIN(f.fm.fm_start + f.fm.fm_length, length);
+ } else {
+ data = f.fe.fe_logical;
+ hole = f.fe.fe_logical + f.fe.fe_length;
+ }
+#else
+ hole = lseek(s->fd, start, SEEK_HOLE);
+ if (hole == -1) {
+ /* -ENXIO indicates that sector_num was past the end of the file.
+ * There is a virtual hole there. */
+ assert(errno != -ENXIO);
+
+ /* Most likely EINVAL. Assume everything is allocated. */
+ *pnum = nb_sectors;
+ return 1;
+ }
+
+ if (hole > start) {
+ data = start;
+ } else {
+ /* On a hole. We need another syscall to find its end. */
+ data = lseek(s->fd, start, SEEK_DATA);
+ if (data == -1) {
+ data = lseek(s->fd, 0, SEEK_END);
+ }
+ }
+#endif
+
+ if (data <= start) {
+ /* On a data extent, compute sectors to the end of the extent. */
+ *pnum = MIN(nb_sectors, (hole - start) / BDRV_SECTOR_SIZE);
+ return 1;
+ } else {
+ /* On a hole, compute sectors to the beginning of the next extent. */
+ *pnum = MIN(nb_sectors, (data - start) / BDRV_SECTOR_SIZE);
+ return 0;
+ }
+}
+
#ifdef CONFIG_XFS
static int xfs_discard(BDRVRawState *s, int64_t sector_num, int nb_sectors)
{
@@ -634,6 +735,7 @@ static BlockDriver bdrv_file = {
.bdrv_close = raw_close,
.bdrv_create = raw_create,
.bdrv_co_discard = raw_co_discard,
+ .bdrv_co_is_allocated = raw_co_is_allocated,
.bdrv_aio_readv = raw_aio_readv,
.bdrv_aio_writev = raw_aio_writev,
diff --git a/block/raw.c b/block/raw.c
index 7086e31..09d9b48 100644
--- a/block/raw.c
+++ b/block/raw.c
@@ -25,6 +25,13 @@ static void raw_close(BlockDriverState *bs)
{
}
+static int coroutine_fn raw_co_is_allocated(BlockDriverState *bs,
+ int64_t sector_num,
+ int nb_sectors, int *pnum)
+{
+ return bdrv_co_is_allocated(bs->file, sector_num, nb_sectors, pnum);
+}
+
static int64_t raw_getlength(BlockDriverState *bs)
{
return bdrv_getlength(bs->file);
@@ -108,6 +115,7 @@ static BlockDriver bdrv_raw = {
.bdrv_co_readv = raw_co_readv,
.bdrv_co_writev = raw_co_writev,
+ .bdrv_co_is_allocated = raw_co_is_allocated,
.bdrv_co_discard = raw_co_discard,
.bdrv_probe = raw_probe,
--
1.7.9.3
- [Qemu-devel] [PATCH 07/26] block: add the support to drain throttled requests, (continued)
- [Qemu-devel] [PATCH 07/26] block: add the support to drain throttled requests, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 08/26] block: add mode argument to blockdev-snapshot-sync, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 06/26] aio: simplify qemu_aio_wait, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 09/26] block: fail live snapshot if disk has no medium, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 10/26] block: fix snapshot on QED, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 14/26] block: fix allocation size for dirty bitmap, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 12/26] block: update in-memory backing file and format, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 11/26] block: push bdrv_change_backing_file error checking up from drivers, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 13/26] block: open backing file as read-only when probing for size, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 22/26] stream: do not copy unallocated sectors from the base, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 23/26] block: implement is_allocated for raw,
Paolo Bonzini <=
- [Qemu-devel] [PATCH 24/26] stream: tweak usage of bdrv_co_is_allocated, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 16/26] qemu-io: correctly print non-integer values as decimals, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 25/26] stream: move is_allocated_above to block.c, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 17/26] qemu-io: fix the alloc command, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 15/26] qemu-iotests: strip spaces from qemu-img/qemu-io/qemu command lines, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 21/26] stream: fix ratelimiting corner case, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 18/26] stream: fix sectors not allocated test, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 19/26] stream: add testcase for partial streaming, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 26/26] stream: move rate limiting to a separate header file, Paolo Bonzini, 2012/04/12
- [Qemu-devel] [PATCH 20/26] stream: pass new base image format to bdrv_change_backing_file, Paolo Bonzini, 2012/04/12