diff --git a/connection.go b/connection.go index 429a3b7c..a0ef1008 100644 --- a/connection.go +++ b/connection.go @@ -401,15 +401,17 @@ func (c *Connection) readMessage() (*buffer.InMessage, error) { // * EINTR means we should try again. (This seems to happen often on // OS X, cf. http://golang.org/issue/11180) // + var errno error = err if pe, ok := err.(*os.PathError); ok { - switch pe.Err { - case syscall.ENODEV: - err = io.EOF + errno = pe.Err + } + switch errno { + case syscall.ENODEV, syscall.EBADF: + err = io.EOF - case syscall.EINTR: - err = nil - continue - } + case syscall.EINTR: + err = nil + continue } if err != nil { @@ -453,6 +455,15 @@ func (c *Connection) writeMessage(msg []byte) error { return nil } +// NumReaders returns the configured number of concurrent goroutines that should +// read ops from /dev/fuse, defaulting to 1 if unset or non-positive. +func (c *Connection) NumReaders() int { + if c.cfg.NumReaders <= 0 { + return 1 + } + return c.cfg.NumReaders +} + // ReadOp consumes the next op from the kernel process, returning the op and a // context that should be used for work related to the op. It returns io.EOF if // the kernel has closed the connection. @@ -460,8 +471,9 @@ func (c *Connection) writeMessage(msg []byte) error { // If err != nil, the user is responsible for later calling c.Reply with the // returned context. // -// This function delivers ops in exactly the order they are received from -// /dev/fuse. It must not be called multiple times concurrently. +// ReadOp is safe to be called concurrently by multiple goroutines. Note that +// concurrent callers may receive ops out of the order in which they were +// delivered by /dev/fuse. // // LOCKS_EXCLUDED(c.mu) func (c *Connection) ReadOp() (_ context.Context, op interface{}, _ error) { diff --git a/connection_test.go b/connection_test.go index c1a4f013..fa8a323d 100644 --- a/connection_test.go +++ b/connection_test.go @@ -142,3 +142,29 @@ func TestSanitizeMaxPagesAndWrite(t *testing.T) { }) } } + +func TestConnectionNumReaders(t *testing.T) { + tests := []struct { + name string + input int + want int + }{ + {name: "unset defaults to 1", input: 0, want: 1}, + {name: "negative defaults to 1", input: -4, want: 1}, + {name: "explicit 1", input: 1, want: 1}, + {name: "explicit 8", input: 8, want: 8}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + c := &Connection{ + cfg: MountConfig{ + NumReaders: tc.input, + }, + } + if got := c.NumReaders(); got != tc.want { + t.Errorf("NumReaders() = %d, want %d", got, tc.want) + } + }) + } +} diff --git a/fuseutil/file_system.go b/fuseutil/file_system.go index b1c27f56..532d50ef 100644 --- a/fuseutil/file_system.go +++ b/fuseutil/file_system.go @@ -104,16 +104,32 @@ func (s *fileSystemServer) ServeOps(c *fuse.Connection) { s.fs.Destroy() }() + numReaders := c.NumReaders() + if numReaders == 1 { + s.readOps(c) + return + } + + var readers sync.WaitGroup + for range numReaders { + readers.Add(1) + go func() { + defer readers.Done() + s.readOps(c) + }() + } + readers.Wait() +} + +func (s *fileSystemServer) readOps(c *fuse.Connection) { for { ctx, op, err := c.ReadOp() if err == io.EOF { - break + return } - if err != nil { panic(err) } - s.opsInFlight.Add(1) if _, ok := op.(*fuseops.ForgetInodeOp); ok { // Special case: call in this goroutine for diff --git a/internal/buffer/in_message.go b/internal/buffer/in_message.go index 60eb7633..486e0ac5 100644 --- a/internal/buffer/in_message.go +++ b/internal/buffer/in_message.go @@ -17,6 +17,7 @@ package buffer import ( "fmt" "io" + "os" "sync" "syscall" "unsafe" @@ -88,7 +89,11 @@ func (m *InMessage) Init(r io.Reader) error { if fusekernel.IsPlatformFuseT { n, err = m.ReadSingle(r) } else { - n, err = r.Read(m.storage[:]) + if f, ok := r.(*os.File); ok { + n, err = syscall.Read(int(f.Fd()), m.storage[:]) + } else { + n, err = r.Read(m.storage[:]) + } } if err != nil { diff --git a/mount_config.go b/mount_config.go index d98058ad..bcc938bb 100644 --- a/mount_config.go +++ b/mount_config.go @@ -276,6 +276,15 @@ type MountConfig struct { // If unset, a default value of 1 MiB is used (or less if MaxPages is set // to a value smaller than 256 pages). MaxWrite uint32 + + // Number of concurrent goroutines reading ops from /dev/fuse in + // fuseutil.NewFileSystemServer (and available to custom Server + // implementations via Connection.NumReaders). + // + // If unset or <= 0, defaults to 1. Values > 1 can improve multi-core + // throughput on Linux, but allow concurrent ForgetInode calls, pin one + // InMessage buffer per reader, and may dispatch ops out of arrival order. + NumReaders int } type FUSEImpl uint8 diff --git a/mount_test.go b/mount_test.go index 87cc36a2..8197595a 100644 --- a/mount_test.go +++ b/mount_test.go @@ -64,6 +64,39 @@ func TestSuccessfulMount(t *testing.T) { defer fuse.Unmount(mfs.Dir()) } +func TestSuccessfulMountWithMultipleReaders(t *testing.T) { + ctx := context.Background() + + // Set up a temporary directory. + dir, err := ioutil.TempDir("", "mount_test") + if err != nil { + t.Fatalf("ioutil.TempDir: %v", err) + } + + defer os.RemoveAll(dir) + + // Mount with multiple readers. + fs := &minimalFS{} + mfs, err := fuse.Mount( + dir, + fuseutil.NewFileSystemServer(fs), + &fuse.MountConfig{ + NumReaders: 8, + }) + + if err != nil { + t.Fatalf("fuse.Mount: %v", err) + } + + defer func() { + if err := mfs.Join(ctx); err != nil { + t.Errorf("Joining: %v", err) + } + }() + + defer fuse.Unmount(mfs.Dir()) +} + func TestNonexistentMountPoint(t *testing.T) { ctx := context.Background()