From 6196a5b5fe78f7a5b8e38c00e656f70c1592e1df Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Sun, 12 Jul 2020 12:59:01 +0200 Subject: v2api: File2: implement Release, Read, Write, Fsync, Flush, Allocate Fortunately, this just means fixing up the function signatures. --- internal/fusefrontend/file2_allocate_truncate.go | 60 ++++++++++++------------ 1 file changed, 30 insertions(+), 30 deletions(-) (limited to 'internal/fusefrontend/file2_allocate_truncate.go') diff --git a/internal/fusefrontend/file2_allocate_truncate.go b/internal/fusefrontend/file2_allocate_truncate.go index 9a3d7d1..9413b90 100644 --- a/internal/fusefrontend/file2_allocate_truncate.go +++ b/internal/fusefrontend/file2_allocate_truncate.go @@ -4,10 +4,11 @@ package fusefrontend // i.e. ftruncate and fallocate import ( + "context" "log" "syscall" - "github.com/hanwen/go-fuse/v2/fuse" + "github.com/hanwen/go-fuse/v2/fs" "github.com/rfjakob/gocryptfs/internal/syscallcompat" "github.com/rfjakob/gocryptfs/internal/tlog" @@ -26,19 +27,19 @@ import ( // complicated and hard to get right. // // Other modes (hole punching, zeroing) are not supported. -func (f *File2) Allocate(off uint64, sz uint64, mode uint32) fuse.Status { +func (f *File2) Allocate(ctx context.Context, off uint64, sz uint64, mode uint32) syscall.Errno { if mode != FALLOC_DEFAULT && mode != FALLOC_FL_KEEP_SIZE { f := func() { tlog.Info.Printf("fallocate: only mode 0 (default) and 1 (keep size) are supported") } allocateWarnOnce.Do(f) - return fuse.Status(syscall.EOPNOTSUPP) + return syscall.EOPNOTSUPP } f.fdLock.RLock() defer f.fdLock.RUnlock() if f.released { - return fuse.EBADF + return syscall.EBADF } f.fileTableEntry.ContentLock.Lock() defer f.fileTableEntry.ContentLock.Unlock() @@ -57,23 +58,23 @@ func (f *File2) Allocate(off uint64, sz uint64, mode uint32) fuse.Status { tlog.Debug.Printf("Allocate off=%d sz=%d mode=%x cipherOff=%d cipherSz=%d\n", off, sz, mode, cipherOff, cipherSz) if err != nil { - return fuse.ToStatus(err) + return fs.ToErrno(err) } if mode == FALLOC_FL_KEEP_SIZE { // The user did not want to change the apparent size. We are done. - return fuse.OK + return 0 } // Step (2): Grow the apparent file size // We need the old file size to determine if we are growing the file at all. newPlainSz := off + sz oldPlainSz, err := f.statPlainSize() if err != nil { - return fuse.ToStatus(err) + return fs.ToErrno(err) } if newPlainSz <= oldPlainSz { // The new size is smaller (or equal). Fallocate with mode = 0 never // truncates a file, so we are done. - return fuse.OK + return 0 } // The file grows. The space has already been allocated in (1), so what is // left to do is to pad the first and last block and call truncate. @@ -82,13 +83,13 @@ func (f *File2) Allocate(off uint64, sz uint64, mode uint32) fuse.Status { } // truncate - called from Setattr. -func (f *File2) truncate(newSize uint64) fuse.Status { +func (f *File2) truncate(newSize uint64) (errno syscall.Errno) { f.fdLock.RLock() defer f.fdLock.RUnlock() if f.released { // The file descriptor has been closed concurrently. tlog.Warn.Printf("ino%d fh%d: Truncate on released file", f.qIno.Ino, f.intFd()) - return fuse.EBADF + return syscall.EBADF } f.fileTableEntry.ContentLock.Lock() defer f.fileTableEntry.ContentLock.Unlock() @@ -98,17 +99,17 @@ func (f *File2) truncate(newSize uint64) fuse.Status { err = syscall.Ftruncate(int(f.fd.Fd()), 0) if err != nil { tlog.Warn.Printf("ino%d fh%d: Ftruncate(fd, 0) returned error: %v", f.qIno.Ino, f.intFd(), err) - return fuse.ToStatus(err) + return fs.ToErrno(err) } // Truncate to zero kills the file header f.fileTableEntry.ID = nil - return fuse.OK + return 0 } // We need the old file size to determine if we are growing or shrinking // the file oldSize, err := f.statPlainSize() if err != nil { - return fuse.ToStatus(err) + return fs.ToErrno(err) } oldB := float32(oldSize) / float32(f.contentEnc.PlainBS()) @@ -117,7 +118,7 @@ func (f *File2) truncate(newSize uint64) fuse.Status { // File size stays the same - nothing to do if newSize == oldSize { - return fuse.OK + return 0 } // File grows if newSize > oldSize { @@ -131,25 +132,24 @@ func (f *File2) truncate(newSize uint64) fuse.Status { lastBlockLen := newSize - plainOff var data []byte if lastBlockLen > 0 { - var status fuse.Status - data, status = f.doRead(nil, plainOff, lastBlockLen) - if status != fuse.OK { + data, errno = f.doRead(nil, plainOff, lastBlockLen) + if errno != 0 { tlog.Warn.Printf("Truncate: shrink doRead returned error: %v", err) - return status + return errno } } // Truncate down to the last complete block err = syscall.Ftruncate(int(f.fd.Fd()), int64(cipherOff)) if err != nil { tlog.Warn.Printf("Truncate: shrink Ftruncate returned error: %v", err) - return fuse.ToStatus(err) + return fs.ToErrno(err) } // Append partial block if lastBlockLen > 0 { _, status := f.doWrite(data, int64(plainOff)) return status } - return fuse.OK + return 0 } // statPlainSize stats the file and returns the plaintext size @@ -167,7 +167,7 @@ func (f *File2) statPlainSize() (uint64, error) { // truncateGrowFile extends a file using seeking or ftruncate performing RMW on // the first and last block as necessary. New blocks in the middle become // file holes unless they have been fallocate()'d beforehand. -func (f *File2) truncateGrowFile(oldPlainSz uint64, newPlainSz uint64) fuse.Status { +func (f *File2) truncateGrowFile(oldPlainSz uint64, newPlainSz uint64) syscall.Errno { if newPlainSz <= oldPlainSz { log.Panicf("BUG: newSize=%d <= oldSize=%d", newPlainSz, oldPlainSz) } @@ -179,17 +179,17 @@ func (f *File2) truncateGrowFile(oldPlainSz uint64, newPlainSz uint64) fuse.Stat // Write a single zero to the last byte and let doWrite figure out the RMW. if n1 == n2 { buf := make([]byte, 1) - _, status := f.doWrite(buf, int64(newEOFOffset)) - return status + _, errno := f.doWrite(buf, int64(newEOFOffset)) + return errno } } // The truncate creates at least one new block. // // Make sure the old last block is padded to the block boundary. This call // is a no-op if it is already block-aligned. - status := f.zeroPad(oldPlainSz) - if !status.Ok() { - return status + errno := f.zeroPad(oldPlainSz) + if errno != 0 { + return errno } // The new size is block-aligned. In this case we can do everything ourselves // and avoid the call to doWrite. @@ -198,7 +198,7 @@ func (f *File2) truncateGrowFile(oldPlainSz uint64, newPlainSz uint64) fuse.Stat if oldPlainSz == 0 { id, err := f.createHeader() if err != nil { - return fuse.ToStatus(err) + return fs.ToErrno(err) } f.fileTableEntry.ID = id } @@ -207,11 +207,11 @@ func (f *File2) truncateGrowFile(oldPlainSz uint64, newPlainSz uint64) fuse.Stat if err != nil { tlog.Warn.Printf("Truncate: grow Ftruncate returned error: %v", err) } - return fuse.ToStatus(err) + return fs.ToErrno(err) } // The new size is NOT aligned, so we need to write a partial block. // Write a single zero to the last byte and let doWrite figure it out. buf := make([]byte, 1) - _, status = f.doWrite(buf, int64(newEOFOffset)) - return status + _, errno = f.doWrite(buf, int64(newEOFOffset)) + return errno } -- cgit v1.2.3