rclone/lib/file/preallocate_unix.go

74 lines
1.6 KiB
Go
Raw Normal View History

2021-09-09 14:25:25 +02:00
//go:build linux
// +build linux
package file
import (
"os"
"sync"
"sync/atomic"
"syscall"
"github.com/rclone/rclone/fs"
"golang.org/x/sys/unix"
)
var (
fallocFlags = [...]uint32{
unix.FALLOC_FL_KEEP_SIZE, // Default
unix.FALLOC_FL_KEEP_SIZE | unix.FALLOC_FL_PUNCH_HOLE, // for ZFS #3066
}
2023-08-18 23:05:17 +02:00
fallocFlagsIndex atomic.Int32
preAllocateMu sync.Mutex
)
2020-05-19 11:15:20 +02:00
// PreallocateImplemented is a constant indicating whether the
// implementation of Preallocate actually does anything.
const PreallocateImplemented = true
// PreAllocate the file for performance reasons
func PreAllocate(size int64, out *os.File) (err error) {
if size <= 0 {
return nil
}
preAllocateMu.Lock()
defer preAllocateMu.Unlock()
for {
2023-08-18 23:05:17 +02:00
index := fallocFlagsIndex.Load()
again:
if index >= int32(len(fallocFlags)) {
return nil // Fallocate is disabled
}
flags := fallocFlags[index]
err = unix.Fallocate(int(out.Fd()), flags, 0, size)
if err == unix.ENOTSUP {
// Try the next flags combination
index++
2023-08-18 23:05:17 +02:00
fallocFlagsIndex.Store(index)
fs.Debugf(nil, "preAllocate: got error on fallocate, trying combination %d/%d: %v", index, len(fallocFlags), err)
goto again
}
// Wrap important errors
if err == unix.ENOSPC {
return ErrDiskFull
}
if err != syscall.EINTR {
break
}
}
return err
}
2020-05-19 11:15:20 +02:00
// SetSparseImplemented is a constant indicating whether the
// implementation of SetSparse actually does anything.
const SetSparseImplemented = false
// SetSparse makes the file be a sparse file
func SetSparse(out *os.File) error {
return nil
}