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
import (
"fmt"
"io"
"os"
"github.com/hanwen/go-fuse/splice"
)
......@@ -11,58 +11,83 @@ func (s *Server) setSplice() {
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 {
pair, err := splice.Get()
var err error
// Get a pair of connected pipes
pair1, err := splice.Get()
if err != nil {
return err
}
defer splice.Done(pair)
defer splice.Done(pair1)
total := len(header) + fdData.Size()
if err := pair.Grow(total); err != nil {
// Grow buffer pipe to requested size + one extra page
// 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
}
_, err = pair.Write(header)
// Read data from file
payloadLen, err := pair1.LoadFromAt(fdData.Fd, fdData.Size(), fdData.Off)
if err != nil {
// TODO - extract the data from splice.
return err
}
var n int
if fdData.Off < 0 {
n, err = pair.LoadFrom(fdData.Fd, fdData.Size())
} else {
n, err = pair.LoadFromAt(fdData.Fd, fdData.Size(), fdData.Off)
// Get another pair of connected pipes
pair2, err := splice.Get()
if err != nil {
return err
}
if err == io.EOF || (err == nil && n < fdData.Size()) {
discard := make([]byte, len(header))
_, err = pair.Read(discard)
if err != nil {
return err
}
header = req.serializeHeader(n)
defer splice.Done(pair2)
newFd := readResultFd{
Fd: pair.ReadFd(),
Off: -1,
Sz: n,
}
return ms.trySplice(header, req, &newFd)
// Grow pipe to header + actually read size + one extra page
// Without the extra page the kernel will block once the pipe is almost full
header = req.serializeHeader(payloadLen)
total := len(header) + payloadLen
pair2Sz := total + os.Getpagesize()
if err := pair2.Grow(pair2Sz); err != nil {
return err
}
// Write header into pair2
n, err := pair2.Write(header)
if err != nil {
// TODO - extract the data from splice.
return err
}
if n != len(header) {
return fmt.Errorf("Short write into splice: wrote %d, want %d", n, len(header))
}
if n != fdData.Size() {
return fmt.Errorf("wrote %d, want %d", n, fdData.Size())
// Write data into pair2
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 {
return err
}
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