Skip to content

Commit

Permalink
storage: cleanup removed volumes
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed May 4, 2023
1 parent 59477f0 commit c020b31
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
34 changes: 21 additions & 13 deletions host/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,15 @@ func (vm *VolumeManager) lockVolume(id int) (func(), error) {
} else if v.busy {
return nil, fmt.Errorf("volume %v is busy", id)
}
var once sync.Once
return func() {
vm.mu.Lock()
vm.volumes[id].busy = false
vm.mu.Unlock()
once.Do(func() {
vm.mu.Lock()
if vm.volumes[id] != nil {
vm.volumes[id].busy = false
}
vm.mu.Unlock()
})
}, nil
}

Expand Down Expand Up @@ -444,20 +449,11 @@ func (vm *VolumeManager) RemoveVolume(id int, force bool) error {
return fmt.Errorf("failed to get volume: %w", err)
}

oldReadonly := vol.ReadOnly

// set the volume to read-only to prevent new sectors from being added
if err := vm.vs.SetReadOnly(id, true); err != nil {
return fmt.Errorf("failed to set volume %v to read-only: %w", id, err)
}

defer func() {
// restore the volume to its original read-only status
if err := vm.vs.SetReadOnly(id, oldReadonly); err != nil {
vm.log.Named("remove").Error("failed to restore volume read-only status", zap.Error(err), zap.Int("volumeID", id))
}
}()

// migrate sectors to other volumes
err = vm.vs.MigrateSectors(id, 0, func(locations []SectorLocation) error {
select {
Expand All @@ -469,8 +465,20 @@ func (vm *VolumeManager) RemoveVolume(id int, force bool) error {
})
if err != nil {
return fmt.Errorf("failed to migrate sector data: %w", err)
} else if err := vm.vs.RemoveVolume(id, force); err != nil {
return fmt.Errorf("failed to remove volume: %w", err)
}
return vm.vs.RemoveVolume(id, force)
vm.mu.Lock()
// close the volume
vm.volumes[id].Close()
// delete the volume from memory
delete(vm.volumes, id)
vm.mu.Unlock()
// remove the volume file
if err := os.Remove(vol.LocalPath); err != nil {
return fmt.Errorf("failed to remove volume file: %w", err)
}
return nil
}

// ResizeVolume resizes a volume to the specified size.
Expand Down
5 changes: 4 additions & 1 deletion host/storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ func TestRemoveVolume(t *testing.T) {
}
defer vm.Close()

volume, err := vm.AddVolume(filepath.Join(t.TempDir(), "hostdata.dat"), expectedSectors)
volumePath := filepath.Join(t.TempDir(), "hostdata.dat")
volume, err := vm.AddVolume(volumePath, expectedSectors)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -259,6 +260,8 @@ func TestRemoveVolume(t *testing.T) {
// remove the volume
if err := vm.RemoveVolume(volume.ID, false); err != nil {
t.Fatal(err)
} else if _, err := os.Stat(volumePath); !errors.Is(err, os.ErrNotExist) {
t.Fatal("volume file still exists", err)
}
}

Expand Down

0 comments on commit c020b31

Please sign in to comment.