[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 39/64] block: Improve get_allocated_file_size's default
From: |
Kevin Wolf |
Subject: |
[PULL 39/64] block: Improve get_allocated_file_size's default |
Date: |
Mon, 7 Sep 2020 13:09:11 +0200 |
From: Max Reitz <mreitz@redhat.com>
There are two practical problems with bdrv_get_allocated_file_size()'s
default right now:
(1) For drivers with children, we should generally sum all their sizes
instead of just passing the request through to bs->file. The latter
is good for filters, but not so much for format drivers.
(2) Filters need not have bs->file, so we should actually go to the
filtered child instead of hard-coding bs->file.
Fix this by splitting the default implementation into three branches:
(1) For filter drivers: Return the size of the filtered child
(2) For protocol drivers: Return -ENOTSUP, because the default
implementation cannot make a guess
(3) For other drivers: Sum all data-bearing children's sizes
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
block.c | 42 +++++++++++++++++++++++++++++++++++++++---
1 file changed, 39 insertions(+), 3 deletions(-)
diff --git a/block.c b/block.c
index ac4ab07f07..63c7d9b1a7 100644
--- a/block.c
+++ b/block.c
@@ -5005,6 +5005,31 @@ exit:
return ret;
}
+/**
+ * Implementation of BlockDriver.bdrv_get_allocated_file_size() that
+ * sums the size of all data-bearing children. (This excludes backing
+ * children.)
+ */
+static int64_t bdrv_sum_allocated_file_size(BlockDriverState *bs)
+{
+ BdrvChild *child;
+ int64_t child_size, sum = 0;
+
+ QLIST_FOREACH(child, &bs->children, next) {
+ if (child->role & (BDRV_CHILD_DATA | BDRV_CHILD_METADATA |
+ BDRV_CHILD_FILTERED))
+ {
+ child_size = bdrv_get_allocated_file_size(child->bs);
+ if (child_size < 0) {
+ return child_size;
+ }
+ sum += child_size;
+ }
+ }
+
+ return sum;
+}
+
/**
* Length of a allocated file in bytes. Sparse files are counted by actual
* allocated space. Return < 0 if error or unknown.
@@ -5018,10 +5043,21 @@ int64_t bdrv_get_allocated_file_size(BlockDriverState
*bs)
if (drv->bdrv_get_allocated_file_size) {
return drv->bdrv_get_allocated_file_size(bs);
}
- if (bs->file) {
- return bdrv_get_allocated_file_size(bs->file->bs);
+
+ if (drv->bdrv_file_open) {
+ /*
+ * Protocol drivers default to -ENOTSUP (most of their data is
+ * not stored in any of their children (if they even have any),
+ * so there is no generic way to figure it out).
+ */
+ return -ENOTSUP;
+ } else if (drv->is_filter) {
+ /* Filter drivers default to the size of their filtered child */
+ return bdrv_get_allocated_file_size(bdrv_filter_bs(bs));
+ } else {
+ /* Other drivers default to summing their children's sizes */
+ return bdrv_sum_allocated_file_size(bs);
}
- return -ENOTSUP;
}
/*
--
2.25.4
- [PULL 32/64] block: Flush all children in generic code, (continued)
- [PULL 32/64] block: Flush all children in generic code, Kevin Wolf, 2020/09/07
- [PULL 31/64] block: Re-evaluate backing file handling in reopen, Kevin Wolf, 2020/09/07
- [PULL 34/64] block: Iterate over children in refresh_limits, Kevin Wolf, 2020/09/07
- [PULL 38/64] block: Use CAFs for debug breakpoints, Kevin Wolf, 2020/09/07
- [PULL 33/64] vmdk: Drop vmdk_co_flush(), Kevin Wolf, 2020/09/07
- [PULL 30/64] block: Use bdrv_cow_child() in bdrv_co_truncate(), Kevin Wolf, 2020/09/07
- [PULL 35/64] block: Use CAFs in bdrv_refresh_filename(), Kevin Wolf, 2020/09/07
- [PULL 36/64] block: Use CAF in bdrv_co_rw_vmstate(), Kevin Wolf, 2020/09/07
- [PULL 40/64] block/null: Implement bdrv_get_allocated_file_size, Kevin Wolf, 2020/09/07
- [PULL 43/64] block: Use child access functions for QAPI queries, Kevin Wolf, 2020/09/07
- [PULL 39/64] block: Improve get_allocated_file_size's default,
Kevin Wolf <=
- [PULL 41/64] blockdev: Use CAF in external_snapshot_prepare(), Kevin Wolf, 2020/09/07
- [PULL 46/64] backup: Deal with filters, Kevin Wolf, 2020/09/07
- [PULL 45/64] mirror: Deal with filters, Kevin Wolf, 2020/09/07
- [PULL 47/64] commit: Deal with filters, Kevin Wolf, 2020/09/07
- [PULL 48/64] nbd: Use CAF when looking for dirty bitmap, Kevin Wolf, 2020/09/07
- [PULL 49/64] qemu-img: Use child access functions, Kevin Wolf, 2020/09/07
- [PULL 42/64] block: Report data child for query-blockstats, Kevin Wolf, 2020/09/07
- [PULL 51/64] blockdev: Fix active commit choice, Kevin Wolf, 2020/09/07
- [PULL 50/64] block: Drop backing_bs(), Kevin Wolf, 2020/09/07
- [PULL 53/64] block: Leave BDS.backing_{file,format} constant, Kevin Wolf, 2020/09/07