---
filters/partition/partition.c | 52 +++++++++++++++++++++++++++++++++++
1 file changed, 52 insertions(+)
diff --git a/filters/partition/partition.c b/filters/partition/partition.c
index c1fe7a1..c8fcc7f 100644
--- a/filters/partition/partition.c
+++ b/filters/partition/partition.c
@@ -38,6 +38,7 @@
#include <stdint.h>
#include <string.h>
#include <inttypes.h>
+#include <assert.h>
#include <nbdkit-filter.h>
@@ -222,6 +223,56 @@ partition_zero (struct nbdkit_next_ops *next_ops, void *nxdata,
return next_ops->zero (nxdata, count, offs + h->offset, flags, err);
}
+/* Extents. */
+struct subtract_offset_data {
+ struct handle *h;
+ struct nbdkit_extents_map *extents_map;
+};
+
+static int
+subtract_offset (uint64_t offs, uint64_t length, uint32_t type,
+ void *dv)
+{
+ struct subtract_offset_data *data = dv;
+
+ assert (offs >= data->h->offset);
+ offs -= data->h->offset;
+ return nbdkit_extent_add (data->extents_map, offs, length, type);
+}
+
+static int
+partition_extents (struct nbdkit_next_ops *next_ops, void *nxdata,
+ void *handle, uint32_t count, uint64_t offs, uint32_t flags,
+ struct nbdkit_extents_map *extents_map,
+ int *err)
+{
+ struct handle *h = handle;
+ struct nbdkit_extents_map *map2;
+ struct subtract_offset_data data;
+
+ map2 = nbdkit_extents_new ();
+ if (map2 == NULL)
+ return -1;
+ if (next_ops->extents (nxdata, count, offs + h->offset,
+ flags, map2, err) == -1) {
+ nbdkit_extents_free (map2);
+ return -1;
+ }
+
+ /* Transform offsets in map2, return result in extents_map. */
+ data.h = h;
+ data.extents_map = extents_map;
+ if (nbdkit_extents_foreach (map2, subtract_offset, &data,
+ NBDKIT_EXTENTS_FOREACH_FLAG_RANGE,
+ h->offset, h->range) == -1) {
+ nbdkit_extents_free (map2);
+ return -1;
+ }
+ nbdkit_extents_free (map2);
+
+ return 0;
+}
+
static struct nbdkit_filter filter = {
.name = "partition",
.longname = "nbdkit partition filter",
@@ -237,6 +288,7 @@ static struct nbdkit_filter filter = {
.pwrite = partition_pwrite,
.trim = partition_trim,
.zero = partition_zero,
+ .extents = partition_extents,
};
NBDKIT_REGISTER_FILTER(filter)
--
2.20.1