From b9bf91c510118f88826960a265cc738ec9b5151c Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 16 Feb 2021 16:36:27 +0000 Subject: [PATCH] lib/file: don't run preallocate concurrently This seems to cause file systems to get the amount of free space wrong. --- lib/file/preallocate_unix.go | 4 ++++ lib/file/preallocate_windows.go | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/lib/file/preallocate_unix.go b/lib/file/preallocate_unix.go index c44c869ea..d610b46b1 100644 --- a/lib/file/preallocate_unix.go +++ b/lib/file/preallocate_unix.go @@ -4,6 +4,7 @@ package file import ( "os" + "sync" "sync/atomic" "github.com/rclone/rclone/fs" @@ -16,6 +17,7 @@ var ( unix.FALLOC_FL_KEEP_SIZE | unix.FALLOC_FL_PUNCH_HOLE, // for ZFS #3066 } fallocFlagsIndex int32 + preAllocateMu sync.Mutex ) // PreallocateImplemented is a constant indicating whether the @@ -27,6 +29,8 @@ func PreAllocate(size int64, out *os.File) error { if size <= 0 { return nil } + preAllocateMu.Lock() + defer preAllocateMu.Unlock() index := atomic.LoadInt32(&fallocFlagsIndex) again: if index >= int32(len(fallocFlags)) { diff --git a/lib/file/preallocate_windows.go b/lib/file/preallocate_windows.go index 847a49a48..11b39ad15 100644 --- a/lib/file/preallocate_windows.go +++ b/lib/file/preallocate_windows.go @@ -4,6 +4,7 @@ package file import ( "os" + "sync" "syscall" "unsafe" @@ -15,6 +16,7 @@ var ( ntdll = windows.NewLazySystemDLL("ntdll.dll") ntQueryVolumeInformationFile = ntdll.NewProc("NtQueryVolumeInformationFile") ntSetInformationFile = ntdll.NewProc("NtSetInformationFile") + preAllocateMu sync.Mutex ) type fileAllocationInformation struct { @@ -42,6 +44,9 @@ func PreAllocate(size int64, out *os.File) error { return nil } + preAllocateMu.Lock() + defer preAllocateMu.Unlock() + var ( iosb ioStatusBlock fsSizeInfo fileFsSizeInformation