diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-04-05 07:49:02 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-04-05 07:49:02 -0600 |
commit | 68eb7a16b9759646619a7d9dec2b62fa9d0c30cf (patch) | |
tree | 8bb4b70337a299f1dacb3c2858210d4bf6bd4b04 /cmd/btrfs-rec/inspect/rebuildtrees | |
parent | b0f290078d531d2dcb5d34e809b0711ce9b6491e (diff) | |
parent | d7dd6dfd7aeb40f06ff4fbe7f906d8feed64b95f (diff) |
Merge branch 'lukeshu/misc'
Diffstat (limited to 'cmd/btrfs-rec/inspect/rebuildtrees')
-rw-r--r-- | cmd/btrfs-rec/inspect/rebuildtrees/rebuild.go | 28 | ||||
-rw-r--r-- | cmd/btrfs-rec/inspect/rebuildtrees/rebuild_wantcb.go | 61 | ||||
-rw-r--r-- | cmd/btrfs-rec/inspect/rebuildtrees/scan.go | 30 |
3 files changed, 65 insertions, 54 deletions
diff --git a/cmd/btrfs-rec/inspect/rebuildtrees/rebuild.go b/cmd/btrfs-rec/inspect/rebuildtrees/rebuild.go index 0d25ac3..427070a 100644 --- a/cmd/btrfs-rec/inspect/rebuildtrees/rebuild.go +++ b/cmd/btrfs-rec/inspect/rebuildtrees/rebuild.go @@ -198,11 +198,13 @@ func (o *rebuilder) processAddedItemQueue(ctx context.Context) error { ctx := dlog.WithField(ctx, "btrfs.inspect.rebuild-trees.rebuild.settle.item", key) tree := o.rebuilt.RebuiltTree(ctx, key.TreeID) - incPtr, ok := tree.RebuiltItems(ctx).Load(key.Key) + incPtr, ok := tree.RebuiltAcquireItems(ctx).Load(key.Key) + tree.RebuiltReleaseItems() if !ok { panic(fmt.Errorf("should not happen: failed to load already-added item: %v", key)) } - excPtr, ok := tree.RebuiltPotentialItems(ctx).Load(key.Key) + excPtr, ok := tree.RebuiltAcquirePotentialItems(ctx).Load(key.Key) + tree.RebuiltReleasePotentialItems() if ok && tree.RebuiltShouldReplace(incPtr.Node, excPtr.Node) { wantKey := wantWithTree{ TreeID: key.TreeID, @@ -517,24 +519,12 @@ func (o *rebuilder) resolveTreeAugments(ctx context.Context, treeID btrfsprim.Ob //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// func (queue *treeAugmentQueue) has(wantKey want) bool { - if queue != nil { - if queue.zero != nil { - if _, ok := queue.zero[wantKey]; ok { - return true - } - } - if queue.single != nil { - if _, ok := queue.single[wantKey]; ok { - return true - } - } - if queue.multi != nil { - if _, ok := queue.multi[wantKey]; ok { - return true - } - } + if queue == nil { + return false } - return false + return (queue.zero != nil && maps.HasKey(queue.zero, wantKey)) || + (queue.single != nil && maps.HasKey(queue.single, wantKey)) || + (queue.multi != nil && maps.HasKey(queue.multi, wantKey)) } func (queue *treeAugmentQueue) store(wantKey want, choices containers.Set[btrfsvol.LogicalAddr]) { diff --git a/cmd/btrfs-rec/inspect/rebuildtrees/rebuild_wantcb.go b/cmd/btrfs-rec/inspect/rebuildtrees/rebuild_wantcb.go index eff2a83..0526e93 100644 --- a/cmd/btrfs-rec/inspect/rebuildtrees/rebuild_wantcb.go +++ b/cmd/btrfs-rec/inspect/rebuildtrees/rebuild_wantcb.go @@ -43,18 +43,22 @@ func (o graphCallbacks) Want(ctx context.Context, reason string, treeID btrfspri } func (o *rebuilder) _want(ctx context.Context, wantKey wantWithTree) (key btrfsprim.Key, ok bool) { - if o.rebuilt.RebuiltTree(ctx, wantKey.TreeID) == nil { + tree := o.rebuilt.RebuiltTree(ctx, wantKey.TreeID) + if tree == nil { o.enqueueRetry(wantKey.TreeID) return btrfsprim.Key{}, false } + tgt := wantKey.Key.Key() + // check if we already have it - tgt := wantKey.Key.Key() - if key, _, ok := o.rebuilt.RebuiltTree(ctx, wantKey.TreeID).RebuiltItems(ctx).Search(func(key btrfsprim.Key, _ btrfsutil.ItemPtr) int { + key, _, ok = tree.RebuiltAcquireItems(ctx).Search(func(key btrfsprim.Key, _ btrfsutil.ItemPtr) int { key.Offset = 0 return tgt.Compare(key) - }); ok { + }) + tree.RebuiltReleaseItems() + if ok { return key, true } @@ -64,15 +68,16 @@ func (o *rebuilder) _want(ctx context.Context, wantKey wantWithTree) (key btrfsp return btrfsprim.Key{}, false } wants := make(containers.Set[btrfsvol.LogicalAddr]) - o.rebuilt.RebuiltTree(ctx, wantKey.TreeID).RebuiltPotentialItems(ctx).Subrange( + tree.RebuiltAcquirePotentialItems(ctx).Subrange( func(k btrfsprim.Key, _ btrfsutil.ItemPtr) int { k.Offset = 0 return tgt.Compare(k) }, func(_ btrfsprim.Key, v btrfsutil.ItemPtr) bool { - wants.InsertFrom(o.rebuilt.RebuiltTree(ctx, wantKey.TreeID).RebuiltLeafToRoots(ctx, v.Node)) + wants.InsertFrom(tree.RebuiltLeafToRoots(ctx, v.Node)) return true }) + tree.RebuiltReleasePotentialItems() o.wantAugment(ctx, wantKey, wants) return btrfsprim.Key{}, false } @@ -93,15 +98,19 @@ func (o graphCallbacks) WantOff(ctx context.Context, reason string, treeID btrfs } func (o *rebuilder) _wantOff(ctx context.Context, wantKey wantWithTree) (ok bool) { - if o.rebuilt.RebuiltTree(ctx, wantKey.TreeID) == nil { + tree := o.rebuilt.RebuiltTree(ctx, wantKey.TreeID) + if tree == nil { o.enqueueRetry(wantKey.TreeID) return false } + tgt := wantKey.Key.Key() + // check if we already have it - tgt := wantKey.Key.Key() - if _, ok := o.rebuilt.RebuiltTree(ctx, wantKey.TreeID).RebuiltItems(ctx).Load(tgt); ok { + _, ok = tree.RebuiltAcquireItems(ctx).Load(tgt) + tree.RebuiltReleaseItems() + if ok { return true } @@ -111,12 +120,13 @@ func (o *rebuilder) _wantOff(ctx context.Context, wantKey wantWithTree) (ok bool return false } wants := make(containers.Set[btrfsvol.LogicalAddr]) - o.rebuilt.RebuiltTree(ctx, wantKey.TreeID).RebuiltPotentialItems(ctx).Subrange( + tree.RebuiltAcquirePotentialItems(ctx).Subrange( func(k btrfsprim.Key, _ btrfsutil.ItemPtr) int { return tgt.Compare(k) }, func(_ btrfsprim.Key, v btrfsutil.ItemPtr) bool { - wants.InsertFrom(o.rebuilt.RebuiltTree(ctx, wantKey.TreeID).RebuiltLeafToRoots(ctx, v.Node)) + wants.InsertFrom(tree.RebuiltLeafToRoots(ctx, v.Node)) return true }) + tree.RebuiltReleasePotentialItems() o.wantAugment(ctx, wantKey, wants) return false } @@ -134,16 +144,18 @@ func (o graphCallbacks) WantDirIndex(ctx context.Context, reason string, treeID } ctx = withWant(ctx, logFieldItemWant, reason, wantKey) - if o.rebuilt.RebuiltTree(ctx, treeID) == nil { + tree := o.rebuilt.RebuiltTree(ctx, treeID) + if tree == nil { o.enqueueRetry(treeID) return } + tgt := wantKey.Key.Key() + // check if we already have it - tgt := wantKey.Key.Key() found := false - o.rebuilt.RebuiltTree(ctx, treeID).RebuiltItems(ctx).Subrange( + tree.RebuiltAcquireItems(ctx).Subrange( func(key btrfsprim.Key, _ btrfsutil.ItemPtr) int { key.Offset = 0 return tgt.Compare(key) @@ -154,6 +166,7 @@ func (o graphCallbacks) WantDirIndex(ctx context.Context, reason string, treeID } return !found }) + tree.RebuiltReleaseItems() if found { return } @@ -164,17 +177,18 @@ func (o graphCallbacks) WantDirIndex(ctx context.Context, reason string, treeID return } wants := make(containers.Set[btrfsvol.LogicalAddr]) - o.rebuilt.RebuiltTree(ctx, treeID).RebuiltPotentialItems(ctx).Subrange( + tree.RebuiltAcquirePotentialItems(ctx).Subrange( func(key btrfsprim.Key, _ btrfsutil.ItemPtr) int { key.Offset = 0 return tgt.Compare(key) }, func(_ btrfsprim.Key, ptr btrfsutil.ItemPtr) bool { if itemName, ok := o.scan.Names[ptr]; ok && bytes.Equal(itemName, name) { - wants.InsertFrom(o.rebuilt.RebuiltTree(ctx, treeID).RebuiltLeafToRoots(ctx, ptr.Node)) + wants.InsertFrom(tree.RebuiltLeafToRoots(ctx, ptr.Node)) } return true }) + tree.RebuiltReleasePotentialItems() o.wantAugment(ctx, wantKey, wants) } @@ -259,7 +273,8 @@ func (o graphCallbacks) _wantRange( ctx = withWant(ctx, logFieldItemWant, reason, wantKey) wantKey.Key.OffsetType = offsetRange - if o.rebuilt.RebuiltTree(ctx, treeID) == nil { + tree := o.rebuilt.RebuiltTree(ctx, treeID) + if tree == nil { o.enqueueRetry(treeID) return } @@ -275,7 +290,7 @@ func (o graphCallbacks) _wantRange( }) o._walkRange( ctx, - o.rebuilt.RebuiltTree(ctx, treeID).RebuiltItems(ctx), + tree.RebuiltAcquireItems(ctx), treeID, objID, typ, beg, end, func(runKey btrfsprim.Key, _ btrfsutil.ItemPtr, runBeg, runEnd uint64) { var overlappingGaps []*containers.RBNode[gap] @@ -315,12 +330,13 @@ func (o graphCallbacks) _wantRange( }) } }) + tree.RebuiltReleaseItems() // Step 2: Fill each gap. if gaps.Len() == 0 { return } - potentialItems := o.rebuilt.RebuiltTree(ctx, treeID).RebuiltPotentialItems(ctx) + potentialItems := tree.RebuiltAcquirePotentialItems(ctx) gaps.Range(func(rbNode *containers.RBNode[gap]) bool { gap := rbNode.Value last := gap.Beg @@ -340,7 +356,7 @@ func (o graphCallbacks) _wantRange( wantKey.Key.OffsetLow = gap.Beg wantKey.Key.OffsetHigh = gap.End wantCtx := withWant(ctx, logFieldItemWant, reason, wantKey) - o.wantAugment(wantCtx, wantKey, o.rebuilt.RebuiltTree(wantCtx, treeID).RebuiltLeafToRoots(wantCtx, v.Node)) + o.wantAugment(wantCtx, wantKey, tree.RebuiltLeafToRoots(wantCtx, v.Node)) last = runEnd }) if last < gap.End { @@ -352,6 +368,7 @@ func (o graphCallbacks) _wantRange( } return true }) + tree.RebuiltReleasePotentialItems() } // WantCSum implements btrfscheck.GraphCallbacks. @@ -372,7 +389,9 @@ func (o graphCallbacks) WantCSum(ctx context.Context, reason string, inodeTree, o.enqueueRetry(inodeTree) return } - inodePtr, ok := o.rebuilt.RebuiltTree(inodeCtx, inodeTree).RebuiltItems(inodeCtx).Load(inodeWant.Key.Key()) + tree := o.rebuilt.RebuiltTree(inodeCtx, inodeTree) + inodePtr, ok := tree.RebuiltAcquireItems(inodeCtx).Load(inodeWant.Key.Key()) + tree.RebuiltReleaseItems() if !ok { panic(fmt.Errorf("should not happen: could not load key: %v", inodeWant)) } diff --git a/cmd/btrfs-rec/inspect/rebuildtrees/scan.go b/cmd/btrfs-rec/inspect/rebuildtrees/scan.go index a6f9c7a..f266dab 100644 --- a/cmd/btrfs-rec/inspect/rebuildtrees/scan.go +++ b/cmd/btrfs-rec/inspect/rebuildtrees/scan.go @@ -41,31 +41,35 @@ type ScanDevicesResult struct { Sizes map[btrfsutil.ItemPtr]SizeAndErr // EXTENT_CSUM and EXTENT_DATA } -func ScanDevices(ctx context.Context, fs *btrfs.FS, nodeList []btrfsvol.LogicalAddr) (ScanDevicesResult, error) { +func ScanDevices(_ctx context.Context, fs *btrfs.FS, nodeList []btrfsvol.LogicalAddr) (ScanDevicesResult, error) { + // read-superblock ///////////////////////////////////////////////////////////// + ctx := dlog.WithField(_ctx, "btrfs.inspect.rebuild-trees.read.substep", "read-superblock") dlog.Info(ctx, "Reading superblock...") sb, err := fs.Superblock() if err != nil { return ScanDevicesResult{}, err } - dlog.Infof(ctx, "Reading node data from FS...") - - var stats textui.Portion[int] - stats.D = len(nodeList) - progressWriter := textui.NewProgress[textui.Portion[int]]( - dlog.WithField(ctx, "btrfs.inspect.rebuild-trees.read.substep", "read-nodes"), - dlog.LogLevelInfo, textui.Tunable(1*time.Second)) - + // read-roots ////////////////////////////////////////////////////////////////// + ctx = dlog.WithField(_ctx, "btrfs.inspect.rebuild-trees.read.substep", "read-roots") ret := ScanDevicesResult{ Superblock: *sb, Graph: btrfsutil.NewGraph(ctx, *sb), - Flags: make(map[btrfsutil.ItemPtr]FlagsAndErr), Names: make(map[btrfsutil.ItemPtr][]byte), Sizes: make(map[btrfsutil.ItemPtr]SizeAndErr), } + // read-nodes ////////////////////////////////////////////////////////////////// + ctx = dlog.WithField(_ctx, "btrfs.inspect.rebuild-trees.read.substep", "read-nodes") + dlog.Infof(ctx, "Reading node data from FS...") + var stats textui.Portion[int] + stats.D = len(nodeList) + progressWriter := textui.NewProgress[textui.Portion[int]]( + ctx, + dlog.LogLevelInfo, + textui.Tunable(1*time.Second)) progressWriter.Set(stats) for _, laddr := range nodeList { if err := ctx.Err(); err != nil { @@ -78,11 +82,8 @@ func ScanDevices(ctx context.Context, fs *btrfs.FS, nodeList []btrfsvol.LogicalA fs.ReleaseNode(node) return ScanDevicesResult{}, err } - ret.insertNode(node) - fs.ReleaseNode(node) - stats.N++ progressWriter.Set(stats) } @@ -92,7 +93,8 @@ func ScanDevices(ctx context.Context, fs *btrfs.FS, nodeList []btrfsvol.LogicalA progressWriter.Done() dlog.Info(ctx, "... done reading node data") - ctx = dlog.WithField(ctx, "btrfs.inspect.rebuild-trees.read.substep", "check") + // check /////////////////////////////////////////////////////////////////////// + ctx = dlog.WithField(_ctx, "btrfs.inspect.rebuild-trees.read.substep", "check") if err := ret.Graph.FinalCheck(ctx, fs); err != nil { return ScanDevicesResult{}, err } |