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