diff options
-rw-r--r-- | internal/fusefrontend/file.go | 4 | ||||
-rw-r--r-- | internal/fusefrontend/file_allocate_truncate.go | 3 | ||||
-rw-r--r-- | internal/syscallcompat/sys_darwin.go | 33 | ||||
-rw-r--r-- | internal/syscallcompat/sys_linux.go | 19 | ||||
-rw-r--r-- | tests/matrix/matrix_test.go | 15 |
5 files changed, 61 insertions, 13 deletions
diff --git a/internal/fusefrontend/file.go b/internal/fusefrontend/file.go index f0e0c41..b9bb2c9 100644 --- a/internal/fusefrontend/file.go +++ b/internal/fusefrontend/file.go @@ -101,7 +101,7 @@ func (f *file) createHeader() error { buf := h.Pack() // Prevent partially written (=corrupt) header by preallocating the space beforehand - err := syscallcompat.Prealloc(int(f.fd.Fd()), 0, contentenc.HEADER_LEN) + err := syscallcompat.EnospcPrealloc(int(f.fd.Fd()), 0, contentenc.HEADER_LEN) if err != nil { tlog.Warn.Printf("ino%d: createHeader: prealloc failed: %s\n", f.ino, err.Error()) return err @@ -262,7 +262,7 @@ func (f *file) doWrite(data []byte, off int64) (uint32, fuse.Status) { f.ino, uint64(len(blockData))-f.contentEnc.BlockOverhead(), b.BlockNo) // Prevent partially written (=corrupt) blocks by preallocating the space beforehand - err := syscallcompat.Prealloc(int(f.fd.Fd()), int64(blockOffset), int64(len(blockData))) + err := syscallcompat.EnospcPrealloc(int(f.fd.Fd()), int64(blockOffset), int64(len(blockData))) if err != nil { tlog.Warn.Printf("ino%d fh%d: doWrite: prealloc failed: %s", f.ino, f.intFd(), err.Error()) status = fuse.ToStatus(err) diff --git a/internal/fusefrontend/file_allocate_truncate.go b/internal/fusefrontend/file_allocate_truncate.go index 65d6df6..f22fa4b 100644 --- a/internal/fusefrontend/file_allocate_truncate.go +++ b/internal/fusefrontend/file_allocate_truncate.go @@ -10,6 +10,7 @@ import ( "github.com/hanwen/go-fuse/fuse" + "github.com/rfjakob/gocryptfs/internal/syscallcompat" "github.com/rfjakob/gocryptfs/internal/tlog" ) @@ -59,7 +60,7 @@ func (f *file) Allocate(off uint64, sz uint64, mode uint32) fuse.Status { cipherOff := firstBlock.BlockCipherOff() cipherSz := lastBlock.BlockCipherOff() - cipherOff + f.contentEnc.PlainSizeToCipherSize(lastBlock.Skip+lastBlock.Length) - err := syscall.Fallocate(f.intFd(), FALLOC_FL_KEEP_SIZE, int64(cipherOff), int64(cipherSz)) + err := syscallcompat.Fallocate(f.intFd(), FALLOC_FL_KEEP_SIZE, int64(cipherOff), int64(cipherSz)) tlog.Debug.Printf("Allocate off=%d sz=%d mode=%x cipherOff=%d cipherSz=%d\n", off, sz, mode, cipherOff, cipherSz) if err != nil { diff --git a/internal/syscallcompat/sys_darwin.go b/internal/syscallcompat/sys_darwin.go index d437170..81e1b15 100644 --- a/internal/syscallcompat/sys_darwin.go +++ b/internal/syscallcompat/sys_darwin.go @@ -1,5 +1,11 @@ package syscallcompat +import ( + "os" + "sync" + "syscall" +) + // prealloc - preallocate space without changing the file size. This prevents // us from running out of space in the middle of an operation. func Prealloc(fd int, off int64, len int64) (err error) { @@ -10,3 +16,30 @@ func Prealloc(fd int, off int64, len int64) (err error) { // See https://github.com/rfjakob/gocryptfs/issues/18 if you want to help. return nil } + +var openatLock sync.Mutex + +// Poor man's Openat: +// 1) fchdir to the dirfd +// 2) open the file +// 3) chdir back. +func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) { + openatLock.Lock() + defer openatLock.Unlock() + + oldWd, err := os.Getwd() + if err != nil { + return -1, err + } + err = syscall.Fchdir(dirfd) + if err != nil { + return -1, err + } + defer os.Chdir(oldWd) + + return syscall.Open(path, flags, mode) +} + +func Fallocate(fd int, mode uint32, off int64, len int64) (err error) { + return syscall.EOPNOTSUPP +} diff --git a/internal/syscallcompat/sys_linux.go b/internal/syscallcompat/sys_linux.go index 9fe9da5..ca6df95 100644 --- a/internal/syscallcompat/sys_linux.go +++ b/internal/syscallcompat/sys_linux.go @@ -11,9 +11,10 @@ const FALLOC_FL_KEEP_SIZE = 0x01 var preallocWarn sync.Once -// prealloc - preallocate space without changing the file size. This prevents -// us from running out of space in the middle of an operation. -func Prealloc(fd int, off int64, len int64) (err error) { +// EnospcPrealloc preallocates ciphertext space without changing the file +// size. This guarantees that we don't run out of space while writing a +// ciphertext block (that would corrupt the block). +func EnospcPrealloc(fd int, off int64, len int64) (err error) { for { err = syscall.Fallocate(fd, FALLOC_FL_KEEP_SIZE, off, len) if err == syscall.EINTR { @@ -23,8 +24,8 @@ func Prealloc(fd int, off int64, len int64) (err error) { continue } if err == syscall.EOPNOTSUPP { - // ZFS does not support fallocate which caused gocryptfs to abort - // every write operation: https://github.com/rfjakob/gocryptfs/issues/22 + // ZFS and ext3 do not support fallocate. Warn but continue anyway. + // https://github.com/rfjakob/gocryptfs/issues/22 preallocWarn.Do(func() { tlog.Warn.Printf("Warning: The underlying filesystem " + "does not support fallocate(2). gocryptfs will continue working " + @@ -35,3 +36,11 @@ func Prealloc(fd int, off int64, len int64) (err error) { return err } } + +func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) { + return syscall.Openat(dirfd, path, flags, mode) +} + +func Fallocate(fd int, mode uint32, off int64, len int64) (err error) { + return syscall.Fallocate(fd, mode, off, len) +} diff --git a/tests/matrix/matrix_test.go b/tests/matrix/matrix_test.go index be5ff60..3e4ecb7 100644 --- a/tests/matrix/matrix_test.go +++ b/tests/matrix/matrix_test.go @@ -23,6 +23,7 @@ import ( "syscall" "testing" + "github.com/rfjakob/gocryptfs/internal/syscallcompat" "github.com/rfjakob/gocryptfs/tests/test_helpers" ) @@ -176,6 +177,10 @@ const FALLOC_DEFAULT = 0x00 const FALLOC_FL_KEEP_SIZE = 0x01 func TestFallocate(t *testing.T) { + if runtime.GOOS == "darwin" { + t.Skipf("OSX does not support fallocate") + } + fn := test_helpers.DefaultPlainDir + "/fallocate" file, err := os.Create(fn) if err != nil { @@ -190,7 +195,7 @@ func TestFallocate(t *testing.T) { // Allocate 30 bytes, keep size // gocryptfs || (0 blocks) // ext4 | d | (1 block) - err = syscall.Fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, 30) + err = syscallcompat.Fallocate(fd, FALLOC_FL_KEEP_SIZE, 0, 30) if err != nil { t.Error(err) } @@ -218,7 +223,7 @@ func TestFallocate(t *testing.T) { // Allocate the whole file space // gocryptfs | h | h | d| (1 block) // ext4 | d | d | d | (3 blocks - err = syscall.Fallocate(fd, FALLOC_DEFAULT, 0, 9000) + err = syscallcompat.Fallocate(fd, FALLOC_DEFAULT, 0, 9000) if err != nil { t.Fatal(err) } @@ -244,7 +249,7 @@ func TestFallocate(t *testing.T) { // Allocate 10 bytes in the second block // gocryptfs | h | h | d| (1 block) // ext4 | d | d | d | (2 blocks) - syscall.Fallocate(fd, FALLOC_DEFAULT, 5000, 10) + syscallcompat.Fallocate(fd, FALLOC_DEFAULT, 5000, 10) _, nBlocks = test_helpers.Du(t, fd) if want := 3; nBlocks/8 != int64(want) { t.Errorf("Expected %d 4k block(s), got %d", want, nBlocks/8) @@ -257,7 +262,7 @@ func TestFallocate(t *testing.T) { // Grow the file to 4 blocks // gocryptfs | h | h | d |d| (2 blocks) // ext4 | d | d | d | d | (3 blocks) - syscall.Fallocate(fd, FALLOC_DEFAULT, 15000, 10) + syscallcompat.Fallocate(fd, FALLOC_DEFAULT, 15000, 10) _, nBlocks = test_helpers.Du(t, fd) if want := 4; nBlocks/8 != int64(want) { t.Errorf("Expected %d 4k block(s), got %d", want, nBlocks/8) @@ -269,7 +274,7 @@ func TestFallocate(t *testing.T) { // Shrinking a file using fallocate should have no effect for _, off := range []int64{0, 10, 2000, 5000} { for _, sz := range []int64{0, 1, 42, 6000} { - syscall.Fallocate(fd, FALLOC_DEFAULT, off, sz) + syscallcompat.Fallocate(fd, FALLOC_DEFAULT, off, sz) test_helpers.VerifySize(t, fn, 15010) if md5 := test_helpers.Md5fn(fn); md5 != "c4c44c7a41ab7798a79d093eb44f99fc" { t.Errorf("Wrong md5 %s", md5) |