From 5627aaea2c15a6fa8cca202614119f72972be37f Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Fri, 15 Jul 2022 14:36:47 -0600 Subject: tidy up --- cmd/btrfs-fsck/pass1.go | 2 +- lib/btrfs/btrfsitem/item_chunk.go | 6 +++++- lib/btrfs/btrfsitem/item_devextent.go | 1 - lib/btrfs/btrfsvol/addr.go | 7 +++++++ lib/btrfs/btrfsvol/chunk.go | 19 ++++++++----------- lib/btrfs/btrfsvol/devext.go | 14 +++++++------- lib/btrfs/btrfsvol/lvm.go | 16 ++++------------ lib/btrfs/io2_lv.go | 4 ---- lib/btrfsprogs/btrfsinspect/print_addrspace.go | 4 ++-- lib/btrfsprogs/btrfsinspect/recoverchunks.go | 15 +++++++-------- lib/containers/optional.go | 10 ++++++++++ 11 files changed, 51 insertions(+), 47 deletions(-) create mode 100644 lib/containers/optional.go diff --git a/cmd/btrfs-fsck/pass1.go b/cmd/btrfs-fsck/pass1.go index 4defd02..9c1d845 100644 --- a/cmd/btrfs-fsck/pass1.go +++ b/cmd/btrfs-fsck/pass1.go @@ -114,7 +114,7 @@ func pass1WriteReconstructedChunks(ctx context.Context, fs *btrfs.FS) { Size: mapping.Size, Owner: btrfs.EXTENT_TREE_OBJECTID, StripeLen: 65536, // ??? - Type: *mapping.Flags, + Type: mapping.Flags.Val, IOOptimalAlign: superblock.DevItem.IOOptimalAlign, IOOptimalWidth: superblock.DevItem.IOOptimalWidth, IOMinSize: superblock.DevItem.IOMinSize, diff --git a/lib/btrfs/btrfsitem/item_chunk.go b/lib/btrfs/btrfsitem/item_chunk.go index 754d650..fe2637d 100644 --- a/lib/btrfs/btrfsitem/item_chunk.go +++ b/lib/btrfs/btrfsitem/item_chunk.go @@ -8,6 +8,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/binstruct" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsvol" "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/internal" + "git.lukeshu.com/btrfs-progs-ng/lib/containers" ) // Maps logical address to physical. @@ -50,7 +51,10 @@ func (chunk Chunk) Mappings(key internal.Key) []btrfsvol.Mapping { }, Size: chunk.Head.Size, SizeLocked: true, - Flags: &chunk.Head.Type, + Flags: containers.Optional[btrfsvol.BlockGroupFlags]{ + OK: true, + Val: chunk.Head.Type, + }, }) } return ret diff --git a/lib/btrfs/btrfsitem/item_devextent.go b/lib/btrfs/btrfsitem/item_devextent.go index 8eca935..4f26b27 100644 --- a/lib/btrfs/btrfsitem/item_devextent.go +++ b/lib/btrfs/btrfsitem/item_devextent.go @@ -30,6 +30,5 @@ func (devext DevExtent) Mapping(key internal.Key) btrfsvol.Mapping { }, Size: devext.Length, SizeLocked: true, - Flags: nil, } } diff --git a/lib/btrfs/btrfsvol/addr.go b/lib/btrfs/btrfsvol/addr.go index 640a3e8..76b531d 100644 --- a/lib/btrfs/btrfsvol/addr.go +++ b/lib/btrfs/btrfsvol/addr.go @@ -43,6 +43,13 @@ type QualifiedPhysicalAddr struct { Addr PhysicalAddr } +func (a QualifiedPhysicalAddr) Add(b AddrDelta) QualifiedPhysicalAddr { + return QualifiedPhysicalAddr{ + Dev: a.Dev, + Addr: a.Addr.Add(b), + } +} + func (a QualifiedPhysicalAddr) Cmp(b QualifiedPhysicalAddr) int { if d := int(a.Dev - b.Dev); d != 0 { return d diff --git a/lib/btrfs/btrfsvol/chunk.go b/lib/btrfs/btrfsvol/chunk.go index 9c77a49..8a2d439 100644 --- a/lib/btrfs/btrfsvol/chunk.go +++ b/lib/btrfs/btrfsvol/chunk.go @@ -8,6 +8,7 @@ import ( "fmt" "sort" + "git.lukeshu.com/btrfs-progs-ng/lib/containers" "git.lukeshu.com/btrfs-progs-ng/lib/maps" "git.lukeshu.com/btrfs-progs-ng/lib/slices" ) @@ -18,7 +19,7 @@ type chunkMapping struct { PAddrs []QualifiedPhysicalAddr Size AddrDelta SizeLocked bool - Flags *BlockGroupFlags + Flags containers.Optional[BlockGroupFlags] } type ChunkMapping = chunkMapping @@ -73,10 +74,7 @@ func (a chunkMapping) union(rest ...chunkMapping) (chunkMapping, error) { for _, chunk := range chunks { offsetWithinRet := chunk.LAddr.Sub(ret.LAddr) for _, stripe := range chunk.PAddrs { - paddrs[QualifiedPhysicalAddr{ - Dev: stripe.Dev, - Addr: stripe.Addr.Add(-offsetWithinRet), - }] = struct{}{} + paddrs[stripe.Add(-offsetWithinRet)] = struct{}{} } } ret.PAddrs = maps.Keys(paddrs) @@ -85,15 +83,14 @@ func (a chunkMapping) union(rest ...chunkMapping) (chunkMapping, error) { }) // figure out the flags (.Flags) for _, chunk := range chunks { - if chunk.Flags == nil { + if !chunk.Flags.OK { continue } - if ret.Flags == nil { - val := *chunk.Flags - ret.Flags = &val + if !ret.Flags.OK { + ret.Flags = chunk.Flags } - if *ret.Flags != *chunk.Flags { - return ret, fmt.Errorf("mismatch flags: %v != %v", *ret.Flags, *chunk.Flags) + if ret.Flags != chunk.Flags { + return ret, fmt.Errorf("mismatch flags: %v != %v", ret.Flags.Val, chunk.Flags.Val) } } // done diff --git a/lib/btrfs/btrfsvol/devext.go b/lib/btrfs/btrfsvol/devext.go index 83ece99..e8e5446 100644 --- a/lib/btrfs/btrfsvol/devext.go +++ b/lib/btrfs/btrfsvol/devext.go @@ -7,6 +7,7 @@ package btrfsvol import ( "fmt" + "git.lukeshu.com/btrfs-progs-ng/lib/containers" "git.lukeshu.com/btrfs-progs-ng/lib/slices" ) @@ -16,7 +17,7 @@ type devextMapping struct { LAddr LogicalAddr Size AddrDelta SizeLocked bool - Flags *BlockGroupFlags + Flags containers.Optional[BlockGroupFlags] } // return -1 if 'a' is wholly to the left of 'b' @@ -77,15 +78,14 @@ func (a devextMapping) union(rest ...devextMapping) (devextMapping, error) { } // figure out the flags (.Flags) for _, ext := range exts { - if ext.Flags == nil { + if !ext.Flags.OK { continue } - if ret.Flags == nil { - val := *ext.Flags - ret.Flags = &val + if !ret.Flags.OK { + ret.Flags = ext.Flags } - if *ret.Flags != *ext.Flags { - return ret, fmt.Errorf("mismatch flags: %v != %v", *ret.Flags, *ext.Flags) + if ret.Flags != ext.Flags { + return ret, fmt.Errorf("mismatch flags: %v != %v", ret.Flags.Val, ext.Flags.Val) } } // done diff --git a/lib/btrfs/btrfsvol/lvm.go b/lib/btrfs/btrfsvol/lvm.go index 149c8cd..9b16e10 100644 --- a/lib/btrfs/btrfsvol/lvm.go +++ b/lib/btrfs/btrfsvol/lvm.go @@ -113,8 +113,8 @@ type Mapping struct { LAddr LogicalAddr PAddr QualifiedPhysicalAddr Size AddrDelta - SizeLocked bool `json:",omitempty"` - Flags *BlockGroupFlags `json:",omitempty"` + SizeLocked bool `json:",omitempty"` + Flags containers.Optional[BlockGroupFlags] `json:",omitempty"` } func (lv *LogicalVolume[PhysicalVolume]) AddMapping(m Mapping) error { @@ -232,17 +232,12 @@ func (lv *LogicalVolume[PhysicalVolume]) Mappings() []Mapping { var ret []Mapping _ = lv.logical2physical.Walk(func(node *containers.RBNode[chunkMapping]) error { chunk := node.Value - var flags *BlockGroupFlags - if chunk.Flags != nil { - val := *chunk.Flags - flags = &val - } for _, slice := range chunk.PAddrs { ret = append(ret, Mapping{ LAddr: chunk.LAddr, PAddr: slice, Size: chunk.Size, - Flags: flags, + Flags: chunk.Flags, }) } return nil @@ -264,10 +259,7 @@ func (lv *LogicalVolume[PhysicalVolume]) Resolve(laddr LogicalAddr) (paddrs map[ paddrs = make(map[QualifiedPhysicalAddr]struct{}) maxlen = chunk.Size - offsetWithinChunk for _, stripe := range chunk.PAddrs { - paddrs[QualifiedPhysicalAddr{ - Dev: stripe.Dev, - Addr: stripe.Addr.Add(offsetWithinChunk), - }] = struct{}{} + paddrs[stripe.Add(offsetWithinChunk)] = struct{}{} } return paddrs, maxlen diff --git a/lib/btrfs/io2_lv.go b/lib/btrfs/io2_lv.go index 9316e27..facd88c 100644 --- a/lib/btrfs/io2_lv.go +++ b/lib/btrfs/io2_lv.go @@ -68,10 +68,6 @@ func (fs *FS) WriteAt(p []byte, off btrfsvol.LogicalAddr) (int, error) { return fs.LV.WriteAt(p, off) } -func (fs *FS) Resolve(laddr btrfsvol.LogicalAddr) (paddrs map[btrfsvol.QualifiedPhysicalAddr]struct{}, maxlen btrfsvol.AddrDelta) { - return fs.LV.Resolve(laddr) -} - func (fs *FS) Superblocks() ([]*diskio.Ref[btrfsvol.PhysicalAddr, Superblock], error) { if fs.cacheSuperblocks != nil { return fs.cacheSuperblocks, nil diff --git a/lib/btrfsprogs/btrfsinspect/print_addrspace.go b/lib/btrfsprogs/btrfsinspect/print_addrspace.go index d291b7c..7436d5b 100644 --- a/lib/btrfsprogs/btrfsinspect/print_addrspace.go +++ b/lib/btrfsprogs/btrfsinspect/print_addrspace.go @@ -26,12 +26,12 @@ func PrintLogicalSpace(out io.Writer, fs *btrfs.FS) { sumHole += size } if mapping.LAddr != prevBeg { - if mapping.Flags == nil { + if !mapping.Flags.OK { fmt.Fprintf(out, "chunk laddr=%v size=%v flags=(missing)\n", mapping.LAddr, mapping.Size) } else { fmt.Fprintf(out, "chunk laddr=%v size=%v flags=%v\n", - mapping.LAddr, mapping.Size, *mapping.Flags) + mapping.LAddr, mapping.Size, mapping.Flags.Val) } } fmt.Fprintf(out, "\tstripe dev_id=%v paddr=%v\n", diff --git a/lib/btrfsprogs/btrfsinspect/recoverchunks.go b/lib/btrfsprogs/btrfsinspect/recoverchunks.go index 25794be..012049c 100644 --- a/lib/btrfsprogs/btrfsinspect/recoverchunks.go +++ b/lib/btrfsprogs/btrfsinspect/recoverchunks.go @@ -14,6 +14,7 @@ import ( "git.lukeshu.com/btrfs-progs-ng/lib/btrfs/btrfsitem" "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/containers" "git.lukeshu.com/btrfs-progs-ng/lib/diskio" "git.lukeshu.com/btrfs-progs-ng/lib/maps" ) @@ -90,7 +91,6 @@ func (found ScanOneDevResult) AddToLV(ctx context.Context, fs *btrfs.FS, dev *bt }, Size: btrfsvol.AddrDelta(sb.NodeSize), SizeLocked: false, - Flags: nil, }); err != nil { dlog.Errorf(ctx, "... dev[%q] error: adding node ident: %v", dev.Name(), err) @@ -132,16 +132,15 @@ func (found ScanOneDevResult) AddToLV(ctx context.Context, fs *btrfs.FS, dev *bt } offsetWithinChunk := otherLAddr.Sub(bg.LAddr) - flags := bg.Flags mapping := btrfsvol.Mapping{ - LAddr: bg.LAddr, - PAddr: btrfsvol.QualifiedPhysicalAddr{ - Dev: otherPAddr.Dev, - Addr: otherPAddr.Addr.Add(-offsetWithinChunk), - }, + LAddr: bg.LAddr, + PAddr: otherPAddr.Add(-offsetWithinChunk), Size: bg.Size, SizeLocked: true, - Flags: &flags, + Flags: containers.Optional[btrfsvol.BlockGroupFlags]{ + OK: true, + Val: bg.Flags, + }, } if err := fs.LV.AddMapping(mapping); err != nil { dlog.Errorf(ctx, "... dev[%q] error: adding flags from blockgroup: %v", diff --git a/lib/containers/optional.go b/lib/containers/optional.go new file mode 100644 index 0000000..3055308 --- /dev/null +++ b/lib/containers/optional.go @@ -0,0 +1,10 @@ +// Copyright (C) 2022 Luke Shumaker +// +// SPDX-License-Identifier: GPL-2.0-or-later + +package containers + +type Optional[T any] struct { + OK bool + Val T +} -- cgit v1.2.3-2-g168b