fusego/fuseops/common_op.go

251 lines
6.3 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:34:52 +03:00
"flag"
2015-05-01 04:43:46 +03:00
"fmt"
2015-05-01 04:55:13 +03:00
"log"
2015-05-01 03:57:49 +03:00
"reflect"
2015-05-01 04:24:05 +03:00
"strings"
2015-05-01 03:49:14 +03:00
"sync"
2015-05-01 04:49:25 +03:00
"time"
2015-05-01 03:49:14 +03:00
"github.com/jacobsa/bazilfuse"
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-01 04:49:25 +03:00
"golang.org/x/sys/unix"
2015-05-01 03:49:14 +03:00
)
2015-05-01 04:35:46 +03:00
var fTraceByPID = flag.Bool(
"fuse.trace_by_pid",
2015-05-01 04:34:52 +03:00
false,
"Enable a hacky mode that uses reqtrace to group all ops from each "+
"individual PID. Not a good idea to use in production; races, bugs, and "+
"resource leaks likely lurk.")
2015-05-05 03:20:03 +03:00
// An interface that all ops inside which commonOp is embedded must
// implement.
type internalOp interface {
Op
// Convert to a bazilfuse response compatible with the Respond method on the
// wrapped bazilfuse request. If that Respond method takes no arguments,
// return nil.
toBazilfuseResponse() interface{}
}
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-01 05:24:36 +03:00
op Op
2015-05-01 05:21:06 +03:00
// The underlying bazilfuse request for this op.
2015-05-01 04:57:37 +03:00
bazilReq bazilfuse.Request
2015-05-05 02:52:25 +03:00
// A function that can be used to log information about the op. The first
// argument is a call depth.
log func(int, string, ...interface{})
2015-05-05 02:52:25 +03:00
// A function that is invoked with the error given to Respond, for use in
// closing off traces and reporting back to the connection.
2015-05-05 02:59:14 +03:00
finished func(error)
2015-05-01 03:49:14 +03:00
}
2015-05-01 04:43:46 +03:00
var gPIDMapMu sync.Mutex
// A map from PID to a traced context for that PID.
//
// GUARDED_BY(gPIDMapMu)
var gPIDMap = make(map[int]context.Context)
2015-05-01 04:49:25 +03:00
// Wait until the process completes, then close off the trace and remove the
// context from the map.
2015-05-01 04:43:46 +03:00
func reportWhenPIDGone(
pid int,
ctx context.Context,
2015-05-01 04:49:25 +03:00
report reqtrace.ReportFunc) {
2015-05-01 04:55:13 +03:00
// HACK(jacobsa): Poll until the process no longer exists.
2015-05-01 04:49:25 +03:00
const pollPeriod = 50 * time.Millisecond
for {
2015-05-01 04:55:13 +03:00
// The man page for kill(2) says that if the signal is zero, then "no
// signal is sent, but error checking is still performed; this can be used
// to check for the existence of a process ID".
2015-05-01 04:49:25 +03:00
err := unix.Kill(pid, 0)
2015-05-01 04:55:13 +03:00
// ESRCH means the process is gone.
2015-05-01 04:49:25 +03:00
if err == unix.ESRCH {
break
}
2015-05-01 04:55:13 +03:00
// If we receive EPERM, we're not going to be able to do what we want. We
// don't really have any choice but to print info and leak.
if err == unix.EPERM {
log.Printf("Failed to kill(2) PID %v; no permissions. Leaking trace.", pid)
return
}
// Otherwise, panic.
2015-05-01 04:49:25 +03:00
if err != nil {
panic(fmt.Errorf("Kill(%v): %v", pid, err))
}
time.Sleep(pollPeriod)
}
// Finish up.
report(nil)
gPIDMapMu.Lock()
delete(gPIDMap, pid)
gPIDMapMu.Unlock()
}
2015-05-01 04:43:46 +03:00
func (o *commonOp) maybeTraceByPID(
in context.Context,
pid int) (out context.Context) {
2015-05-01 04:34:52 +03:00
// Is there anything to do?
2015-05-01 04:43:46 +03:00
if !reqtrace.Enabled() || !*fTraceByPID {
2015-05-01 04:34:52 +03:00
out = in
return
}
2015-05-01 04:43:46 +03:00
gPIDMapMu.Lock()
defer gPIDMapMu.Unlock()
// Do we already have a traced context for this PID?
if existing, ok := gPIDMap[pid]; ok {
out = existing
return
}
// Set up a new one and stick it in the map.
var report reqtrace.ReportFunc
2015-05-01 05:21:22 +03:00
out, report = reqtrace.Trace(in, fmt.Sprintf("Requests from PID %v", pid))
2015-05-01 04:43:46 +03:00
gPIDMap[pid] = out
// Ensure we close the trace and remove it from the map eventually.
go reportWhenPIDGone(pid, out, report)
2015-05-01 04:34:52 +03:00
return
}
2015-05-01 05:24:36 +03:00
func (o *commonOp) ShortDesc() (desc string) {
2015-05-01 05:28:25 +03:00
opName := reflect.TypeOf(o.op).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-05-01 05:28:25 +03:00
// Include the inode number to which the op applies.
desc = fmt.Sprintf("%s(inode=%v)", opName, o.bazilReq.Hdr().Node)
2015-05-01 05:24:36 +03:00
return
}
2015-05-01 03:49:14 +03:00
func (o *commonOp) init(
ctx context.Context,
2015-05-01 05:21:06 +03:00
op Op,
2015-05-01 04:57:37 +03:00
bazilReq bazilfuse.Request,
2015-05-01 03:49:14 +03:00
log func(int, string, ...interface{}),
2015-05-05 02:59:14 +03:00
finished func(error)) {
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-05-01 04:57:37 +03:00
o.bazilReq = bazilReq
2015-05-01 03:49:14 +03:00
o.log = log
2015-05-05 02:59:14 +03:00
o.finished = finished
2015-05-05 02:56:49 +03:00
// Set up a context that reflects per-PID tracing if appropriate.
o.ctx = o.maybeTraceByPID(o.ctx, int(bazilReq.Hdr().Pid))
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
o.ctx, reportForTrace = reqtrace.StartSpan(ctx, o.op.ShortDesc())
// When the op is finished, report to both reqtrace and the connection.
2015-05-05 02:59:14 +03:00
prevFinish := o.finished
o.finished = func(err error) {
2015-05-05 02:56:49 +03:00
reportForTrace(err)
prevFinish(err)
}
2015-05-01 03:49:14 +03:00
}
func (o *commonOp) Header() OpHeader {
2015-05-01 04:57:37 +03:00
bh := o.bazilReq.Hdr()
2015-05-01 03:49:14 +03:00
return OpHeader{
Uid: bh.Uid,
Gid: bh.Gid,
}
}
func (o *commonOp) Context() context.Context {
return o.ctx
}
func (o *commonOp) Logf(format string, v ...interface{}) {
const calldepth = 2
o.log(calldepth, format, v...)
}
func (o *commonOp) respondErr(err error) {
if err == nil {
panic("Expect non-nil here.")
}
2015-05-05 03:07:34 +03:00
// Don't forget to report back to the connection that we are finished.
defer o.finished(err)
// Log that we are finished.
2015-05-01 03:49:14 +03:00
o.Logf(
"-> (%s) error: %v",
2015-05-01 05:38:03 +03:00
o.op.ShortDesc(),
2015-05-01 03:49:14 +03:00
err)
2015-05-05 02:57:41 +03:00
// Send a response to the kernel.
2015-05-01 04:57:37 +03:00
o.bazilReq.RespondError(err)
2015-05-01 03:49:14 +03:00
}
2015-05-01 03:57:49 +03:00
// Respond with the supplied response struct, which must be accepted by a
2015-05-01 04:57:37 +03:00
// method called Respond on o.bazilReq.
2015-05-01 03:57:49 +03:00
//
2015-05-01 04:57:37 +03:00
// Special case: nil means o.bazilReq.Respond accepts no parameters.
2015-05-01 03:57:49 +03:00
func (o *commonOp) respond(resp interface{}) {
2015-05-05 03:07:34 +03:00
// Don't forget to report back to the connection that we are finished.
defer o.finished(nil)
2015-05-01 03:57:49 +03:00
// Find the Respond method.
2015-05-01 04:57:37 +03:00
v := reflect.ValueOf(o.bazilReq)
2015-05-01 03:57:49 +03:00
respond := v.MethodByName("Respond")
// Special case: handle successful ops with no response struct.
if resp == nil {
2015-05-01 05:38:03 +03:00
o.Logf("-> (%s) OK", o.op.ShortDesc())
2015-05-01 03:57:49 +03:00
respond.Call([]reflect.Value{})
return
}
2015-05-05 02:58:18 +03:00
// Otherwise, send the response struct to the kernel.
2015-05-01 03:57:49 +03:00
o.Logf("-> %v", resp)
respond.Call([]reflect.Value{reflect.ValueOf(resp)})
}