diff options
-rw-r--r-- | lib/btrfs/btrfsitem/item_extentcsum.go | 53 | ||||
-rw-r--r-- | lib/btrfs/btrfsitem/items.go | 2 | ||||
-rw-r--r-- | lib/btrfsprogs/btrfsinspect/print_tree.go | 17 | ||||
-rw-r--r-- | lib/btrfsprogs/btrfsutil/csums.go | 12 |
4 files changed, 18 insertions, 66 deletions
diff --git a/lib/btrfs/btrfsitem/item_extentcsum.go b/lib/btrfs/btrfsitem/item_extentcsum.go index 18ee572..bcfe334 100644 --- a/lib/btrfs/btrfsitem/item_extentcsum.go +++ b/lib/btrfs/btrfsitem/item_extentcsum.go @@ -5,73 +5,30 @@ package btrfsitem import ( - "encoding/hex" "fmt" - "io" - - "git.lukeshu.com/go/lowmemjson" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfssum" + "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" ) // key.objectid = BTRFS_EXTENT_CSUM_OBJECTID // key.offset = laddr of checksummed region type ExtentCSum struct { // EXTENT_CSUM=128 - ChecksumSize int // Checksum of each sector starting at key.offset - Sums []btrfssum.CSum + btrfssum.SumRun[btrfsvol.LogicalAddr] } func (o *ExtentCSum) UnmarshalBinary(dat []byte) (int, error) { if o.ChecksumSize == 0 { return 0, fmt.Errorf(".ChecksumSize must be set") } - for len(dat) >= o.ChecksumSize { - var csum btrfssum.CSum - copy(csum[:], dat[:o.ChecksumSize]) - dat = dat[o.ChecksumSize:] - o.Sums = append(o.Sums, csum) - } - return len(o.Sums) * o.ChecksumSize, nil + o.Sums = btrfssum.ShortSum(dat) + return len(dat), nil } func (o ExtentCSum) MarshalBinary() ([]byte, error) { if o.ChecksumSize == 0 { return nil, fmt.Errorf(".ChecksumSize must be set") } - var dat []byte - for _, csum := range o.Sums { - dat = append(dat, csum[:o.ChecksumSize]...) - } - return dat, nil -} - -var ( - _ lowmemjson.Encodable = ExtentCSum{} -) - -func (o ExtentCSum) EncodeJSON(w io.Writer) error { - if _, err := fmt.Fprintf(w, `{"ChecksumSize":%d,"Sums":[`, o.ChecksumSize); err != nil { - return err - } - for i, sum := range o.Sums { - if i > 0 { - if _, err := w.Write([]byte(",")); err != nil { - return err - } - } - if _, err := w.Write([]byte(`"`)); err != nil { - return err - } - if _, err := hex.NewEncoder(w).Write(sum[:o.ChecksumSize]); err != nil { - return err - } - if _, err := w.Write([]byte(`"`)); err != nil { - return err - } - } - if _, err := w.Write([]byte(`]}`)); err != nil { - return err - } - return nil + return []byte(o.Sums), nil } diff --git a/lib/btrfs/btrfsitem/items.go b/lib/btrfs/btrfsitem/items.go index 769a0a8..35d8b4f 100644 --- a/lib/btrfs/btrfsitem/items.go +++ b/lib/btrfs/btrfsitem/items.go @@ -10,6 +10,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/binstruct" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfssum" + "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/internal" ) @@ -61,6 +62,7 @@ func UnmarshalItem(key internal.Key, csumType btrfssum.CSumType, dat []byte) Ite retPtr := reflect.New(gotyp) if csums, ok := retPtr.Interface().(*ExtentCSum); ok { csums.ChecksumSize = csumType.Size() + csums.Addr = btrfsvol.LogicalAddr(key.Offset) } n, err := binstruct.Unmarshal(dat, retPtr.Interface()) if err != nil { diff --git a/lib/btrfsprogs/btrfsinspect/print_tree.go b/lib/btrfsprogs/btrfsinspect/print_tree.go index d7eb6f3..4dd03c4 100644 --- a/lib/btrfsprogs/btrfsinspect/print_tree.go +++ b/lib/btrfsprogs/btrfsinspect/print_tree.go @@ -203,25 +203,22 @@ func printTree(ctx context.Context, out io.Writer, fs *btrfs.FS, treeID btrfs.Ob //case btrfsitem.SHARED_DATA_REF_KEY: // // TODO case btrfsitem.ExtentCSum: - sb, _ := fs.Superblock() - sectorSize := btrfsvol.AddrDelta(sb.SectorSize) - start := btrfsvol.LogicalAddr(item.Key.Offset) - itemSize := btrfsvol.AddrDelta(len(body.Sums)) * sectorSize fmt.Fprintf(out, "\t\trange start %d end %d length %d", - start, start.Add(itemSize), itemSize) + start, start.Add(body.Size()), body.Size()) sumsPerLine := slices.Max(1, len(btrfssum.CSum{})/body.ChecksumSize/2) - pos := start - for i, sum := range body.Sums { + i := 0 + _ = body.Walk(ctx, func(pos btrfsvol.LogicalAddr, sum btrfssum.ShortSum) error { if i%sumsPerLine == 0 { fmt.Fprintf(out, "\n\t\t") } else { fmt.Fprintf(out, " ") } - fmt.Fprintf(out, "[%d] 0x%s", pos, sum.Fmt(sb.ChecksumType)) - pos = pos.Add(sectorSize) - } + fmt.Fprintf(out, "[%d] 0x%x", pos, sum) + i++ + return nil + }) fmt.Fprintf(out, "\n") case btrfsitem.FileExtent: fmt.Fprintf(out, "\t\tgeneration %v type %v\n", diff --git a/lib/btrfsprogs/btrfsutil/csums.go b/lib/btrfsprogs/btrfsutil/csums.go index 274be68..3c3d85f 100644 --- a/lib/btrfsprogs/btrfsutil/csums.go +++ b/lib/btrfsprogs/btrfsutil/csums.go @@ -37,7 +37,7 @@ func ChecksumQualifiedPhysical(fs *btrfs.FS, alg btrfssum.CSumType, paddr btrfsv return ChecksumPhysical(dev, alg, paddr.Addr) } -func LookupCSum(fs btrfs.Trees, alg btrfssum.CSumType, laddr btrfsvol.LogicalAddr) (map[btrfsvol.LogicalAddr]btrfssum.CSum, error) { +func LookupCSum(fs btrfs.Trees, alg btrfssum.CSumType, laddr btrfsvol.LogicalAddr) (btrfssum.SumRun[btrfsvol.LogicalAddr], error) { item, err := fs.TreeSearch(btrfs.CSUM_TREE_OBJECTID, func(key btrfs.Key, size uint32) int { itemBeg := btrfsvol.LogicalAddr(key.ObjectID) numSums := int64(size) / int64(alg.Size()) @@ -52,15 +52,11 @@ func LookupCSum(fs btrfs.Trees, alg btrfssum.CSumType, laddr btrfsvol.LogicalAdd } }) if err != nil { - return nil, err + return btrfssum.SumRun[btrfsvol.LogicalAddr]{}, err } body, ok := item.Body.(btrfsitem.ExtentCSum) if !ok { - return nil, fmt.Errorf("item body is %T not ExtentCSum", item.Body) + return btrfssum.SumRun[btrfsvol.LogicalAddr]{}, fmt.Errorf("item body is %T not ExtentCSum", item.Body) } - ret := make(map[btrfsvol.LogicalAddr]btrfssum.CSum, len(body.Sums)) - for i, sum := range body.Sums { - ret[btrfsvol.LogicalAddr(item.Key.ObjectID)+(btrfsvol.LogicalAddr(i)*btrfssum.BlockSize)] = sum - } - return ret, nil + return body.SumRun, nil } |