aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorJakob Unterwurzacher2026-11-19 20:53:14 +0100
committerJakob Unterwurzacher2026-11-19 21:16:19 +0100
commit52c4864d849cbbb76d49d1a908025b77c7246bda (patch)
tree265f05f143e2129a18359165832e367cfba47c11 /internal
parent43e17fc6488ac088b51758a81dbc039f7c158f6a (diff)
fusefrontend: report mitigated corruptions via the context
Replace the RootNode.MitigatedCorruptions channel with a callback that fsck attaches to the context of each operation via fusefrontend.WithCorruptionReporter(). The channel was needed when fsck went through the kernel, but now that fsck calls the node operations directly, the context is a better way: * Each report is attributed to the operation that caused it, so fsck no longer needs a watcher goroutine and the watchDone channel around every call. * reportMitigatedCorruption() no longer blocks for up to one second (and logs "BUG: reportCorruptItem: timeout") when nobody is listening. * A mounted filesystem has no reporter in the context, so reporting is a no-op there. readFileID() and doRead() now take a ctx. The write paths pass context.Background(), as fsck never writes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'internal')
-rw-r--r--internal/fusefrontend/file.go21
-rw-r--r--internal/fusefrontend/file_allocate_truncate.go2
-rw-r--r--internal/fusefrontend/file_dir_ops.go4
-rw-r--r--internal/fusefrontend/node_xattr.go4
-rw-r--r--internal/fusefrontend/root_node.go56
5 files changed, 50 insertions, 37 deletions
diff --git a/internal/fusefrontend/file.go b/internal/fusefrontend/file.go
index feb40b5..8eee0f8 100644
--- a/internal/fusefrontend/file.go
+++ b/internal/fusefrontend/file.go
@@ -87,7 +87,9 @@ func (f *File) intFd() int {
// readFileID loads the file header from disk and extracts the file ID.
// Returns io.EOF if the file is empty.
-func (f *File) readFileID() ([]byte, error) {
+//
+// ctx is only used to report mitigated corruptions, see WithCorruptionReporter.
+func (f *File) readFileID(ctx context.Context) ([]byte, error) {
// 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.
@@ -105,7 +107,7 @@ func (f *File) readFileID() ([]byte, error) {
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))
+ reportMitigatedCorruption(ctx, fmt.Sprint(f.qIno.Ino))
}
return nil, err
}
@@ -150,7 +152,10 @@ func (f *File) createHeader() (fileID []byte, err error) {
//
// Called by Read() for normal reading,
// by Write() and Truncate() via doWrite() for Read-Modify-Write.
-func (f *File) doRead(dst []byte, off uint64, length uint64) ([]byte, syscall.Errno) {
+//
+// ctx is only used to report mitigated corruptions, see WithCorruptionReporter.
+// The write paths pass context.Background(), as fsck never writes.
+func (f *File) doRead(ctx context.Context, dst []byte, off uint64, length uint64) ([]byte, syscall.Errno) {
// Get the file ID, either from the open file table, or from disk.
var fileID []byte
f.fileTableEntry.IDLock.Lock()
@@ -160,7 +165,7 @@ func (f *File) doRead(dst []byte, off uint64, length uint64) ([]byte, syscall.Er
} else {
// Not cached, we have to read it from disk.
var err error
- fileID, err = f.readFileID()
+ fileID, err = f.readFileID(ctx)
if err != nil {
f.fileTableEntry.IDLock.Unlock()
if err == io.EOF {
@@ -251,7 +256,7 @@ func (f *File) Read(ctx context.Context, buf []byte, off int64) (resultData fuse
defer f.fileTableEntry.ContentLock.RUnlock()
tlog.Debug.Printf("ino%d: FUSE Read: offset=%d length=%d", f.qIno.Ino, off, len(buf))
- out, errno := f.doRead(buf[:0], uint64(off), uint64(len(buf)))
+ out, errno := f.doRead(ctx, buf[:0], uint64(off), uint64(len(buf)))
if errno != 0 {
// With -sharedstorage, when we get a decryption error, we lock the
// byte range and try again.
@@ -266,7 +271,7 @@ func (f *File) Read(ctx context.Context, buf []byte, off int64) (resultData fuse
}
defer f.UnlockSharedStorage(int64(alignedOffset), int64(alignedLength))
- out, errno = f.doRead(buf[:0], uint64(off), uint64(len(buf)))
+ out, errno = f.doRead(ctx, buf[:0], uint64(off), uint64(len(buf)))
if errno != 0 {
return nil, errno
}
@@ -303,7 +308,7 @@ func (f *File) loadOrCreateFileID() (created bool, errno syscall.Errno) {
}
defer f.UnlockSharedStorage(0, contentenc.HeaderLen)
- fileID, err := f.readFileID()
+ fileID, err := f.readFileID(context.Background())
// Write a new file header if the file is empty
if err == io.EOF {
fileID, err = f.createHeader()
@@ -347,7 +352,7 @@ func (f *File) doWrite(data []byte, off int64) (uint32, syscall.Errno) {
// Incomplete block -> Read-Modify-Write
if b.IsPartial() {
// Read
- oldData, errno := f.doRead(nil, b.BlockPlainOff(), f.rootNode.contentEnc.PlainBS())
+ oldData, errno := f.doRead(context.Background(), nil, b.BlockPlainOff(), f.rootNode.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
diff --git a/internal/fusefrontend/file_allocate_truncate.go b/internal/fusefrontend/file_allocate_truncate.go
index b7b2651..19b7697 100644
--- a/internal/fusefrontend/file_allocate_truncate.go
+++ b/internal/fusefrontend/file_allocate_truncate.go
@@ -179,7 +179,7 @@ func (f *File) truncate(newSize uint64) (errno syscall.Errno) {
lastBlockLen := newSize - plainOff
var data []byte
if lastBlockLen > 0 {
- data, errno = f.doRead(nil, plainOff, lastBlockLen)
+ data, errno = f.doRead(context.Background(), nil, plainOff, lastBlockLen)
if errno != 0 {
tlog.Warn.Printf("Truncate: shrink doRead returned error: %v", err)
return errno
diff --git a/internal/fusefrontend/file_dir_ops.go b/internal/fusefrontend/file_dir_ops.go
index b69e7bc..683da87 100644
--- a/internal/fusefrontend/file_dir_ops.go
+++ b/internal/fusefrontend/file_dir_ops.go
@@ -154,7 +154,7 @@ func (f *File) Readdirent(ctx context.Context) (entry *fuse.DirEntry, errno sysc
if err != nil {
tlog.Warn.Printf("Readdirent: incomplete entry %q: Could not read .name: %v",
cName, err)
- f.rootNode.reportMitigatedCorruption(cName)
+ reportMitigatedCorruption(ctx, cName)
continue
}
cName = cNameLong
@@ -166,7 +166,7 @@ func (f *File) Readdirent(ctx context.Context) (entry *fuse.DirEntry, errno sysc
if err != nil {
tlog.Warn.Printf("Readdirent: could not decrypt entry %q: %v",
cName, err)
- f.rootNode.reportMitigatedCorruption(cName)
+ reportMitigatedCorruption(ctx, cName)
continue
}
// Override the ciphertext name with the plaintext name but reuse the rest
diff --git a/internal/fusefrontend/node_xattr.go b/internal/fusefrontend/node_xattr.go
index 1470a2a..cbb0c37 100644
--- a/internal/fusefrontend/node_xattr.go
+++ b/internal/fusefrontend/node_xattr.go
@@ -153,13 +153,13 @@ func (n *Node) Listxattr(ctx context.Context, dest []byte) (uint32, syscall.Errn
name, err := rn.decryptXattrName(curName)
if err != nil {
tlog.Warn.Printf("ListXAttr: invalid xattr name %q: %v", curName, err)
- rn.reportMitigatedCorruption(curName)
+ reportMitigatedCorruption(ctx, curName)
continue
}
// We *used to* encrypt ACLs, which caused a lot of problems.
if isAcl(name) {
tlog.Warn.Printf("ListXAttr: ignoring deprecated encrypted ACL %q = %q", curName, name)
- rn.reportMitigatedCorruption(curName)
+ reportMitigatedCorruption(ctx, curName)
continue
}
buf.WriteString(name + "\000")
diff --git a/internal/fusefrontend/root_node.go b/internal/fusefrontend/root_node.go
index fa8781b..915e3e4 100644
--- a/internal/fusefrontend/root_node.go
+++ b/internal/fusefrontend/root_node.go
@@ -1,11 +1,11 @@
package fusefrontend
import (
+ "context"
"strings"
"sync"
"sync/atomic"
"syscall"
- "time"
"github.com/rfjakob/gocryptfs/v2/internal/configfile"
"github.com/rfjakob/gocryptfs/v2/internal/contentenc"
@@ -31,15 +31,6 @@ type RootNode struct {
// This lock is used by openWriteOnlyFile() to block concurrent opens while
// it relaxes the permissions on a file.
openWriteOnlyLock sync.RWMutex
- // MitigatedCorruptions is used to report data corruption that is internally
- // mitigated by ignoring the corrupt item. For example, when OpenDir() finds
- // a corrupt filename, we still return the other valid filenames.
- // The corruption is logged to syslog to inform the user, and in addition,
- // the corrupt filename is logged to this channel via
- // reportMitigatedCorruption().
- // "gocryptfs -fsck" reads from the channel to also catch these transparently-
- // mitigated corruptions.
- MitigatedCorruptions chan string
// IsIdle flag is set to zero each time fs.isFiltered() is called
// (uint32 so that it can be reset with CompareAndSwapUint32).
// When -idle was used when mounting, idleMonitor() sets it to 1
@@ -109,20 +100,37 @@ func (rn *RootNode) AfterUnmount() {
rn.dirCache.stats()
}
-// reportMitigatedCorruption is used to report a corruption that was transparently
-// mitigated and did not return an error to the user. Pass the name of the corrupt
-// item (filename for OpenDir(), xattr name for ListXAttr() etc).
-// See the MitigatedCorruptions channel for more info.
-func (rn *RootNode) reportMitigatedCorruption(item string) {
- if rn.MitigatedCorruptions == nil {
- return
- }
- select {
- case rn.MitigatedCorruptions <- item:
- case <-time.After(1 * time.Second):
- tlog.Warn.Printf("BUG: reportCorruptItem: timeout")
- //debug.PrintStack()
- return
+type corruptionReporterKey struct{}
+
+// WithCorruptionReporter returns a copy of ctx that carries `report`. Pass it
+// to node and file operations to have mitigated corruptions reported to
+// `report`.
+//
+// A mitigated corruption is one that is handled internally by ignoring the
+// corrupt item, and hence does not return an error. For example, when
+// Readdirent() finds a corrupt filename, we still return the other valid
+// filenames.
+//
+// The corruption is always logged to inform the user. "gocryptfs -fsck"
+// additionally uses this to also catch these transparently-mitigated
+// corruptions.
+//
+// `item` is the name of the corrupt item: the ciphertext filename for
+// Readdirent(), the ciphertext xattr name for Listxattr(), and the inode
+// number for Read().
+//
+// `report` is called synchronously from the goroutine that runs the
+// operation.
+func WithCorruptionReporter(ctx context.Context, report func(item string)) context.Context {
+ return context.WithValue(ctx, corruptionReporterKey{}, report)
+}
+
+// reportMitigatedCorruption reports a corruption that was transparently
+// mitigated to the reporter in ctx, if there is one.
+// See WithCorruptionReporter for more info.
+func reportMitigatedCorruption(ctx context.Context, item string) {
+ if report, ok := ctx.Value(corruptionReporterKey{}).(func(item string)); ok {
+ report(item)
}
}