2015-07-27 07:09:51 +03:00
|
|
|
// Copyright 2015 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-07-27 07:09:56 +03:00
|
|
|
package fuse
|
2015-07-27 07:09:51 +03:00
|
|
|
|
|
|
|
import (
|
2015-07-27 08:12:43 +03:00
|
|
|
"fmt"
|
2015-07-27 07:45:59 +03:00
|
|
|
"syscall"
|
2015-07-27 07:09:51 +03:00
|
|
|
"unsafe"
|
|
|
|
|
2015-07-27 07:13:08 +03:00
|
|
|
"github.com/jacobsa/fuse/fuseops"
|
2015-07-27 07:09:51 +03:00
|
|
|
"github.com/jacobsa/fuse/internal/buffer"
|
|
|
|
"github.com/jacobsa/fuse/internal/fusekernel"
|
|
|
|
)
|
|
|
|
|
2015-07-27 08:32:17 +03:00
|
|
|
// Return the response that should be sent to the kernel. If the op requires no
|
|
|
|
// response, return a nil response.
|
2015-07-27 07:45:59 +03:00
|
|
|
func kernelResponse(
|
2015-07-27 08:32:17 +03:00
|
|
|
fuseID uint64,
|
2015-07-27 08:43:41 +03:00
|
|
|
op interface{},
|
2015-07-27 08:32:17 +03:00
|
|
|
opErr error,
|
|
|
|
protocol fusekernel.Protocol) (msg []byte) {
|
|
|
|
// If the user replied with an error, create room enough just for the result
|
|
|
|
// header and fill it in with an error. Otherwise create an appropriate
|
|
|
|
// response.
|
2015-07-27 08:34:14 +03:00
|
|
|
var b buffer.OutMessage
|
2015-07-27 08:32:17 +03:00
|
|
|
if opErr != nil {
|
|
|
|
b = buffer.NewOutMessage(0)
|
|
|
|
if errno, ok := opErr.(syscall.Errno); ok {
|
|
|
|
b.OutHeader().Error = -int32(errno)
|
|
|
|
} else {
|
|
|
|
b.OutHeader().Error = -int32(syscall.EIO)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
b = kernelResponseForOp(op, protocol)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = b.Bytes()
|
|
|
|
|
|
|
|
// Fill in the rest of the header.
|
|
|
|
h := b.OutHeader()
|
|
|
|
h.Unique = fuseID
|
|
|
|
h.Len = uint32(len(msg))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Like kernelResponse, but assumes the user replied with a nil error to the
|
|
|
|
// op.
|
|
|
|
func kernelResponseForOp(
|
2015-07-27 08:43:41 +03:00
|
|
|
op interface{},
|
2015-07-27 08:34:14 +03:00
|
|
|
protocol fusekernel.Protocol) (b buffer.OutMessage) {
|
2015-07-27 08:32:17 +03:00
|
|
|
// Create the appropriate output message
|
|
|
|
switch o := op.(type) {
|
2015-07-27 07:45:59 +03:00
|
|
|
case *fuseops.LookUpInodeOp:
|
|
|
|
size := fusekernel.EntryOutSize(protocol)
|
|
|
|
b = buffer.NewOutMessage(size)
|
|
|
|
out := (*fusekernel.EntryOut)(b.Grow(size))
|
2015-07-27 07:46:41 +03:00
|
|
|
convertChildInodeEntry(&o.Entry, out)
|
2015-07-27 07:45:59 +03:00
|
|
|
|
|
|
|
case *fuseops.GetInodeAttributesOp:
|
2015-07-27 07:46:41 +03:00
|
|
|
size := fusekernel.AttrOutSize(protocol)
|
2015-07-27 07:45:59 +03:00
|
|
|
b = buffer.NewOutMessage(size)
|
|
|
|
out := (*fusekernel.AttrOut)(b.Grow(size))
|
|
|
|
out.AttrValid, out.AttrValidNsec = convertExpirationTime(o.AttributesExpiration)
|
|
|
|
convertAttributes(o.Inode, &o.Attributes, &out.Attr)
|
|
|
|
|
|
|
|
case *fuseops.SetInodeAttributesOp:
|
2015-07-27 07:46:41 +03:00
|
|
|
size := fusekernel.AttrOutSize(protocol)
|
2015-07-27 07:45:59 +03:00
|
|
|
b = buffer.NewOutMessage(size)
|
|
|
|
out := (*fusekernel.AttrOut)(b.Grow(size))
|
|
|
|
out.AttrValid, out.AttrValidNsec = convertExpirationTime(o.AttributesExpiration)
|
|
|
|
convertAttributes(o.Inode, &o.Attributes, &out.Attr)
|
|
|
|
|
|
|
|
case *fuseops.ForgetInodeOp:
|
|
|
|
// No response.
|
|
|
|
|
|
|
|
case *fuseops.MkDirOp:
|
2015-07-27 07:46:41 +03:00
|
|
|
size := fusekernel.EntryOutSize(protocol)
|
2015-07-27 07:45:59 +03:00
|
|
|
b = buffer.NewOutMessage(size)
|
|
|
|
out := (*fusekernel.EntryOut)(b.Grow(size))
|
|
|
|
convertChildInodeEntry(&o.Entry, out)
|
|
|
|
|
|
|
|
case *fuseops.CreateFileOp:
|
2015-07-27 07:46:41 +03:00
|
|
|
eSize := fusekernel.EntryOutSize(protocol)
|
2015-07-27 07:45:59 +03:00
|
|
|
b = buffer.NewOutMessage(eSize + unsafe.Sizeof(fusekernel.OpenOut{}))
|
|
|
|
|
|
|
|
e := (*fusekernel.EntryOut)(b.Grow(eSize))
|
|
|
|
convertChildInodeEntry(&o.Entry, e)
|
|
|
|
|
|
|
|
oo := (*fusekernel.OpenOut)(b.Grow(unsafe.Sizeof(fusekernel.OpenOut{})))
|
|
|
|
oo.Fh = uint64(o.Handle)
|
|
|
|
|
|
|
|
case *fuseops.CreateSymlinkOp:
|
2015-07-27 07:46:41 +03:00
|
|
|
size := fusekernel.EntryOutSize(protocol)
|
2015-07-27 07:45:59 +03:00
|
|
|
b = buffer.NewOutMessage(size)
|
|
|
|
out := (*fusekernel.EntryOut)(b.Grow(size))
|
|
|
|
convertChildInodeEntry(&o.Entry, out)
|
|
|
|
|
|
|
|
case *fuseops.RenameOp:
|
|
|
|
b = buffer.NewOutMessage(0)
|
|
|
|
|
|
|
|
case *fuseops.RmDirOp:
|
|
|
|
b = buffer.NewOutMessage(0)
|
|
|
|
|
|
|
|
case *fuseops.UnlinkOp:
|
|
|
|
b = buffer.NewOutMessage(0)
|
|
|
|
|
|
|
|
case *fuseops.OpenDirOp:
|
|
|
|
b = buffer.NewOutMessage(unsafe.Sizeof(fusekernel.OpenOut{}))
|
|
|
|
out := (*fusekernel.OpenOut)(b.Grow(unsafe.Sizeof(fusekernel.OpenOut{})))
|
|
|
|
out.Fh = uint64(o.Handle)
|
|
|
|
|
|
|
|
case *fuseops.ReadDirOp:
|
|
|
|
b = buffer.NewOutMessage(uintptr(len(o.Data)))
|
|
|
|
b.Append(o.Data)
|
|
|
|
|
|
|
|
case *fuseops.ReleaseDirHandleOp:
|
|
|
|
b = buffer.NewOutMessage(0)
|
|
|
|
|
|
|
|
case *fuseops.OpenFileOp:
|
|
|
|
b = buffer.NewOutMessage(unsafe.Sizeof(fusekernel.OpenOut{}))
|
|
|
|
out := (*fusekernel.OpenOut)(b.Grow(unsafe.Sizeof(fusekernel.OpenOut{})))
|
|
|
|
out.Fh = uint64(o.Handle)
|
|
|
|
|
|
|
|
case *fuseops.ReadFileOp:
|
|
|
|
b = buffer.NewOutMessage(uintptr(len(o.Data)))
|
|
|
|
b.Append(o.Data)
|
|
|
|
|
|
|
|
case *fuseops.WriteFileOp:
|
|
|
|
b = buffer.NewOutMessage(unsafe.Sizeof(fusekernel.WriteOut{}))
|
|
|
|
out := (*fusekernel.WriteOut)(b.Grow(unsafe.Sizeof(fusekernel.WriteOut{})))
|
|
|
|
out.Size = uint32(len(o.Data))
|
|
|
|
|
|
|
|
case *fuseops.SyncFileOp:
|
|
|
|
b = buffer.NewOutMessage(0)
|
|
|
|
|
|
|
|
case *fuseops.FlushFileOp:
|
|
|
|
b = buffer.NewOutMessage(0)
|
|
|
|
|
|
|
|
case *fuseops.ReleaseFileHandleOp:
|
|
|
|
b = buffer.NewOutMessage(0)
|
|
|
|
|
|
|
|
case *fuseops.ReadSymlinkOp:
|
|
|
|
b = buffer.NewOutMessage(uintptr(len(o.Target)))
|
|
|
|
b.AppendString(o.Target)
|
|
|
|
|
|
|
|
case *internalStatFSOp:
|
|
|
|
b = buffer.NewOutMessage(unsafe.Sizeof(fusekernel.StatfsOut{}))
|
|
|
|
b.Grow(unsafe.Sizeof(fusekernel.StatfsOut{}))
|
|
|
|
|
|
|
|
case *internalInterruptOp:
|
|
|
|
// No response.
|
|
|
|
|
|
|
|
case *internalInitOp:
|
|
|
|
b = buffer.NewOutMessage(unsafe.Sizeof(fusekernel.InitOut{}))
|
|
|
|
out := (*fusekernel.InitOut)(b.Grow(unsafe.Sizeof(fusekernel.InitOut{})))
|
|
|
|
|
|
|
|
out.Major = o.Library.Major
|
|
|
|
out.Minor = o.Library.Minor
|
|
|
|
out.MaxReadahead = o.MaxReadahead
|
|
|
|
out.Flags = uint32(o.Flags)
|
|
|
|
out.MaxWrite = o.MaxWrite
|
|
|
|
|
|
|
|
default:
|
2015-07-27 08:32:17 +03:00
|
|
|
panic(fmt.Sprintf("Unknown op: %#v", op))
|
2015-07-27 07:45:59 +03:00
|
|
|
}
|
2015-07-27 07:09:51 +03:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2015-07-27 07:47:29 +03:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// Internal
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-07-27 08:12:43 +03:00
|
|
|
// A sentinel used for unknown ops. The user is expected to respond with a
|
|
|
|
// non-nil error.
|
|
|
|
type unknownOp struct {
|
|
|
|
opCode uint32
|
|
|
|
inode fuseops.InodeID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *unknownOp) ShortDesc() (desc string) {
|
|
|
|
desc = fmt.Sprintf("<opcode %d>(inode=%v)", o.opCode, o.inode)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *unknownOp) DebugString() string {
|
|
|
|
return o.ShortDesc()
|
|
|
|
}
|
|
|
|
|
2015-07-27 07:47:29 +03:00
|
|
|
// Common implementation for our "internal" ops that don't need to be pretty.
|
|
|
|
type internalOp struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *internalOp) ShortDesc() string { return "<internalOp>" }
|
|
|
|
func (o *internalOp) DebugString() string { return "<internalOp>" }
|
|
|
|
|
|
|
|
type internalStatFSOp struct {
|
|
|
|
internalOp
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *internalStatFSOp) kernelResponse() (b buffer.OutMessage) {
|
|
|
|
b = buffer.NewOutMessage(unsafe.Sizeof(fusekernel.StatfsOut{}))
|
|
|
|
b.Grow(unsafe.Sizeof(fusekernel.StatfsOut{}))
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
type internalInterruptOp struct {
|
|
|
|
internalOp
|
|
|
|
FuseID uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *internalInterruptOp) kernelResponse() (b buffer.OutMessage) {
|
|
|
|
panic("Shouldn't get here.")
|
|
|
|
}
|
|
|
|
|
|
|
|
type internalInitOp struct {
|
|
|
|
internalOp
|
|
|
|
|
|
|
|
// In
|
|
|
|
Kernel fusekernel.Protocol
|
|
|
|
|
|
|
|
// Out
|
|
|
|
Library fusekernel.Protocol
|
|
|
|
MaxReadahead uint32
|
|
|
|
Flags fusekernel.InitFlags
|
|
|
|
MaxWrite uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *internalInitOp) kernelResponse() (b buffer.OutMessage) {
|
|
|
|
b = buffer.NewOutMessage(unsafe.Sizeof(fusekernel.InitOut{}))
|
|
|
|
out := (*fusekernel.InitOut)(b.Grow(unsafe.Sizeof(fusekernel.InitOut{})))
|
|
|
|
|
|
|
|
out.Major = o.Library.Major
|
|
|
|
out.Minor = o.Library.Minor
|
|
|
|
out.MaxReadahead = o.MaxReadahead
|
|
|
|
out.Flags = uint32(o.Flags)
|
|
|
|
out.MaxWrite = o.MaxWrite
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|