m/p/blockdev: fix discard
Fix various problems with discard. Because discard errors are ignored in
osimage, these problems were not noticed.
Discard takes byte offsets, not block offsets, and multiple places were
missing the multiplication with BlockSize.
The BLKDISCARD and BLKZEROOUT ioctls take start and length, not start
and end.
unix.Fallocate already converts errno 0 to nil.
I manually tested that discard now works correctly in the installer.
Change-Id: I52d9e30a087bb6a792396baaefb2d7d09c43abfe
Reviewed-on: https://review.monogon.dev/c/monogon/+/2953
Tested-by: Jenkins CI
Reviewed-by: Lorenz Brun <lorenz@monogon.tech>
Vouch-Run-CI: Lorenz Brun <lorenz@monogon.tech>
diff --git a/metropolis/pkg/blockdev/blockdev.go b/metropolis/pkg/blockdev/blockdev.go
index c7b2875..0e3c6e1 100644
--- a/metropolis/pkg/blockdev/blockdev.go
+++ b/metropolis/pkg/blockdev/blockdev.go
@@ -154,7 +154,8 @@
if err := s.inRange(startByte, endByte); err != nil {
return err
}
- return s.b.Discard(s.startBlock+startByte, s.startBlock+endByte)
+ offset := s.startBlock * s.b.BlockSize()
+ return s.b.Discard(offset+startByte, offset+endByte)
}
func (s *Section) OptimalBlockSize() int64 {
@@ -165,7 +166,8 @@
if err := s.inRange(startByte, endByte); err != nil {
return err
}
- return s.b.Zero(s.startBlock+startByte, s.startBlock+endByte)
+ offset := s.startBlock * s.b.BlockSize()
+ return s.b.Zero(offset+startByte, offset+endByte)
}
// GenericZero implements software-based zeroing. This can be used to implement
diff --git a/metropolis/pkg/blockdev/blockdev_linux.go b/metropolis/pkg/blockdev/blockdev_linux.go
index b40e5cd..6d87c87 100644
--- a/metropolis/pkg/blockdev/blockdev_linux.go
+++ b/metropolis/pkg/blockdev/blockdev_linux.go
@@ -44,7 +44,7 @@
var args [2]uint64
var err unix.Errno
args[0] = uint64(startByte)
- args[1] = uint64(endByte)
+ args[1] = uint64(endByte - startByte)
if ctrlErr := d.rawConn.Control(func(fd uintptr) {
_, _, err = unix.Syscall(unix.SYS_IOCTL, fd, unix.BLKDISCARD, uintptr(unsafe.Pointer(&args[0])))
}); ctrlErr != nil {
@@ -67,7 +67,7 @@
var args [2]uint64
var err error
args[0] = uint64(startByte)
- args[1] = uint64(endByte)
+ args[1] = uint64(endByte - startByte)
if ctrlErr := d.rawConn.Control(func(fd uintptr) {
// Attempts to leverage discard guarantees to provide extremely quick
// metadata-only zeroing.
@@ -215,7 +215,7 @@
if errors.Is(err, unix.EOPNOTSUPP) {
return errors.ErrUnsupported
}
- if err != unix.Errno(0) {
+ if err != nil {
return fmt.Errorf("failed to discard: %w", err)
}
return nil