Skip to contentSkip to conversation

block/rbd: implement .bdrv_get_allocated_file_size callback

PATCH Superseded Stefano Garzarella 2 participants 3 messages May 3, 2019 Block layer

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block/rbd: implement .bdrv_get_allocated_file_size callback

This patch allows 'qemu-img info' to show the 'disk size' for rbd images. We use the rbd_diff_iterate2() API to calculate the allocated size for the image.

---
 block/rbd.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
block/rbd.c +33 -0
index 0c549c9935..61447bc0cb 100644
@@ -1046,6 +1046,38 @@ static int64_t qemu_rbd_getlength(BlockDriverState *bs)
     return info.size;
 }
 
+static int rbd_allocated_size_cb(uint64_t offset, size_t len, int exists,
+                                 void *arg)
+{
+    int64_t *alloc_size = (int64_t *) arg;
+
+    if (exists) {
+        (*alloc_size) += len;
+    }
+
+    return 0;
+}
+
+static int64_t qemu_rbd_get_allocated_file_size(BlockDriverState *bs)
+{
+    BDRVRBDState *s = bs->opaque;
+    int64_t alloc_size = 0;
+    int r;
+
+    /*
+     * rbd_diff_iterate2(), if the source snapshot name is NULL, invokes
+     * the callback on all allocated regions of the image.
+     */
+    r = rbd_diff_iterate2(s->image, NULL, 0,
+                          bs->total_sectors * BDRV_SECTOR_SIZE, 0, 1,
+                          &rbd_allocated_size_cb, &alloc_size);
+    if (r < 0) {
+        return r;
+    }
+
+    return alloc_size;
+}
+
 static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs,
                                              int64_t offset,
                                              PreallocMode prealloc,
@@ -1254,6 +1286,7 @@ static BlockDriver bdrv_rbd = {
     .bdrv_get_info          = qemu_rbd_getinfo,
     .create_opts            = &qemu_rbd_create_opts,
     .bdrv_getlength         = qemu_rbd_getlength,
+    .bdrv_get_allocated_file_size = qemu_rbd_get_allocated_file_size,
     .bdrv_co_truncate       = qemu_rbd_co_truncate,
     .protocol_name          = "rbd",
 
Signature · 1 line
2.20.1
#1 lore raw
Quoted 33 lines · Stefano Garzarella

This patch allows 'qemu-img info' to show the 'disk size' for rbd images. We use the rbd_diff_iterate2() API to calculate the allocated size for the image.

---
 block/rbd.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
block/rbd.c +15 -0
index 0c549c9935..61447bc0cb 100644
@@ -1046,6 +1046,38 @@ static int64_t qemu_rbd_getlength(BlockDriverState *bs)
     return info.size;
 }
 
+static int rbd_allocated_size_cb(uint64_t offset, size_t len, int exists,
+                                 void *arg)
+{
+    int64_t *alloc_size = (int64_t *) arg;
+
+    if (exists) {
+        (*alloc_size) += len;
+    }
+
+    return 0;
+}
+
+static int64_t qemu_rbd_get_allocated_file_size(BlockDriverState *bs)
+{
+    BDRVRBDState *s = bs->opaque;

block/rbd.c static int64_t qemu_rbd_getlength(BlockDriverState *bs) · line 1073

Quoted 10 lines
block/rbd.c +10 -0
+    int64_t alloc_size = 0;
+    int r;
+
+    /*
+     * rbd_diff_iterate2(), if the source snapshot name is NULL, invokes
+     * the callback on all allocated regions of the image.
+     */
+    r = rbd_diff_iterate2(s->image, NULL, 0,
+                          bs->total_sectors * BDRV_SECTOR_SIZE, 0, 1,
+                          &rbd_allocated_size_cb, &alloc_size);

Is there any concern that running this on very large images will take a very long time since it needs to iterate through each individual 4MiB (by default) backing object in the image? In libvirt, it only attempts to calculate the actual usage if the fast-diff feature is enabled, and recently it also got a new control to optionally disable the functionality entirely since even with fast-diff it's can be very slow to compute over hundreds of images in a libvirt storage pool.

Quoted 21 lines
block/rbd.c +8 -1
+    if (r < 0) {
+        return r;
+    }
+
+    return alloc_size;
+}
+
 static int coroutine_fn qemu_rbd_co_truncate(BlockDriverState *bs,
                                              int64_t offset,
                                              PreallocMode prealloc,
@@ -1254,6 +1286,7 @@ static BlockDriver bdrv_rbd = {
     .bdrv_get_info          = qemu_rbd_getinfo,
     .create_opts            = &qemu_rbd_create_opts,
     .bdrv_getlength         = qemu_rbd_getlength,
+    .bdrv_get_allocated_file_size = qemu_rbd_get_allocated_file_size,
     .bdrv_co_truncate       = qemu_rbd_co_truncate,
     .protocol_name          = "rbd",
 
--

2.20.1

Signature · 1 line
Jason
#2 lore raw
Quoted 45 lines · Jason Dillaman
Quoted 33 lines · Stefano Garzarella

This patch allows 'qemu-img info' to show the 'disk size' for rbd images. We use the rbd_diff_iterate2() API to calculate the allocated size for the image.

---
 block/rbd.c | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)
block/rbd.c +15 -0
index 0c549c9935..61447bc0cb 100644
@@ -1046,6 +1046,38 @@ static int64_t qemu_rbd_getlength(BlockDriverState *bs)
     return info.size;
 }
 
+static int rbd_allocated_size_cb(uint64_t offset, size_t len, int exists,
+                                 void *arg)
+{
+    int64_t *alloc_size = (int64_t *) arg;
+
+    if (exists) {
+        (*alloc_size) += len;
+    }
+
+    return 0;
+}
+
+static int64_t qemu_rbd_get_allocated_file_size(BlockDriverState *bs)
+{
+    BDRVRBDState *s = bs->opaque;

block/rbd.c static int64_t qemu_rbd_getlength(BlockDriverState *bs) · line 1073

Quoted 10 lines
block/rbd.c +10 -0
+    int64_t alloc_size = 0;
+    int r;
+
+    /*
+     * rbd_diff_iterate2(), if the source snapshot name is NULL, invokes
+     * the callback on all allocated regions of the image.
+     */
+    r = rbd_diff_iterate2(s->image, NULL, 0,
+                          bs->total_sectors * BDRV_SECTOR_SIZE, 0, 1,
+                          &rbd_allocated_size_cb, &alloc_size);
Quoted 8 lines

Is there any concern that running this on very large images will take a very long time since it needs to iterate through each individual 4MiB (by default) backing object in the image? In libvirt, it only attempts to calculate the actual usage if the fast-diff feature is enabled, and recently it also got a new control to optionally disable the functionality entirely since even with fast-diff it's can be very slow to compute over hundreds of images in a libvirt storage pool.

Thank you for pointing that out to me. I'll add check on fast-diff feature on v2.
Since we only have one image here, do you think it would be reasonable to add this feature or is it useless?

Thanks,
Stefano