Commit c4179aac authored by Jakob Unterwurzacher's avatar Jakob Unterwurzacher Committed by Han-Wen Nienhuys

fuse: rewrite trySplice() without recursion

The old implementation was buggy and failed xfstests generic/001
(last 1kB of some files were filled with zeros).

The new one introduces a second splice layer to get rid of recursion
and passes generic/001.

Fixes issue #54.
parent 41b8187f
...@@ -2,7 +2,7 @@ package fuse ...@@ -2,7 +2,7 @@ package fuse
import ( import (
"fmt" "fmt"
"io" "os"
"github.com/hanwen/go-fuse/splice" "github.com/hanwen/go-fuse/splice"
) )
...@@ -11,58 +11,83 @@ func (s *Server) setSplice() { ...@@ -11,58 +11,83 @@ func (s *Server) setSplice() {
s.canSplice = splice.Resizable() s.canSplice = splice.Resizable()
} }
// trySplice: Zero-copy read from fdData.Fd into /dev/fuse
//
// This is a four-step process:
//
// 1) Splice data form fdData.Fd into the "pair1" pipe buffer --> pair1: [payload]
// Now we know the actual payload length and can
// construct the reply header
// 2) Write header into the "pair2" pipe buffer --> pair2: [header]
// 4) Splice data from "pair1" into "pair2" --> pair2: [header][payload]
// 3) Splice the data from "pair2" into /dev/fuse
//
// This dance is neccessary because header and payload cannot be split across
// two splices and we cannot seek in a pipe buffer.
func (ms *Server) trySplice(header []byte, req *request, fdData *readResultFd) error { func (ms *Server) trySplice(header []byte, req *request, fdData *readResultFd) error {
pair, err := splice.Get() var err error
// Get a pair of connected pipes
pair1, err := splice.Get()
if err != nil { if err != nil {
return err return err
} }
defer splice.Done(pair) defer splice.Done(pair1)
total := len(header) + fdData.Size() // Grow buffer pipe to requested size + one extra page
if err := pair.Grow(total); err != nil { // Without the extra page the kernel will block once the pipe is almost full
pair1Sz := fdData.Size() + os.Getpagesize()
if err := pair1.Grow(pair1Sz); err != nil {
return err return err
} }
_, err = pair.Write(header) // Read data from file
payloadLen, err := pair1.LoadFromAt(fdData.Fd, fdData.Size(), fdData.Off)
if err != nil { if err != nil {
// TODO - extract the data from splice.
return err return err
} }
var n int // Get another pair of connected pipes
if fdData.Off < 0 { pair2, err := splice.Get()
n, err = pair.LoadFrom(fdData.Fd, fdData.Size()) if err != nil {
} else { return err
n, err = pair.LoadFromAt(fdData.Fd, fdData.Size(), fdData.Off)
} }
if err == io.EOF || (err == nil && n < fdData.Size()) { defer splice.Done(pair2)
discard := make([]byte, len(header))
_, err = pair.Read(discard)
if err != nil {
return err
}
header = req.serializeHeader(n)
newFd := readResultFd{ // Grow pipe to header + actually read size + one extra page
Fd: pair.ReadFd(), // Without the extra page the kernel will block once the pipe is almost full
Off: -1, header = req.serializeHeader(payloadLen)
Sz: n, total := len(header) + payloadLen
} pair2Sz := total + os.Getpagesize()
return ms.trySplice(header, req, &newFd) if err := pair2.Grow(pair2Sz); err != nil {
return err
} }
// Write header into pair2
n, err := pair2.Write(header)
if err != nil { if err != nil {
// TODO - extract the data from splice.
return err return err
} }
if n != len(header) {
return fmt.Errorf("Short write into splice: wrote %d, want %d", n, len(header))
}
if n != fdData.Size() { // Write data into pair2
return fmt.Errorf("wrote %d, want %d", n, fdData.Size()) n, err = pair2.LoadFrom(pair1.ReadFd(), payloadLen)
if err != nil {
return err
}
if n != payloadLen {
return fmt.Errorf("Short splice: wrote %d, want %d", n, payloadLen)
} }
_, err = pair.WriteTo(uintptr(ms.mountFd), total) // Write header + data to /dev/fuse
_, err = pair2.WriteTo(uintptr(ms.mountFd), total)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment