Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package fuse

import (
"context"
"errors"
"fmt"
"io"
"log"
Expand All @@ -29,6 +30,7 @@ import (
"github.com/jacobsa/fuse/internal/buffer"
"github.com/jacobsa/fuse/internal/freelist"
"github.com/jacobsa/fuse/internal/fusekernel"
"github.com/jacobsa/fuse/internal/operror"
)

type contextKeyType uint64
Expand Down Expand Up @@ -438,10 +440,13 @@ func (c *Connection) writeOutMessage(outMsg *buffer.OutMessage) error {
return err
}

// syscallWrite is syscall.Write. Tests replace it to make a reply write fail.
var syscallWrite = syscall.Write

// Write the supplied message to the kernel.
func (c *Connection) writeMessage(msg []byte) error {
// Avoid the retry loop in os.File.Write.
n, err := syscall.Write(int(c.dev.Fd()), msg)
n, err := syscallWrite(int(c.dev.Fd()), msg)
if err != nil {
return err
}
Expand Down Expand Up @@ -519,21 +524,24 @@ func (c *Connection) shouldLogError(
return false
}

// Judge the errno that kernelResponse sends, not any errno in the chain.
errno, _ := operror.Errno(err)

switch op.(type) {
case *fuseops.LookUpInodeOp:
// It is totally normal for the kernel to ask to look up an inode by name
// and find the name doesn't exist. For example, this happens when linking
// a new file.
if err == syscall.ENOENT {
if errno == syscall.ENOENT {
return false
}
case *fuseops.GetXattrOp, *fuseops.ListXattrOp:
if err == syscall.ENOSYS || err == syscall.ENODATA || err == syscall.ERANGE {
if errno == syscall.ENOSYS || errno == syscall.ENODATA || errno == syscall.ERANGE {
return false
}
case *unknownOp:
// Don't bother the user with methods we intentionally don't support.
if err == syscall.ENOSYS {
if errno == syscall.ENOSYS {
return false
}
}
Expand All @@ -546,6 +554,10 @@ var writeLock sync.Mutex
// Reply replies to an op previously read using ReadOp, with the supplied error
// (or nil if successful). The context must be the context returned by ReadOp.
//
// The kernel receives the first syscall.Errno in opErr's chain, or EIO when
// the chain has none. A reply the kernel refuses with ENOENT, because the
// request is gone, is not an error.
//
// LOCKS_EXCLUDED(c.mu)
func (c *Connection) Reply(ctx context.Context, opErr error) error {
// Extract the state we stuffed in earlier.
Expand Down Expand Up @@ -585,7 +597,7 @@ func (c *Connection) Reply(ctx context.Context, opErr error) error {
c.debugLog(fuseID, 1, "-> %s", describeResponse(op))
} else {
if !logError {
c.debugLog(fuseID, 1, "-> Error: %q", opErr.Error())
c.debugLog(fuseID, 1, "-> Error: %q", opErr)
}
}
}
Expand All @@ -600,7 +612,10 @@ func (c *Connection) Reply(ctx context.Context, opErr error) error {

if !noResponse {
err := c.writeOutMessage(outMsg)
if err != nil {
// The kernel answers a reply to a request it no longer tracks with
// ENOENT. libfuse leaves it out of its error output, and Reply neither
// logs nor returns it.
if err != nil && !errors.Is(err, syscall.ENOENT) {
writeErrMsg := fmt.Sprintf("writeMessage: %v %v", err, outMsg.OutHeaderBytes())
if c.errorLogger != nil {
c.errorLogger.Print(writeErrMsg)
Expand Down
200 changes: 200 additions & 0 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,212 @@
package fuse

import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"os"
"strings"
"syscall"
"testing"
"unsafe"

"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/fuse/internal/buffer"
"github.com/jacobsa/fuse/internal/fusekernel"
)

// A nil *os.PathError panics when errors.As or errors.Is unwraps it.
var nilPathErr error = (*os.PathError)(nil)

// newReplyTestConnection returns a Connection that reads one unknown op from
// a pipe. Its reply writes return writeErr.
func newReplyTestConnection(t *testing.T, writeErr error) *Connection {
t.Helper()

message := make([]byte, unsafe.Sizeof(fusekernel.InHeader{}))
binary.NativeEndian.PutUint32(message[0:4], uint32(len(message)))
binary.NativeEndian.PutUint64(message[8:16], 7)

r, w, err := os.Pipe()
if err != nil {
t.Fatalf("Pipe: %v", err)
}
t.Cleanup(func() { r.Close() })
if _, err := w.Write(message); err != nil {
t.Fatalf("Write: %v", err)
}
w.Close()

oldWrite := syscallWrite
syscallWrite = func(fd int, p []byte) (int, error) {
if writeErr != nil {
return 0, writeErr
}
return len(p), nil
}
t.Cleanup(func() { syscallWrite = oldWrite })

return &Connection{
cfg: MountConfig{OpContext: context.Background()},
dev: r,
cancelFuncs: make(map[uint64]func()),
inMessageBufSize: buffer.GetPageSize(),
}
}

func TestKernelResponseKeepsWrappedErrno(t *testing.T) {
tests := []struct {
name string
opErr error
want syscall.Errno
}{
{"bare errno", syscall.ENOENT, syscall.ENOENT},
{"wrapped errno", fmt.Errorf("lookup: %w", syscall.ENOENT), syscall.ENOENT},
{"path error", &os.PathError{Op: "open", Path: "f", Err: syscall.EACCES}, syscall.EACCES},
{"no errno", errors.New("lookup failed"), syscall.EIO},
{"EIO before ENOENT", fmt.Errorf("lookup: %w (cleanup: %w)", syscall.EIO, syscall.ENOENT), syscall.EIO},
{"joined EACCES and ENOENT", errors.Join(syscall.EACCES, syscall.ENOENT), syscall.EACCES},
{"nil path error", nilPathErr, syscall.EIO},
{"wrapped nil path error", fmt.Errorf("lookup: %w", nilPathErr), syscall.EIO},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var c Connection
var m buffer.OutMessage
m.Reset()
c.kernelResponse(&m, 1, &fuseops.LookUpInodeOp{}, tt.opErr)
if got := m.OutHeader().Error; got != -int32(tt.want) {
t.Errorf("reply error = %d, want %d", got, -int32(tt.want))
}
})
}
}

func TestShouldLogErrorMatchesWrappedErrno(t *testing.T) {
wrap := func(errno syscall.Errno) error { return fmt.Errorf("op: %w", errno) }
tests := []struct {
name string
op interface{}
err error
want bool
}{
{"lookup ENOENT", &fuseops.LookUpInodeOp{}, wrap(syscall.ENOENT), false},
{"getxattr ENODATA", &fuseops.GetXattrOp{}, wrap(syscall.ENODATA), false},
{"listxattr ERANGE", &fuseops.ListXattrOp{}, wrap(syscall.ERANGE), false},
{"getxattr ENOSYS", &fuseops.GetXattrOp{}, wrap(syscall.ENOSYS), false},
{"unknown op ENOSYS", &unknownOp{}, wrap(syscall.ENOSYS), false},
{"lookup EIO", &fuseops.LookUpInodeOp{}, wrap(syscall.EIO), true},
{"lookup EIO before ENOENT", &fuseops.LookUpInodeOp{}, fmt.Errorf("lookup: %w (cleanup: %w)", syscall.EIO, syscall.ENOENT), true},
{"lookup joined EACCES and ENOENT", &fuseops.LookUpInodeOp{}, errors.Join(syscall.EACCES, syscall.ENOENT), true},
{"lookup no errno", &fuseops.LookUpInodeOp{}, errors.New("lookup failed"), true},
{"lookup nil path error", &fuseops.LookUpInodeOp{}, nilPathErr, true},
{"lookup wrapped nil path error", &fuseops.LookUpInodeOp{}, fmt.Errorf("lookup: %w", nilPathErr), true},
}
c := &Connection{errorLogger: log.New(io.Discard, "", 0)}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := c.shouldLogError(tt.op, tt.err); got != tt.want {
t.Errorf("shouldLogError = %v, want %v", got, tt.want)
}
})
}
}

// The kernel answers a reply to a request it no longer tracks with ENOENT.
func TestReplyTreatsENOENTWriteAsBenign(t *testing.T) {
tests := []struct {
name string
writeErr error
wantErr bool
}{
{"ENOENT", syscall.ENOENT, false},
{"EIO", syscall.EIO, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var logged bytes.Buffer
c := newReplyTestConnection(t, tt.writeErr)
c.errorLogger = log.New(&logged, "", 0)
ctx, _, err := c.ReadOp()
if err != nil {
t.Fatalf("ReadOp: %v", err)
}

err = c.Reply(ctx, syscall.ENOSYS)
if (err != nil) != tt.wantErr {
t.Errorf("Reply = %v, want error: %v", err, tt.wantErr)
}
if (logged.Len() != 0) != tt.wantErr {
t.Errorf("logged %q, want a log: %v", logged.String(), tt.wantErr)
}
})
}
}

func TestFormatWireLogEntryStatus(t *testing.T) {
tests := []struct {
name string
opErr error
want int
}{
{"wrapped errno", fmt.Errorf("lookup: %w", syscall.ENOENT), int(syscall.ENOENT)},
{"no errno", errors.New("lookup failed"), 0},
{"nil path error", nilPathErr, 0},
{"wrapped nil path error", fmt.Errorf("lookup: %w", nilPathErr), 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
wlog := NewWireLogRecord()
if _, err := formatWireLogEntry(&fuseops.LookUpInodeOp{}, tt.opErr, wlog); err != nil {
t.Fatalf("formatWireLogEntry: %v", err)
}
if wlog.Status != tt.want {
t.Errorf("Status = %d, want %d", wlog.Status, tt.want)
}
})
}
}

// fmt recovers from a panic in Error when it formats the error value itself.
func TestReplyDebugLogsOpError(t *testing.T) {
tests := []struct {
name string
opErr error
errorLogger bool
want string
}{
{"ordinary error", errors.New("lookup failed"), false, `-> Error: "lookup failed"`},
{"nil path error", nilPathErr, false, "-> Error: <nil>"},
{"suppressed errno joined with nil path error", errors.Join(syscall.ENOSYS, nilPathErr), true,
"-> Error: %!q(PANIC=Error method: runtime error: invalid memory address or nil pointer dereference)"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var debugged bytes.Buffer
c := newReplyTestConnection(t, nil)
c.debugLogger = log.New(&debugged, "", 0)
if tt.errorLogger {
c.errorLogger = log.New(io.Discard, "", 0)
}
ctx, _, err := c.ReadOp()
if err != nil {
t.Fatalf("ReadOp: %v", err)
}

if err := c.Reply(ctx, tt.opErr); err != nil {
t.Errorf("Reply: %v", err)
}
if !strings.HasSuffix(debugged.String(), "] "+tt.want+"\n") {
t.Errorf("debug log = %q, want it to end with %q", debugged.String(), tt.want)
}
})
}
}

func TestSanitizeMaxPagesAndWrite(t *testing.T) {
pageSize := uint32(buffer.GetPageSize())
defaultMaxWrite := uint32(buffer.MaxWriteSize)
Expand Down
3 changes: 2 additions & 1 deletion conversions.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/fuse/internal/buffer"
"github.com/jacobsa/fuse/internal/fusekernel"
"github.com/jacobsa/fuse/internal/operror"
)

////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -805,7 +806,7 @@ func (c *Connection) kernelResponse(

if !handled {
m.OutHeader().Error = -int32(syscall.EIO)
if errno, ok := opErr.(syscall.Errno); ok {
if errno, ok := operror.Errno(opErr); ok {
m.OutHeader().Error = -int32(errno)
}

Expand Down
36 changes: 22 additions & 14 deletions fuseutil/file_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/jacobsa/fuse"
"github.com/jacobsa/fuse/fuseops"
"github.com/jacobsa/fuse/internal/operror"
)

// An interface with a method for each op type in the fuseops package. This can
Expand Down Expand Up @@ -155,20 +156,7 @@ func (s *fileSystemServer) handleOp(
err = s.fs.ForgetInode(ctx, typed)

case *fuseops.BatchForgetOp:
err = s.fs.BatchForget(ctx, typed)
if err == fuse.ENOSYS {
// Handle as a series of single-inode forget operations
for _, entry := range typed.Entries {
err = s.fs.ForgetInode(ctx, &fuseops.ForgetInodeOp{
Inode: entry.Inode,
N: entry.N,
OpContext: typed.OpContext,
})
if err != nil {
break
}
}
}
err = s.batchForget(ctx, typed)

case *fuseops.MkDirOp:
err = s.fs.MkDir(ctx, typed)
Expand Down Expand Up @@ -248,3 +236,23 @@ func (s *fileSystemServer) handleOp(

c.Reply(ctx, err)
}

func (s *fileSystemServer) batchForget(
ctx context.Context,
op *fuseops.BatchForgetOp) error {
err := s.fs.BatchForget(ctx, op)
if errno, _ := operror.Errno(err); errno == fuse.ENOSYS {
// Handle as a series of single-inode forget operations
for _, entry := range op.Entries {
err = s.fs.ForgetInode(ctx, &fuseops.ForgetInodeOp{
Inode: entry.Inode,
N: entry.N,
OpContext: op.OpContext,
})
if err != nil {
break
}
}
}
return err
}
Loading