2020-06-21 13:25:12 +02:00
|
|
|
package fusefrontend
|
|
|
|
|
|
|
|
// FUSE operations on file handles
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-07-05 20:05:07 +02:00
|
|
|
"context"
|
2020-06-21 13:25:12 +02:00
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
2021-06-02 18:20:05 +02:00
|
|
|
"math"
|
2020-06-21 13:25:12 +02:00
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
|
2020-07-05 20:05:07 +02:00
|
|
|
"github.com/hanwen/go-fuse/v2/fs"
|
2020-06-21 13:25:12 +02:00
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
|
|
|
|
2021-08-23 15:05:15 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/contentenc"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/inomap"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/openfiletable"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/serialize_reads"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/stupidgcm"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/syscallcompat"
|
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/tlog"
|
2020-06-21 13:25:12 +02:00
|
|
|
)
|
|
|
|
|
2020-08-16 12:48:16 +02:00
|
|
|
// File implements the go-fuse v2 API (github.com/hanwen/go-fuse/v2/fs)
|
|
|
|
type File struct {
|
2020-06-21 13:25:12 +02:00
|
|
|
fd *os.File
|
|
|
|
// Has Release() already been called on this file? This also means that the
|
|
|
|
// wlock entry has been freed, so let's not crash trying to access it.
|
|
|
|
// Due to concurrency, Release can overtake other operations. These will
|
|
|
|
// return EBADF in that case.
|
|
|
|
released bool
|
|
|
|
// fdLock prevents the fd to be closed while we are in the middle of
|
|
|
|
// an operation.
|
|
|
|
// Every FUSE entrypoint should RLock(). The only user of Lock() is
|
|
|
|
// Release(), which closes the fd and sets "released" to true.
|
|
|
|
fdLock sync.RWMutex
|
|
|
|
// Content encryption helper
|
|
|
|
contentEnc *contentenc.ContentEnc
|
|
|
|
// Device and inode number uniquely identify the backing file
|
|
|
|
qIno inomap.QIno
|
|
|
|
// Entry in the open file table
|
|
|
|
fileTableEntry *openfiletable.Entry
|
|
|
|
// Store where the last byte was written
|
|
|
|
lastWrittenOffset int64
|
|
|
|
// The opCount is used to judge whether "lastWrittenOffset" is still
|
|
|
|
// guaranteed to be correct.
|
|
|
|
lastOpCount uint64
|
|
|
|
// Parent filesystem
|
|
|
|
rootNode *RootNode
|
|
|
|
}
|
|
|
|
|
2021-03-21 09:31:05 +01:00
|
|
|
// NewFile returns a new go-fuse File instance based on an already-open file
|
|
|
|
// descriptor. NewFile internally calls Fstat() on the fd. The resulting Stat_t
|
|
|
|
// is returned because node.Create() needs it.
|
|
|
|
//
|
|
|
|
// `cName` is only used for error logging and may be left blank.
|
|
|
|
func NewFile(fd int, cName string, rn *RootNode) (f *File, st *syscall.Stat_t, errno syscall.Errno) {
|
|
|
|
// Need device number and inode number for openfiletable locking
|
|
|
|
st = &syscall.Stat_t{}
|
|
|
|
if err := syscall.Fstat(fd, st); err != nil {
|
|
|
|
errno = fs.ToErrno(err)
|
|
|
|
return
|
|
|
|
}
|
2020-06-21 13:25:12 +02:00
|
|
|
qi := inomap.QInoFromStat(st)
|
|
|
|
e := openfiletable.Register(qi)
|
|
|
|
|
2021-03-21 09:31:05 +01:00
|
|
|
osFile := os.NewFile(uintptr(fd), cName)
|
|
|
|
|
|
|
|
f = &File{
|
|
|
|
fd: osFile,
|
2020-06-21 13:25:12 +02:00
|
|
|
contentEnc: rn.contentEnc,
|
|
|
|
qIno: qi,
|
|
|
|
fileTableEntry: e,
|
|
|
|
rootNode: rn,
|
|
|
|
}
|
2021-03-21 09:31:05 +01:00
|
|
|
return f, st, 0
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// intFd - return the backing file descriptor as an integer.
|
2020-08-16 12:48:16 +02:00
|
|
|
func (f *File) intFd() int {
|
2020-06-21 13:25:12 +02:00
|
|
|
return int(f.fd.Fd())
|
|
|
|
}
|
|
|
|
|
|
|
|
// readFileID loads the file header from disk and extracts the file ID.
|
|
|
|
// Returns io.EOF if the file is empty.
|
2020-08-16 12:48:16 +02:00
|
|
|
func (f *File) readFileID() ([]byte, error) {
|
2020-06-21 13:25:12 +02:00
|
|
|
// We read +1 byte to determine if the file has actual content
|
|
|
|
// and not only the header. A header-only file will be considered empty.
|
|
|
|
// This makes File ID poisoning more difficult.
|
|
|
|
readLen := contentenc.HeaderLen + 1
|
|
|
|
buf := make([]byte, readLen)
|
|
|
|
n, err := f.fd.ReadAt(buf, 0)
|
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF && n != 0 {
|
|
|
|
tlog.Warn.Printf("readFileID %d: incomplete file, got %d instead of %d bytes",
|
|
|
|
f.qIno.Ino, n, readLen)
|
|
|
|
f.rootNode.reportMitigatedCorruption(fmt.Sprint(f.qIno.Ino))
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
buf = buf[:contentenc.HeaderLen]
|
|
|
|
h, err := contentenc.ParseHeader(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return h.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// createHeader creates a new random header and writes it to disk.
|
|
|
|
// Returns the new file ID.
|
|
|
|
// The caller must hold fileIDLock.Lock().
|
2020-08-16 12:48:16 +02:00
|
|
|
func (f *File) createHeader() (fileID []byte, err error) {
|
2020-06-21 13:25:12 +02:00
|
|
|
h := contentenc.RandomHeader()
|
|
|
|
buf := h.Pack()
|
|
|
|
// Prevent partially written (=corrupt) header by preallocating the space beforehand
|
2021-08-11 20:21:32 +02:00
|
|
|
if !f.rootNode.args.NoPrealloc && f.rootNode.quirks&syscallcompat.QuirkBrokenFalloc == 0 {
|
2020-06-21 13:25:12 +02:00
|
|
|
err = syscallcompat.EnospcPrealloc(f.intFd(), 0, contentenc.HeaderLen)
|
|
|
|
if err != nil {
|
|
|
|
if !syscallcompat.IsENOSPC(err) {
|
|
|
|
tlog.Warn.Printf("ino%d: createHeader: prealloc failed: %s\n", f.qIno.Ino, err.Error())
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Actually write header
|
|
|
|
_, err = f.fd.WriteAt(buf, 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return h.ID, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// doRead - read "length" plaintext bytes from plaintext offset "off" and append
|
|
|
|
// to "dst".
|
|
|
|
// Arguments "length" and "off" do not have to be block-aligned.
|
|
|
|
//
|
|
|
|
// doRead reads the corresponding ciphertext blocks from disk, decrypts them and
|
|
|
|
// returns the requested part of the plaintext.
|
|
|
|
//
|
|
|
|
// Called by Read() for normal reading,
|
|
|
|
// by Write() and Truncate() via doWrite() for Read-Modify-Write.
|
2020-08-16 12:48:16 +02:00
|
|
|
func (f *File) doRead(dst []byte, off uint64, length uint64) ([]byte, syscall.Errno) {
|
2020-06-21 13:25:12 +02:00
|
|
|
// Get the file ID, either from the open file table, or from disk.
|
|
|
|
var fileID []byte
|
|
|
|
f.fileTableEntry.IDLock.Lock()
|
|
|
|
if f.fileTableEntry.ID != nil {
|
|
|
|
// Use the cached value in the file table
|
|
|
|
fileID = f.fileTableEntry.ID
|
|
|
|
} else {
|
|
|
|
// Not cached, we have to read it from disk.
|
|
|
|
var err error
|
|
|
|
fileID, err = f.readFileID()
|
|
|
|
if err != nil {
|
|
|
|
f.fileTableEntry.IDLock.Unlock()
|
|
|
|
if err == io.EOF {
|
|
|
|
// Empty file
|
2020-07-12 12:59:01 +02:00
|
|
|
return nil, 0
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
buf := make([]byte, 100)
|
|
|
|
n, _ := f.fd.ReadAt(buf, 0)
|
|
|
|
buf = buf[:n]
|
|
|
|
hexdump := hex.EncodeToString(buf)
|
|
|
|
tlog.Warn.Printf("doRead %d: corrupt header: %v\nFile hexdump (%d bytes): %s",
|
|
|
|
f.qIno.Ino, err, n, hexdump)
|
2020-07-12 12:59:01 +02:00
|
|
|
return nil, syscall.EIO
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
// Save into the file table
|
|
|
|
f.fileTableEntry.ID = fileID
|
|
|
|
}
|
|
|
|
f.fileTableEntry.IDLock.Unlock()
|
|
|
|
if fileID == nil {
|
|
|
|
log.Panicf("fileID=%v", fileID)
|
|
|
|
}
|
|
|
|
// Read the backing ciphertext in one go
|
|
|
|
blocks := f.contentEnc.ExplodePlainRange(off, length)
|
|
|
|
alignedOffset, alignedLength := blocks[0].JointCiphertextRange(blocks)
|
2021-06-02 18:20:05 +02:00
|
|
|
// f.fd.ReadAt takes an int64!
|
|
|
|
if alignedOffset > math.MaxInt64 {
|
|
|
|
return nil, syscall.EFBIG
|
|
|
|
}
|
2020-06-21 13:25:12 +02:00
|
|
|
skip := blocks[0].Skip
|
|
|
|
tlog.Debug.Printf("doRead: off=%d len=%d -> off=%d len=%d skip=%d\n",
|
|
|
|
off, length, alignedOffset, alignedLength, skip)
|
|
|
|
|
|
|
|
ciphertext := f.rootNode.contentEnc.CReqPool.Get()
|
|
|
|
ciphertext = ciphertext[:int(alignedLength)]
|
|
|
|
n, err := f.fd.ReadAt(ciphertext, int64(alignedOffset))
|
|
|
|
if err != nil && err != io.EOF {
|
|
|
|
tlog.Warn.Printf("read: ReadAt: %s", err.Error())
|
2020-07-12 12:59:01 +02:00
|
|
|
return nil, fs.ToErrno(err)
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
// The ReadAt came back empty. We can skip all the decryption and return early.
|
|
|
|
if n == 0 {
|
|
|
|
f.rootNode.contentEnc.CReqPool.Put(ciphertext)
|
2020-07-12 12:59:01 +02:00
|
|
|
return dst, 0
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
// Truncate ciphertext buffer down to actually read bytes
|
|
|
|
ciphertext = ciphertext[0:n]
|
|
|
|
|
|
|
|
firstBlockNo := blocks[0].BlockNo
|
|
|
|
tlog.Debug.Printf("ReadAt offset=%d bytes (%d blocks), want=%d, got=%d", alignedOffset, firstBlockNo, alignedLength, n)
|
|
|
|
|
|
|
|
// Decrypt it
|
|
|
|
plaintext, err := f.contentEnc.DecryptBlocks(ciphertext, firstBlockNo, fileID)
|
|
|
|
f.rootNode.contentEnc.CReqPool.Put(ciphertext)
|
|
|
|
if err != nil {
|
|
|
|
if f.rootNode.args.ForceDecode && err == stupidgcm.ErrAuth {
|
|
|
|
// We do not have the information which block was corrupt here anymore,
|
|
|
|
// but DecryptBlocks() has already logged it anyway.
|
|
|
|
tlog.Warn.Printf("doRead %d: off=%d len=%d: returning corrupt data due to forcedecode",
|
|
|
|
f.qIno.Ino, off, length)
|
|
|
|
} else {
|
|
|
|
curruptBlockNo := firstBlockNo + f.contentEnc.PlainOffToBlockNo(uint64(len(plaintext)))
|
|
|
|
tlog.Warn.Printf("doRead %d: corrupt block #%d: %v", f.qIno.Ino, curruptBlockNo, err)
|
2020-07-12 12:59:01 +02:00
|
|
|
return nil, syscall.EIO
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Crop down to the relevant part
|
|
|
|
var out []byte
|
|
|
|
lenHave := len(plaintext)
|
|
|
|
lenWant := int(skip + length)
|
|
|
|
if lenHave > lenWant {
|
|
|
|
out = plaintext[skip:lenWant]
|
|
|
|
} else if lenHave > int(skip) {
|
|
|
|
out = plaintext[skip:lenHave]
|
|
|
|
}
|
|
|
|
// else: out stays empty, file was smaller than the requested offset
|
|
|
|
|
|
|
|
out = append(dst, out...)
|
|
|
|
f.rootNode.contentEnc.PReqPool.Put(plaintext)
|
|
|
|
|
2020-07-12 12:59:01 +02:00
|
|
|
return out, 0
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read - FUSE call
|
2020-08-16 12:48:16 +02:00
|
|
|
func (f *File) Read(ctx context.Context, buf []byte, off int64) (resultData fuse.ReadResult, errno syscall.Errno) {
|
2020-06-21 13:25:12 +02:00
|
|
|
if len(buf) > fuse.MAX_KERNEL_WRITE {
|
|
|
|
// This would crash us due to our fixed-size buffer pool
|
|
|
|
tlog.Warn.Printf("Read: rejecting oversized request with EMSGSIZE, len=%d", len(buf))
|
2020-07-12 12:59:01 +02:00
|
|
|
return nil, syscall.EMSGSIZE
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
f.fdLock.RLock()
|
|
|
|
defer f.fdLock.RUnlock()
|
|
|
|
|
|
|
|
f.fileTableEntry.ContentLock.RLock()
|
|
|
|
defer f.fileTableEntry.ContentLock.RUnlock()
|
|
|
|
|
|
|
|
tlog.Debug.Printf("ino%d: FUSE Read: offset=%d length=%d", f.qIno.Ino, off, len(buf))
|
|
|
|
if f.rootNode.args.SerializeReads {
|
|
|
|
serialize_reads.Wait(off, len(buf))
|
|
|
|
}
|
2020-07-12 12:59:01 +02:00
|
|
|
out, errno := f.doRead(buf[:0], uint64(off), uint64(len(buf)))
|
2020-06-21 13:25:12 +02:00
|
|
|
if f.rootNode.args.SerializeReads {
|
|
|
|
serialize_reads.Done()
|
|
|
|
}
|
2020-07-12 12:59:01 +02:00
|
|
|
if errno != 0 {
|
|
|
|
return nil, errno
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
2020-07-12 12:59:01 +02:00
|
|
|
tlog.Debug.Printf("ino%d: Read: errno=%d, returning %d bytes", f.qIno.Ino, errno, len(out))
|
|
|
|
return fuse.ReadResultData(out), errno
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// doWrite - encrypt "data" and write it to plaintext offset "off"
|
|
|
|
//
|
|
|
|
// Arguments do not have to be block-aligned, read-modify-write is
|
|
|
|
// performed internally as necessary
|
|
|
|
//
|
|
|
|
// Called by Write() for normal writing,
|
|
|
|
// and by Truncate() to rewrite the last file block.
|
|
|
|
//
|
|
|
|
// Empty writes do nothing and are allowed.
|
2020-08-16 12:48:16 +02:00
|
|
|
func (f *File) doWrite(data []byte, off int64) (uint32, syscall.Errno) {
|
2020-06-21 13:25:12 +02:00
|
|
|
fileWasEmpty := false
|
|
|
|
// The caller has exclusively locked ContentLock, which blocks all other
|
|
|
|
// readers and writers. No need to take IDLock.
|
2021-08-18 15:47:17 +02:00
|
|
|
//
|
|
|
|
// If the file ID is not cached, read it from disk
|
|
|
|
if f.fileTableEntry.ID == nil {
|
2020-06-21 13:25:12 +02:00
|
|
|
var err error
|
2021-08-18 15:47:17 +02:00
|
|
|
fileID, err := f.readFileID()
|
2020-06-21 13:25:12 +02:00
|
|
|
// Write a new file header if the file is empty
|
|
|
|
if err == io.EOF {
|
|
|
|
fileID, err = f.createHeader()
|
|
|
|
fileWasEmpty = true
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-07-12 12:59:01 +02:00
|
|
|
return 0, fs.ToErrno(err)
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
f.fileTableEntry.ID = fileID
|
|
|
|
}
|
|
|
|
// Handle payload data
|
|
|
|
dataBuf := bytes.NewBuffer(data)
|
|
|
|
blocks := f.contentEnc.ExplodePlainRange(uint64(off), uint64(len(data)))
|
|
|
|
toEncrypt := make([][]byte, len(blocks))
|
|
|
|
for i, b := range blocks {
|
|
|
|
blockData := dataBuf.Next(int(b.Length))
|
|
|
|
// Incomplete block -> Read-Modify-Write
|
|
|
|
if b.IsPartial() {
|
|
|
|
// Read
|
2020-07-12 12:59:01 +02:00
|
|
|
oldData, errno := f.doRead(nil, b.BlockPlainOff(), f.contentEnc.PlainBS())
|
|
|
|
if errno != 0 {
|
|
|
|
tlog.Warn.Printf("ino%d fh%d: RMW read failed: errno=%d", f.qIno.Ino, f.intFd(), errno)
|
|
|
|
return 0, errno
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
// Modify
|
|
|
|
blockData = f.contentEnc.MergeBlocks(oldData, blockData, int(b.Skip))
|
|
|
|
tlog.Debug.Printf("len(oldData)=%d len(blockData)=%d", len(oldData), len(blockData))
|
|
|
|
}
|
|
|
|
tlog.Debug.Printf("ino%d: Writing %d bytes to block #%d",
|
|
|
|
f.qIno.Ino, len(blockData), b.BlockNo)
|
|
|
|
// Write into the to-encrypt list
|
|
|
|
toEncrypt[i] = blockData
|
|
|
|
}
|
|
|
|
// Encrypt all blocks
|
|
|
|
ciphertext := f.contentEnc.EncryptBlocks(toEncrypt, blocks[0].BlockNo, f.fileTableEntry.ID)
|
|
|
|
// Preallocate so we cannot run out of space in the middle of the write.
|
|
|
|
// This prevents partially written (=corrupt) blocks.
|
|
|
|
var err error
|
2021-06-02 18:20:05 +02:00
|
|
|
cOff := blocks[0].BlockCipherOff()
|
|
|
|
// f.fd.WriteAt & syscallcompat.EnospcPrealloc take int64 offsets!
|
|
|
|
if cOff > math.MaxInt64 {
|
|
|
|
return 0, syscall.EFBIG
|
|
|
|
}
|
2020-06-21 13:25:12 +02:00
|
|
|
if !f.rootNode.args.NoPrealloc {
|
2021-06-02 18:20:05 +02:00
|
|
|
err = syscallcompat.EnospcPrealloc(f.intFd(), int64(cOff), int64(len(ciphertext)))
|
2020-06-21 13:25:12 +02:00
|
|
|
if err != nil {
|
|
|
|
if !syscallcompat.IsENOSPC(err) {
|
|
|
|
tlog.Warn.Printf("ino%d fh%d: doWrite: prealloc failed: %v", f.qIno.Ino, f.intFd(), err)
|
|
|
|
}
|
|
|
|
if fileWasEmpty {
|
|
|
|
// Kill the file header again
|
|
|
|
f.fileTableEntry.ID = nil
|
|
|
|
err2 := syscall.Ftruncate(f.intFd(), 0)
|
|
|
|
if err2 != nil {
|
|
|
|
tlog.Warn.Printf("ino%d fh%d: doWrite: rollback failed: %v", f.qIno.Ino, f.intFd(), err2)
|
|
|
|
}
|
|
|
|
}
|
2020-07-12 12:59:01 +02:00
|
|
|
return 0, fs.ToErrno(err)
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Write
|
2021-06-02 18:20:05 +02:00
|
|
|
_, err = f.fd.WriteAt(ciphertext, int64(cOff))
|
2020-06-21 13:25:12 +02:00
|
|
|
// Return memory to CReqPool
|
|
|
|
f.rootNode.contentEnc.CReqPool.Put(ciphertext)
|
|
|
|
if err != nil {
|
|
|
|
tlog.Warn.Printf("ino%d fh%d: doWrite: WriteAt off=%d len=%d failed: %v",
|
|
|
|
f.qIno.Ino, f.intFd(), cOff, len(ciphertext), err)
|
2020-07-12 12:59:01 +02:00
|
|
|
return 0, fs.ToErrno(err)
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
2020-07-12 12:59:01 +02:00
|
|
|
return uint32(len(data)), 0
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// isConsecutiveWrite returns true if the current write
|
|
|
|
// directly (in time and space) follows the last write.
|
|
|
|
// This is an optimisation for streaming writes on NFS where a
|
|
|
|
// Stat() call is very expensive.
|
|
|
|
// The caller must "wlock.lock(f.devIno.ino)" otherwise this check would be racy.
|
2020-08-16 12:48:16 +02:00
|
|
|
func (f *File) isConsecutiveWrite(off int64) bool {
|
2020-06-21 13:25:12 +02:00
|
|
|
opCount := openfiletable.WriteOpCount()
|
|
|
|
return opCount == f.lastOpCount+1 && off == f.lastWrittenOffset+1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write - FUSE call
|
|
|
|
//
|
|
|
|
// If the write creates a hole, pads the file to the next block boundary.
|
2020-08-16 12:48:16 +02:00
|
|
|
func (f *File) Write(ctx context.Context, data []byte, off int64) (uint32, syscall.Errno) {
|
2020-06-21 13:25:12 +02:00
|
|
|
if len(data) > fuse.MAX_KERNEL_WRITE {
|
|
|
|
// This would crash us due to our fixed-size buffer pool
|
|
|
|
tlog.Warn.Printf("Write: rejecting oversized request with EMSGSIZE, len=%d", len(data))
|
2020-07-12 12:59:01 +02:00
|
|
|
return 0, syscall.EMSGSIZE
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
f.fdLock.RLock()
|
|
|
|
defer f.fdLock.RUnlock()
|
|
|
|
if f.released {
|
|
|
|
// The file descriptor has been closed concurrently
|
|
|
|
tlog.Warn.Printf("ino%d fh%d: Write on released file", f.qIno.Ino, f.intFd())
|
2020-07-12 12:59:01 +02:00
|
|
|
return 0, syscall.EBADF
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
f.fileTableEntry.ContentLock.Lock()
|
|
|
|
defer f.fileTableEntry.ContentLock.Unlock()
|
|
|
|
tlog.Debug.Printf("ino%d: FUSE Write: offset=%d length=%d", f.qIno.Ino, off, len(data))
|
|
|
|
// If the write creates a file hole, we have to zero-pad the last block.
|
|
|
|
// But if the write directly follows an earlier write, it cannot create a
|
|
|
|
// hole, and we can save one Stat() call.
|
|
|
|
if !f.isConsecutiveWrite(off) {
|
2020-07-12 12:59:01 +02:00
|
|
|
errno := f.writePadHole(off)
|
|
|
|
if errno != 0 {
|
|
|
|
return 0, errno
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-12 12:59:01 +02:00
|
|
|
n, errno := f.doWrite(data, off)
|
|
|
|
if errno != 0 {
|
2020-06-21 13:25:12 +02:00
|
|
|
f.lastOpCount = openfiletable.WriteOpCount()
|
|
|
|
f.lastWrittenOffset = off + int64(len(data)) - 1
|
|
|
|
}
|
2020-07-12 12:59:01 +02:00
|
|
|
return n, errno
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Release - FUSE call, close file
|
2020-08-16 12:48:16 +02:00
|
|
|
func (f *File) Release(ctx context.Context) syscall.Errno {
|
2020-06-21 13:25:12 +02:00
|
|
|
f.fdLock.Lock()
|
|
|
|
if f.released {
|
|
|
|
log.Panicf("ino%d fh%d: double release", f.qIno.Ino, f.intFd())
|
|
|
|
}
|
|
|
|
f.released = true
|
|
|
|
openfiletable.Unregister(f.qIno)
|
2020-10-14 13:40:12 +02:00
|
|
|
err := f.fd.Close()
|
2020-06-21 13:25:12 +02:00
|
|
|
f.fdLock.Unlock()
|
2020-10-14 13:40:12 +02:00
|
|
|
return fs.ToErrno(err)
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flush - FUSE call
|
2020-08-16 12:48:16 +02:00
|
|
|
func (f *File) Flush(ctx context.Context) syscall.Errno {
|
2020-06-21 13:25:12 +02:00
|
|
|
f.fdLock.RLock()
|
|
|
|
defer f.fdLock.RUnlock()
|
|
|
|
|
2020-10-14 13:40:12 +02:00
|
|
|
err := syscallcompat.Flush(f.intFd())
|
2020-07-12 12:59:01 +02:00
|
|
|
return fs.ToErrno(err)
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
|
2021-07-29 20:39:50 +02:00
|
|
|
// Fsync: handles FUSE opcode FSYNC
|
|
|
|
//
|
|
|
|
// Unfortunately, as Node.Fsync is also defined and takes precedence,
|
|
|
|
// File.Fsync is never called at the moment.
|
2020-08-16 12:48:16 +02:00
|
|
|
func (f *File) Fsync(ctx context.Context, flags uint32) (errno syscall.Errno) {
|
2020-06-21 13:25:12 +02:00
|
|
|
f.fdLock.RLock()
|
|
|
|
defer f.fdLock.RUnlock()
|
|
|
|
|
2020-07-12 12:59:01 +02:00
|
|
|
return fs.ToErrno(syscall.Fsync(f.intFd()))
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
|
2020-07-05 20:05:07 +02:00
|
|
|
// Getattr FUSE call (like stat)
|
2020-08-16 12:48:16 +02:00
|
|
|
func (f *File) Getattr(ctx context.Context, a *fuse.AttrOut) syscall.Errno {
|
2020-06-21 13:25:12 +02:00
|
|
|
f.fdLock.RLock()
|
|
|
|
defer f.fdLock.RUnlock()
|
|
|
|
|
|
|
|
tlog.Debug.Printf("file.GetAttr()")
|
|
|
|
st := syscall.Stat_t{}
|
|
|
|
err := syscall.Fstat(f.intFd(), &st)
|
|
|
|
if err != nil {
|
2020-07-05 20:05:07 +02:00
|
|
|
return fs.ToErrno(err)
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|
|
|
|
f.rootNode.inoMap.TranslateStat(&st)
|
|
|
|
a.FromStat(&st)
|
|
|
|
a.Size = f.contentEnc.CipherSizeToPlainSize(a.Size)
|
|
|
|
if f.rootNode.args.ForceOwner != nil {
|
|
|
|
a.Owner = *f.rootNode.args.ForceOwner
|
|
|
|
}
|
|
|
|
|
2020-07-05 20:05:07 +02:00
|
|
|
return 0
|
2020-06-21 13:25:12 +02:00
|
|
|
}
|