Skip to content
Merged
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
30 changes: 21 additions & 9 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -453,15 +455,25 @@ 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.
//
// 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) {
Expand Down
26 changes: 26 additions & 0 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}
22 changes: 19 additions & 3 deletions fuseutil/file_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion internal/buffer/in_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package buffer
import (
"fmt"
"io"
"os"
"sync"
"syscall"
"unsafe"
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions mount_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
Loading