2021-09-09 14:25:25 +02:00
|
|
|
//go:build linux
|
2018-08-20 21:14:19 +02:00
|
|
|
|
2020-03-24 17:26:45 +01:00
|
|
|
package file
|
2018-08-20 21:14:19 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2021-02-16 17:36:27 +01:00
|
|
|
"sync"
|
2019-03-25 16:22:40 +01:00
|
|
|
"sync/atomic"
|
2021-02-17 19:11:27 +01:00
|
|
|
"syscall"
|
2018-08-20 21:14:19 +02:00
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
2018-08-20 21:14:19 +02:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2019-03-25 16:22:40 +01:00
|
|
|
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
|
2021-02-16 17:36:27 +01:00
|
|
|
preAllocateMu sync.Mutex
|
2019-03-25 16:22:40 +01:00
|
|
|
)
|
|
|
|
|
2020-05-19 11:15:20 +02:00
|
|
|
// PreallocateImplemented is a constant indicating whether the
|
|
|
|
// implementation of Preallocate actually does anything.
|
|
|
|
const PreallocateImplemented = true
|
|
|
|
|
2020-03-24 17:26:45 +01:00
|
|
|
// PreAllocate the file for performance reasons
|
2021-02-17 19:11:27 +01:00
|
|
|
func PreAllocate(size int64, out *os.File) (err error) {
|
2018-08-20 21:14:19 +02:00
|
|
|
if size <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-02-17 19:11:27 +01:00
|
|
|
|
2021-02-16 17:36:27 +01:00
|
|
|
preAllocateMu.Lock()
|
|
|
|
defer preAllocateMu.Unlock()
|
2019-03-25 16:22:40 +01:00
|
|
|
|
2021-02-17 19:11:27 +01:00
|
|
|
for {
|
|
|
|
|
2023-08-18 23:05:17 +02:00
|
|
|
index := fallocFlagsIndex.Load()
|
2021-02-17 19:11:27 +01:00
|
|
|
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)
|
2021-02-17 19:11:27 +01:00
|
|
|
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
|
|
|
|
}
|
2021-02-17 14:05:15 +01:00
|
|
|
}
|
2018-08-20 21:14:19 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-03-06 13:41:48 +01:00
|
|
|
|
2020-05-19 11:15:20 +02:00
|
|
|
// SetSparseImplemented is a constant indicating whether the
|
|
|
|
// implementation of SetSparse actually does anything.
|
|
|
|
const SetSparseImplemented = false
|
|
|
|
|
2020-03-24 17:26:45 +01:00
|
|
|
// SetSparse makes the file be a sparse file
|
|
|
|
func SetSparse(out *os.File) error {
|
2020-03-06 13:41:48 +01:00
|
|
|
return nil
|
|
|
|
}
|