BeesExtent
BeesExtent::subextent(const BeesExtent &child, uint64_t begin, uint64_t end)
{
+ // A SuperExtent has no single bytenr, so a SubExtent never wraps one.
+ // A range inside one part is that part's child, narrowed. A range
+ // across parts has no btrfs counterpart; build it from a TempExtent
+ // or MaterializedExtent instead.
+ if (child.kind() == BeesTreeExtentKind::super) {
+ const auto super = dynamic_pointer_cast<BeesSuperExtentNode>(child.node());
+ THROW_CHECK0(runtime_error, super);
+ for (const auto &part : super->parts()) {
+ if (begin >= part.m_begin && end <= part.m_end) {
+ const auto child_begin = part.m_child.begin() + (begin - part.m_begin);
+ return subextent(part.m_child.extent(), child_begin, child_begin + (end - begin));
+ }
+ }
+ THROW_ERROR(invalid_argument, "subextent [" << to_hex(begin) << ", " << to_hex(end)
+ << ") spans more than one part of a SuperExtent");
+ }
return BeesExtent(make_shared<BeesSubExtentNode>(child, begin, end));
}
THROW_CHECK2(invalid_argument, end, *child_size, end <= *child_size);
}
// bytenr() delegates to the child, so the child cannot be a
- // SuperExtent (spans multiple bytenrs). Hole is allowed: it has
+ // SuperExtent (spans multiple bytenrs); BeesExtent::subextent()
+ // resolves a single-part range to that part. Hole is allowed: it has
// no bytenr but also does not appear in any bytenr-reading path
// reached by operator<< through a parent SuperExtent (hole and
// sub's print() methods never call bytenr()).
}
// Covers the case that motivated the old sub(super) test: slicing
-// across super part boundaries. SubExtent delegates bytenr() to its
-// child, so sub(super) is forbidden at construction — exercise the
-// same boundary behavior by slicing the super directly.
+// across super part boundaries. A SubExtent across parts has no single
+// bytenr and is rejected, so exercise the same boundary behavior by
+// slicing the super directly.
static void
test_superextent_partial_slice_is_exact_once()
{
SHOULD_THROW(bees_make_dedupeextent(super));
}
+// A SubExtent within one part of a SuperExtent is a SubExtent of that
+// part's child, so bytenr() is the part's.
+static void
+test_subextent_of_super_single_part()
+{
+ const auto a = BeesExtent(make_shared<BeesBtrfsExtentNode>(0xaaaa000, 16384));
+ const auto b = BeesExtent(make_shared<BeesBtrfsExtentNode>(0xbbbb000, 16384));
+ const auto c = BeesExtent(make_shared<BeesBtrfsExtentNode>(0xcccc000, 16384));
+ const auto super = bees_make_superextent({
+ BeesTreeSuperPart { BeesExtentSlice(a, 0, 4096), 0, 4096 },
+ BeesTreeSuperPart { BeesExtentSlice(b, 4096, 12288), 4096, 12288 },
+ BeesTreeSuperPart { BeesExtentSlice(c, 8192, 12288), 12288, 16384 },
+ });
+ // [6144, 10240) lies inside b's part, which maps super 4096 to b 4096.
+ const auto sub = BeesExtent::subextent(super, 6144, 10240);
+ assert(sub.kind() == BeesTreeExtentKind::sub);
+ assert(sub.bytenr() == 0xbbbb000);
+ // Same node as narrowing b directly at the offset the part maps to.
+ ostringstream got, want;
+ got << sub;
+ want << BeesExtent::subextent(b, 6144, 10240);
+ assert(got.str() == want.str());
+ // c's part maps super 12288 to c 8192, so the offset shifts.
+ ostringstream got_c, want_c;
+ got_c << BeesExtent::subextent(super, 13312, 15360);
+ want_c << BeesExtent::subextent(c, 9216, 11264);
+ assert(got_c.str() == want_c.str());
+ // A whole part is still a single part.
+ assert(BeesExtent::subextent(super, 12288, 16384).bytenr() == 0xcccc000);
+ // Crossing a part boundary, even by one byte, has no single bytenr.
+ SHOULD_THROW(BeesExtent::subextent(super, 4095, 8192));
+ SHOULD_THROW(BeesExtent::subextent(super, 8192, 12289));
+}
+
// Hole is allowed as a transform's child: it has no bytenr, but no
// print path through a parent super reaches hole's bytenr either.
// Wrapping hole is a legitimate planner pattern (e.g. sub(hole) for
RUN_A_TEST(test_bytenr_delegates_through_dedupe());
RUN_A_TEST(test_bytenr_delegates_through_nested_transforms());
RUN_A_TEST(test_transforms_reject_super_as_delegate());
+ RUN_A_TEST(test_subextent_of_super_single_part());
RUN_A_TEST(test_sub_accepts_hole_child());
RUN_A_TEST(test_dedupe_accepts_hole_src());
RUN_A_TEST(test_datacow_compatible_both_cow());