summaryrefslogtreecommitdiff
path: root/cmd/btrfs-fsck/pass2.go
blob: e71cd8425fd3c5dcd5825c60f7b3e55ff2ce6a7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (C) 2022  Luke Shumaker <lukeshu@lukeshu.com>
//
// SPDX-License-Identifier: GPL-2.0-or-later

package main

import (
	"context"
	"fmt"

	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs"
	"git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol"
	"git.lukeshu.com/btrfs-progs-ng/lib/btrfsprogs/btrfsutil"
	"git.lukeshu.com/btrfs-progs-ng/lib/diskio"
)

func pass2(ctx context.Context, fs *btrfs.FS, foundNodes map[btrfsvol.LogicalAddr]struct{}) {
	fmt.Printf("\nPass 2: orphaned nodes\n")

	visitedNodes := make(map[btrfsvol.LogicalAddr]struct{})
	btrfsutil.WalkAllTrees(ctx, fs, btrfsutil.WalkAllTreesHandler{
		TreeWalkHandler: btrfs.TreeWalkHandler{
			Node: func(path btrfs.TreePath, node *diskio.Ref[btrfsvol.LogicalAddr, btrfs.Node]) error {
				visitedNodes[node.Addr] = struct{}{}
				return nil
			},
		},
		Err: func(err *btrfsutil.WalkError) {
			fmt.Printf("Pass 2: walk FS: error: %v\n", err)
		},
	})

	orphanedNodes := make(map[btrfsvol.LogicalAddr]int)
	for foundNode := range foundNodes {
		if _, visited := visitedNodes[foundNode]; !visited {
			orphanedNodes[foundNode] = 0
		}
	}

	orphanedRoots := make(map[btrfsvol.LogicalAddr]struct{}, len(orphanedNodes))
	for node := range orphanedNodes {
		orphanedRoots[node] = struct{}{}
	}
	/*
		for potentialRoot := range orphanedRoots {
			if err := fs.TreeWalk(potentialRoot, btrfs.TreeWalkHandler{
				Node: func(path btrfs.TreePath, _ *diskio.Ref[btrfsvol.LogicalAddr, btrfs.Node], _ error) error {
					nodeAddr := path[len(path)-1].NodeAddr
					if nodeAddr != potentialRoot {
						delete(orphanedRoots, nodeAddr)
					}
					orphanedNodes[nodeAddr] = orphanedNodes[nodeAddr] + 1
					visitCnt := orphanedNodes[nodeAddr]
					if visitCnt > 1 {
						return iofs.SkipDir
					}
					return nil
				},
			}); err != nil {
				fmt.Printf("Pass 2: walk orphans: error: %v\n", err)
			}
		}

		for _, node := range maps.SortedKeys(orphanedRoots) {
			fmt.Printf("Pass 2: orphaned root: %v\n", node)
		}
	*/
}