diff options
| -rw-r--r-- | fsck.go | 79 | ||||
| -rw-r--r-- | internal/fusefrontend/file.go | 21 | ||||
| -rw-r--r-- | internal/fusefrontend/file_allocate_truncate.go | 2 | ||||
| -rw-r--r-- | internal/fusefrontend/file_dir_ops.go | 4 | ||||
| -rw-r--r-- | internal/fusefrontend/node_xattr.go | 4 | ||||
| -rw-r--r-- | internal/fusefrontend/root_node.go | 56 |
6 files changed, 70 insertions, 96 deletions
@@ -23,15 +23,12 @@ import ( // fsckObj walks the filesystem by calling the fusefrontend node operations // directly, without mounting it via FUSE. type fsckObj struct { - rootNode *fusefrontend.RootNode // List of corrupt files corruptList []string // List of skipped files skippedList []string // Protects corruptList listLock sync.Mutex - // stop a running watchMitigatedCorruptions thread - watchDone chan struct{} // Inode numbers of hard-linked files (Nlink > 1) that we have already checked seenInodes map[uint64]struct{} // abort the running fsck operation? Checked in a few long-running loops. @@ -64,27 +61,16 @@ func (ck *fsckObj) markError(relPath string, err error) { } } -// Watch for mitigated corruptions that occur during OpenDir() -func (ck *fsckObj) watchMitigatedCorruptionsOpenDir(path string) { - for { - select { - case item := <-ck.rootNode.MitigatedCorruptions: - fmt.Printf("fsck: corrupt entry in dir %q: %q\n", path, item) - ck.markCorrupt(filepath.Join(path, item)) - case <-ck.watchDone: - return - } - } -} - // Recursively check dir for corruption func (ck *fsckObj) dir(inode *fs.Inode, relPath string) { tlog.Debug.Printf("ck.dir %q\n", relPath) ck.xattrs(inode, relPath) - // Run OpendirHandle and Readdirent and catch transparently mitigated corruptions - go ck.watchMitigatedCorruptionsOpenDir(relPath) - entries, errno := ck.readdir(inode, relPath) - ck.watchDone <- struct{}{} + // Catch corrupt entries that Readdirent transparently skips + ctx := fusefrontend.WithCorruptionReporter(context.Background(), func(item string) { + fmt.Printf("fsck: corrupt entry in dir %q: %q\n", relPath, item) + ck.markCorrupt(filepath.Join(relPath, item)) + }) + entries, errno := ck.readdir(ctx, inode, relPath) if errno != 0 { ck.markError(relPath, errno) return @@ -126,15 +112,15 @@ func (ck *fsckObj) dir(inode *fs.Inode, relPath string) { } // readdir returns the names of all entries in the directory, without "." and "..". -func (ck *fsckObj) readdir(inode *fs.Inode, relPath string) (entries []string, errno syscall.Errno) { - fh, _, errno := inode.Operations().(fs.NodeOpendirHandler).OpendirHandle(context.Background(), syscall.O_RDONLY) +func (ck *fsckObj) readdir(ctx context.Context, inode *fs.Inode, relPath string) (entries []string, errno syscall.Errno) { + fh, _, errno := inode.Operations().(fs.NodeOpendirHandler).OpendirHandle(ctx, syscall.O_RDONLY) if errno != 0 { fmt.Printf("fsck: error opening dir %q: %v\n", relPath, errno) return nil, errno } - defer fh.(fs.FileReleasedirer).Releasedir(context.Background(), 0) + defer fh.(fs.FileReleasedirer).Releasedir(ctx, 0) for { - entry, errno := fh.(fs.FileReaddirenter).Readdirent(context.Background()) + entry, errno := fh.(fs.FileReaddirenter).Readdirent(ctx) if errno != 0 { fmt.Printf("fsck: error reading dir %q: %v\n", relPath, errno) return nil, errno @@ -157,19 +143,6 @@ func (ck *fsckObj) symlink(inode *fs.Inode, relPath string) { } } -// Watch for mitigated corruptions that occur during Read() -func (ck *fsckObj) watchMitigatedCorruptionsRead(path string) { - for { - select { - case item := <-ck.rootNode.MitigatedCorruptions: - fmt.Printf("fsck: corrupt file %q (inode %s)\n", path, item) - ck.markCorrupt(path) - case <-ck.watchDone: - return - } - } -} - // Check file for corruption func (ck *fsckObj) file(inode *fs.Inode, relPath string, attr *fuse.Attr) { tlog.Debug.Printf("ck.file %q\n", relPath) @@ -195,14 +168,16 @@ func (ck *fsckObj) file(inode *fs.Inode, relPath string, attr *fuse.Attr) { buf := make([]byte, fuse.MAX_KERNEL_WRITE) var off int64 // Read() through the whole file and catch transparently mitigated corruptions - go ck.watchMitigatedCorruptionsRead(relPath) - defer func() { ck.watchDone <- struct{}{} }() + ctx := fusefrontend.WithCorruptionReporter(context.Background(), func(item string) { + fmt.Printf("fsck: corrupt file %q (inode %s)\n", relPath, item) + ck.markCorrupt(relPath) + }) for { if ck.abort { return } tlog.Debug.Printf("ck.file: read %d bytes from offset %d\n", len(buf), off) - res, errno := reader.Read(context.Background(), buf, off) + res, errno := reader.Read(ctx, buf, off) if errno != 0 { ck.markCorrupt(relPath) fmt.Printf("fsck: error reading file %q (inum %d): %v\n", relPath, attr.Ino, errno) @@ -236,28 +211,17 @@ func (ck *fsckObj) file(inode *fs.Inode, relPath string, attr *fuse.Attr) { } } -// Watch for mitigated corruptions that occur during ListXAttr() -func (ck *fsckObj) watchMitigatedCorruptionsListXAttr(path string) { - for { - select { - case item := <-ck.rootNode.MitigatedCorruptions: - fmt.Printf("fsck: corrupt xattr name on file %q: %q\n", path, item) - ck.markCorrupt(path + " xattr:" + item) - case <-ck.watchDone: - return - } - } -} - // Check xattrs on file/dir func (ck *fsckObj) xattrs(inode *fs.Inode, relPath string) { ops := inode.Operations() // Run Listxattr() and catch transparently mitigated corruptions - go ck.watchMitigatedCorruptionsListXAttr(relPath) + ctx := fusefrontend.WithCorruptionReporter(context.Background(), func(item string) { + fmt.Printf("fsck: corrupt xattr name on file %q: %q\n", relPath, item) + ck.markCorrupt(relPath + " xattr:" + item) + }) list, errno := getBuf(func(dest []byte) (uint32, syscall.Errno) { - return ops.(fs.NodeListxattrer).Listxattr(context.Background(), dest) + return ops.(fs.NodeListxattrer).Listxattr(ctx, dest) }) - ck.watchDone <- struct{}{} if errno != 0 { fmt.Printf("fsck: error listing xattrs on %q: %v\n", relPath, errno) ck.markCorrupt(relPath) @@ -308,10 +272,7 @@ func fsck(args *argContainer) (exitcode int) { // We never mount the filesystem, but NewNodeFS initializes the root // inode, which is required for the node operations to work. _ = fs.NewNodeFS(rn, nil) - rn.MitigatedCorruptions = make(chan string) ck := fsckObj{ - rootNode: rn, - watchDone: make(chan struct{}), seenInodes: make(map[uint64]struct{}), } // Handle SIGINT & SIGTERM 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) } } |
