fix UNMAP data corruption by implementing block zeroing

The UNMAP command was a no-op in all backing stores, causing unmapped
blocks to retain stale data instead of returning zeros per SCSI spec.

- Implement Unmap in FileBackingStore to zero out unmapped blocks
- Implement Unmap in IOUringBackingStore to zero out unmapped blocks
- Enable Unmap in RemBackingStore (was commented out)
- Change UnmapBlockDescriptor.TL from uint32 to uint64 to prevent
  integer overflow when converting block count to byte length with
  large block shifts

Fixes #119

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Lei Xue
2026-03-14 12:29:12 +08:00
parent 2bfc4cae7d
commit 00d0c3a306
5 changed files with 22 additions and 6 deletions

View File

@@ -409,7 +409,7 @@ type LUNMap map[uint64]*SCSILu
type UnmapBlockDescriptor struct {
Offset uint64
TL uint32
TL uint64
}
type ReaderWriterAt interface {

View File

@@ -159,6 +159,12 @@ func (bs *FileBackingStore) DataAdvise(offset, length int64, advise uint32) erro
return util.Fadvise(bs.file, offset, length, advise)
}
func (bs *FileBackingStore) Unmap([]api.UnmapBlockDescriptor) error {
func (bs *FileBackingStore) Unmap(descriptors []api.UnmapBlockDescriptor) error {
for _, desc := range descriptors {
zeros := make([]byte, desc.TL)
if _, err := bs.file.WriteAt(zeros, int64(desc.Offset)); err != nil {
return err
}
}
return nil
}

View File

@@ -628,8 +628,14 @@ func (bs *IOUringBackingStore) DataAdvise(offset, length int64, advise uint32) e
return nil
}
// Unmap is a no-op for file-based storage
func (bs *IOUringBackingStore) Unmap([]api.UnmapBlockDescriptor) error {
// Unmap zeros out the specified blocks
func (bs *IOUringBackingStore) Unmap(descriptors []api.UnmapBlockDescriptor) error {
for _, desc := range descriptors {
zeros := make([]byte, desc.TL)
if _, err := bs.file.WriteAt(zeros, int64(desc.Offset)); err != nil {
return err
}
}
return nil
}

View File

@@ -113,6 +113,10 @@ func (bs *RemBackingStore) DataSync(offset, length int64) (err error) {
}
func (bs *RemBackingStore) Unmap(bd []api.UnmapBlockDescriptor) (err error) {
//_, err = bs.RemBs.Unmap(int64(bd[0].Offset), int64(bd[0].TL))
for _, desc := range bd {
if _, err = bs.RemBs.Unmap(int64(desc.Offset), int64(desc.TL)); err != nil {
return
}
}
return
}

View File

@@ -320,7 +320,7 @@ func SBCUnmap(host int, cmd *api.SCSICommand) api.SAMStat {
num := binary.BigEndian.Uint32(cmd.OutSDBBuffer.Buffer[off+8 : off+12])
blockDescs = append(blockDescs, api.UnmapBlockDescriptor{
Offset: lba << cmd.Device.BlockShift,
TL: num << cmd.Device.BlockShift,
TL: uint64(num) << cmd.Device.BlockShift,
})
}