]> git.hungrycats.org Git - bees/commitdiff
extent-tree: allow a SubExtent within one part of a SuperExtent
authorZygo Blaxell <bees@furryterror.org>
Sat, 10 Oct 2026 20:15:12 +0000 (16:15 -0400)
committerZygo Blaxell <bees@furryterror.org>
Sun, 11 Oct 2026 00:48:00 +0000 (20:48 -0400)
A SubExtent delegates bytenr() to its child, so it was forbidden over a
SuperExtent, which has no single bytenr.  But a range that lies inside
one part of the SuperExtent does have one: it is that part's child,
narrowed.  BeesExtent::subextent() now resolves such a range to a
SubExtent of the part's child at the offset the part maps to, so a
SuperExtent can be decomposed into its constituent SubExtents.

A range that crosses parts still throws.  It has no btrfs counterpart;
data drawn from several extents has to be copied into one, which is
what a TempExtent or MaterializedExtent represents.

Assisted-by: Claude-Code:claude-opus-5-5
src/bees-extent-tree.cc
test/test-bees-extent-tree.cc

index e78192a3eae3413a4ebcb36109b831f0c9282cbd..493d895688f97c37f0a06f14ead95408ab992cc3 100644 (file)
@@ -484,6 +484,22 @@ BeesExtent::from_pre_copy(const BeesExtent &src)
 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));
 }
 
@@ -1827,7 +1843,8 @@ BeesSubExtentNode::BeesSubExtentNode(BeesExtent child, uint64_t begin, uint64_t
                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()).
index 52bdf801b3a080eb1ef5ad5fb8421c4c02d64e89..2a10bc0084e4994941954b220b29c93d3684cb9b 100644 (file)
@@ -453,9 +453,9 @@ test_superextent_covering_visitors_are_exact_once()
 }
 
 // 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()
 {
@@ -1499,6 +1499,40 @@ test_transforms_reject_super_as_delegate()
        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
@@ -1639,6 +1673,7 @@ main()
        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());