fusego/fuseops/common_op.go

175 lines
4.6 KiB
Go
Raw Normal View History

2015-05-01 03:49:14 +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.
package fuseops
import (
2015-05-01 04:43:46 +03:00
"fmt"
2015-05-25 07:15:49 +03:00
"log"
2015-05-01 03:57:49 +03:00
"reflect"
2015-05-01 04:24:05 +03:00
"strings"
2015-07-24 07:02:55 +03:00
"syscall"
2015-05-01 03:49:14 +03:00
2015-07-24 06:41:05 +03:00
"github.com/jacobsa/fuse/internal/buffer"
2015-05-01 04:04:57 +03:00
"github.com/jacobsa/reqtrace"
2015-05-01 03:49:14 +03:00
"golang.org/x/net/context"
)
2015-05-05 03:20:03 +03:00
// An interface that all ops inside which commonOp is embedded must
// implement.
type internalOp interface {
Op
2015-07-24 06:41:05 +03:00
// Create a response message for the kernel, leaving the leading
// fusekernel.OutHeader untouched.
2015-07-24 04:54:56 +03:00
//
2015-07-24 06:41:05 +03:00
// Special case: a zero return value means that the kernel is not expecting a
// response.
2015-07-24 07:12:12 +03:00
kernelResponse() (b buffer.OutMessage)
2015-05-05 03:20:03 +03:00
}
2015-07-24 01:37:20 +03:00
// A function that sends a reply message back to the kernel for the request
2015-07-24 02:43:02 +03:00
// with the given fuse unique ID. The error argument is for informational
// purposes only; the error to hand to the kernel is encoded in the message.
2015-07-24 04:38:23 +03:00
type replyFunc func(Op, uint64, []byte, error) error
2015-07-24 01:37:20 +03:00
2015-05-01 03:49:14 +03:00
// A helper for embedding common behavior.
type commonOp struct {
2015-05-05 02:56:49 +03:00
// The context exposed to the user.
ctx context.Context
2015-05-05 02:48:16 +03:00
// The op in which this struct is embedded.
2015-05-05 03:21:21 +03:00
op internalOp
2015-05-01 05:21:06 +03:00
2015-07-24 01:33:40 +03:00
// The fuse unique ID of this request, as assigned by the kernel.
fuseID uint64
// A function that can be used to send a reply to the kernel.
2015-07-24 01:37:20 +03:00
sendReply replyFunc
2015-05-01 04:57:37 +03:00
// A function that can be used to log debug information about the op. The
// first argument is a call depth.
//
// May be nil.
debugLog func(int, string, ...interface{})
2015-05-25 07:15:49 +03:00
// A logger to be used for logging exceptional errors.
//
// May be nil.
2015-05-25 07:15:49 +03:00
errorLogger *log.Logger
2015-05-01 03:49:14 +03:00
}
2015-05-01 05:24:36 +03:00
func (o *commonOp) ShortDesc() (desc string) {
2015-07-24 01:41:16 +03:00
v := reflect.ValueOf(o.op)
opName := v.Type().String()
2015-05-01 05:24:36 +03:00
2015-05-01 05:28:25 +03:00
// Attempt to better handle the usual case: a string that looks like
// "*fuseops.GetInodeAttributesOp".
2015-05-01 05:24:36 +03:00
const prefix = "*fuseops."
const suffix = "Op"
2015-05-01 05:28:25 +03:00
if strings.HasPrefix(opName, prefix) && strings.HasSuffix(opName, suffix) {
opName = opName[len(prefix) : len(opName)-len(suffix)]
2015-05-01 05:24:36 +03:00
}
2015-07-24 04:13:17 +03:00
desc = opName
2015-07-24 01:41:16 +03:00
// Include the inode number to which the op applies, if possible.
2015-07-24 03:33:39 +03:00
if f := v.Elem().FieldByName("Inode"); f.IsValid() {
2015-07-24 04:13:17 +03:00
desc = fmt.Sprintf("%s(inode=%v)", desc, f.Interface())
2015-07-24 01:41:16 +03:00
}
2015-05-01 05:24:36 +03:00
return
}
2015-07-24 04:47:39 +03:00
func (o *commonOp) DebugString() string {
// By default, defer to ShortDesc.
return o.op.ShortDesc()
}
2015-05-01 03:49:14 +03:00
func (o *commonOp) init(
ctx context.Context,
2015-05-05 03:21:21 +03:00
op internalOp,
2015-07-24 02:43:02 +03:00
fuseID uint64,
sendReply replyFunc,
debugLog func(int, string, ...interface{}),
2015-07-24 02:43:02 +03:00
errorLogger *log.Logger) {
2015-05-01 04:04:57 +03:00
// Initialize basic fields.
2015-05-05 02:56:49 +03:00
o.ctx = ctx
2015-05-01 05:21:06 +03:00
o.op = op
2015-07-24 02:43:02 +03:00
o.fuseID = fuseID
o.sendReply = sendReply
o.debugLog = debugLog
2015-05-25 07:15:49 +03:00
o.errorLogger = errorLogger
2015-05-05 02:56:49 +03:00
2015-05-01 04:04:57 +03:00
// Set up a trace span for this op.
2015-05-05 02:56:49 +03:00
var reportForTrace reqtrace.ReportFunc
2015-05-05 07:49:59 +03:00
o.ctx, reportForTrace = reqtrace.StartSpan(o.ctx, o.op.ShortDesc())
2015-05-05 02:56:49 +03:00
// When the op is finished, report to both reqtrace and the connection.
2015-07-24 02:43:02 +03:00
prevSendReply := o.sendReply
2015-07-24 04:38:23 +03:00
o.sendReply = func(op Op, fuseID uint64, msg []byte, opErr error) (err error) {
2015-07-24 02:43:02 +03:00
reportForTrace(opErr)
2015-07-24 04:38:23 +03:00
err = prevSendReply(op, fuseID, msg, opErr)
2015-07-24 02:43:02 +03:00
return
2015-05-05 02:56:49 +03:00
}
2015-05-01 03:49:14 +03:00
}
func (o *commonOp) Context() context.Context {
return o.ctx
}
func (o *commonOp) Logf(format string, v ...interface{}) {
if o.debugLog == nil {
return
}
2015-05-01 03:49:14 +03:00
const calldepth = 2
o.debugLog(calldepth, format, v...)
2015-05-01 03:49:14 +03:00
}
2015-05-05 03:25:25 +03:00
func (o *commonOp) Respond(err error) {
2015-07-24 02:52:37 +03:00
// If successful, we ask the op for an appopriate response to the kernel, and
// it is responsible for leaving room for the fusekernel.OutHeader struct.
// Otherwise, create our own.
2015-07-24 07:12:12 +03:00
var b buffer.OutMessage
2015-05-05 03:25:25 +03:00
if err == nil {
2015-07-24 06:41:05 +03:00
b = o.op.kernelResponse()
2015-07-24 02:52:37 +03:00
} else {
2015-07-24 07:12:12 +03:00
b = buffer.NewOutMessage(0)
2015-05-05 03:25:25 +03:00
}
2015-07-24 04:54:56 +03:00
// Fill in the header if a reply is needed.
2015-07-24 06:41:05 +03:00
msg := b.Bytes()
2015-07-24 04:54:56 +03:00
if msg != nil {
2015-07-24 06:41:05 +03:00
h := b.OutHeader()
2015-07-24 04:54:56 +03:00
h.Unique = o.fuseID
h.Len = uint32(len(msg))
if err != nil {
2015-07-24 07:02:55 +03:00
// If the user gave us a syscall.Errno, use that value in the reply.
// Otherwise use the generic EIO.
if errno, ok := err.(syscall.Errno); ok {
h.Error = -int32(errno)
} else {
h.Error = -int32(syscall.EIO)
2015-07-24 04:54:56 +03:00
}
2015-07-24 02:52:37 +03:00
}
}
2015-05-01 03:49:14 +03:00
2015-07-24 02:52:37 +03:00
// Reply.
2015-07-24 04:38:23 +03:00
replyErr := o.sendReply(o.op, o.fuseID, msg, err)
2015-07-24 02:52:37 +03:00
if replyErr != nil && o.errorLogger != nil {
o.errorLogger.Printf("Error from sendReply: %v", replyErr)
}
2015-05-01 03:49:14 +03:00
}