Skip to content

Commit

Permalink
fuse: wire up passthrough support
Browse files Browse the repository at this point in the history
The passthrough feature lets a filesystem register an open file
descriptor with the kernel to avoid roundtrips for read/write
operations.

Change-Id: Ia8bde502a3450028f4d87ba61fa9c76ea3ea6c63
  • Loading branch information
hanwen committed Aug 25, 2024
1 parent 1224fbc commit e0641a4
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion fuse/opcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func doInit(server *Server, req *request) {
server.reqMu.Lock()
server.kernelSettings = *input
kernelFlags &= (CAP_ASYNC_READ | CAP_BIG_WRITES | CAP_FILE_OPS |
CAP_READDIRPLUS | CAP_NO_OPEN_SUPPORT | CAP_PARALLEL_DIROPS | CAP_MAX_PAGES | CAP_RENAME_SWAP)
CAP_READDIRPLUS | CAP_NO_OPEN_SUPPORT | CAP_PARALLEL_DIROPS | CAP_MAX_PAGES | CAP_RENAME_SWAP | CAP_PASSTHROUGH)

if server.opts.EnableLocks {
kernelFlags |= CAP_FLOCK_LOCKS | CAP_POSIX_LOCKS
Expand Down Expand Up @@ -154,6 +154,7 @@ func doInit(server *Server, req *request) {
CongestionThreshold: uint16(server.opts.MaxBackground * 3 / 4),
MaxBackground: uint16(server.opts.MaxBackground),
MaxPages: uint16(maxPages),
MaxStackDepth: 1,
}
out.setFlags(kernelFlags)
if server.opts.MaxReadAhead != 0 && uint32(server.opts.MaxReadAhead) < out.MaxReadAhead {
Expand Down
50 changes: 50 additions & 0 deletions fuse/passthrough_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2024 the Go-FUSE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package fuse

import (
"syscall"
"unsafe"
)

const (
_DEV_IOC_BACKING_OPEN = 0x4010e501
_DEV_IOC_BACKING_CLOSE = 0x4004e502
)

type backingMap struct {
Fd int32
Flags uint32
padding uint64
}

// RegisterBackingFd registers the given file descriptor in the
// kernel, so the kernel can bypass FUSE and access the backing file
// directly for read and write calls. On success a backing ID is
// returned. The backing ID should unregistered using
// UnregisterBackingFd() once the file is released. For now, the flags
// argument is unused, and should be 0.
func (ms *Server) RegisterBackingFd(fd int, flags uint32) (int32, syscall.Errno) {
m := backingMap{
Fd: int32(fd),
Flags: flags,
}

ms.writeMu.Lock()
id, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(ms.mountFd), uintptr(_DEV_IOC_BACKING_OPEN), uintptr(unsafe.Pointer(&m)))
ms.writeMu.Unlock()

return int32(id), errno
}

// UnregisterBackingFd unregisters the given ID in the kernel. The ID
// should have been acquired before using RegisterBackingFd.
func (ms *Server) UnregisterBackingFd(id int32) syscall.Errno {
ms.writeMu.Lock()
_, _, ep := syscall.Syscall(syscall.SYS_IOCTL, uintptr(ms.mountFd), uintptr(_DEV_IOC_BACKING_CLOSE), uintptr(unsafe.Pointer(&id)))
ms.writeMu.Unlock()

return ep
}

0 comments on commit e0641a4

Please sign in to comment.