Skip to content

Instantly share code, notes, and snippets.

@starius
Created September 27, 2017 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save starius/0b1489f87dd65773cb39915899890ed7 to your computer and use it in GitHub Desktop.
Save starius/0b1489f87dd65773cb39915899890ed7 to your computer and use it in GitHub Desktop.
diff --git a/modules/host/contractmanager/dependencies.go b/modules/host/contractmanager/dependencies.go
index abbd23d..2508e2c 100644
--- a/modules/host/contractmanager/dependencies.go
+++ b/modules/host/contractmanager/dependencies.go
@@ -89,6 +89,7 @@ type (
Sync() error
Truncate(int64) error
WriteAt([]byte, int64) (int, error)
+ Fd() uintptr
}
)
diff --git a/modules/host/contractmanager/sector.go b/modules/host/contractmanager/sector.go
index f8ba8ba..3553c34 100644
--- a/modules/host/contractmanager/sector.go
+++ b/modules/host/contractmanager/sector.go
@@ -5,10 +5,12 @@ import (
"errors"
"sync"
"sync/atomic"
+ "syscall"
"github.com/NebulousLabs/Sia/build"
"github.com/NebulousLabs/Sia/crypto"
"github.com/NebulousLabs/Sia/modules"
+ "golang.org/x/sys/unix"
)
var (
@@ -94,6 +96,17 @@ func writeSector(f file, sectorIndex uint32, data []byte) error {
return nil
}
+// deallocateSector will clear the region of the file trying to make
+// the disk space free.
+func deallocateSector(f file, sectorIndex uint32) error {
+ return syscall.Fallocate(
+ int(f.Fd()),
+ unix.FALLOC_FL_PUNCH_HOLE | unix.FALLOC_FL_KEEP_SIZE,
+ int64(uint64(sectorIndex)*modules.SectorSize),
+ int64(modules.SectorSize),
+ )
+}
+
// writeSectorMetadata will take a sector update and write the related metadata
// to disk.
func writeSectorMetadata(f file, sectorIndex uint32, id sectorID, count uint16) error {
diff --git a/modules/host/contractmanager/sectorupdate.go b/modules/host/contractmanager/sectorupdate.go
index 808996e..80f08de 100644
--- a/modules/host/contractmanager/sectorupdate.go
+++ b/modules/host/contractmanager/sectorupdate.go
@@ -23,6 +23,7 @@ func (wal *writeAheadLog) commitUpdateSector(su sectorUpdate) {
// If the sector is being cleaned from disk, unset the usage flag.
if su.Count == 0 {
sf.clearUsage(su.Index)
+ _ = deallocateSector(sf.sectorFile, su.Index)
return
}
@@ -99,6 +100,7 @@ func (wal *writeAheadLog) managedAddPhysicalSector(id sectorID, data []byte, cou
sf.clearUsage(sectorIndex)
delete(sf.availableSectors, id)
wal.mu.Unlock()
+ _ = deallocateSector(sf.sectorFile, sectorIndex)
return errDiskTrouble
}
@@ -117,6 +119,7 @@ func (wal *writeAheadLog) managedAddPhysicalSector(id sectorID, data []byte, cou
sf.clearUsage(sectorIndex)
delete(sf.availableSectors, id)
wal.mu.Unlock()
+ _ = deallocateSector(sf.sectorFile, sectorIndex)
return errDiskTrouble
}
@@ -265,6 +268,7 @@ func (wal *writeAheadLog) managedDeleteSector(id sectorID) error {
delete(sf.availableSectors, id)
sf.clearUsage(location.index)
wal.mu.Unlock()
+ _ = deallocateSector(sf.sectorFile, location.index)
return nil
}
@@ -349,6 +353,7 @@ func (wal *writeAheadLog) managedRemoveSector(id sectorID) error {
sf.clearUsage(location.index)
delete(sf.availableSectors, id)
wal.mu.Unlock()
+ _ = deallocateSector(sf.sectorFile, location.index)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment