fusego/samples/cachingfs/caching_fs.go

373 lines
8.2 KiB
Go
Raw Permalink Normal View History

// 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 cachingfs
import (
"context"
2015-07-29 08:43:23 +03:00
"crypto/rand"
2015-03-17 05:25:27 +03:00
"fmt"
2015-07-29 08:43:23 +03:00
"io"
2015-03-17 05:35:47 +03:00
"os"
"time"
"github.com/jacobsa/fuse"
2015-03-24 08:09:29 +03:00
"github.com/jacobsa/fuse/fuseops"
2015-03-25 01:31:43 +03:00
"github.com/jacobsa/fuse/fuseutil"
"github.com/jacobsa/syncutil"
)
2015-03-17 05:22:33 +03:00
const (
// Sizes of the files according to the file system.
FooSize = 123
BarSize = 456
)
// A file system with a fixed structure that looks like this:
//
2022-09-06 09:42:56 +03:00
// foo
// dir/
// bar
//
// The file system is configured with durations that specify how long to allow
// inode entries and attributes to be cached, used when responding to fuse
// requests. It also exposes methods for renumbering inodes and updating mtimes
// that are useful in testing that these durations are honored.
//
// Each file responds to reads with random contents. SetKeepCache can be used
// to control whether the response to OpenFileOp tells the kernel to keep the
// file's data in the page cache or not.
type CachingFS interface {
2015-03-25 01:31:43 +03:00
fuseutil.FileSystem
2015-03-17 04:40:51 +03:00
2015-03-17 05:14:19 +03:00
// Return the current inode ID of the file/directory with the given name.
2015-03-24 08:09:29 +03:00
FooID() fuseops.InodeID
DirID() fuseops.InodeID
BarID() fuseops.InodeID
2015-03-17 05:14:19 +03:00
// Cause the inode IDs to change to values that have never before been used.
RenumberInodes()
2015-03-17 04:40:51 +03:00
// Cause further queries for the attributes of inodes to use the supplied
// time as the inode's mtime.
SetMtime(mtime time.Time)
// Instruct the file system whether or not to reply to OpenFileOp with
// FOPEN_KEEP_CACHE set.
SetKeepCache(keep bool)
}
// Create a file system that issues cacheable responses according to the
// following rules:
//
2022-09-06 09:42:56 +03:00
// - LookUpInodeResponse.Entry.EntryExpiration is set according to
// lookupEntryTimeout.
//
2022-09-06 09:42:56 +03:00
// - GetInodeAttributesResponse.AttributesExpiration is set according to
// getattrTimeout.
//
2022-09-06 09:42:56 +03:00
// - Nothing else is marked cacheable. (In particular, the attributes
// returned by LookUpInode are not cacheable.)
func NewCachingFS(
lookupEntryTimeout time.Duration,
getattrTimeout time.Duration) (CachingFS, error) {
2015-03-24 08:09:29 +03:00
roundUp := func(n fuseops.InodeID) fuseops.InodeID {
2015-03-17 05:42:47 +03:00
return numInodes * ((n + numInodes - 1) / numInodes)
}
2015-03-17 05:22:33 +03:00
cfs := &cachingFS{
2015-03-17 05:34:50 +03:00
lookupEntryTimeout: lookupEntryTimeout,
getattrTimeout: getattrTimeout,
2015-03-24 08:10:28 +03:00
baseID: roundUp(fuseops.RootInodeID + 1),
2015-03-17 05:34:50 +03:00
mtime: time.Now(),
2015-03-17 04:40:51 +03:00
}
2015-03-17 05:22:33 +03:00
cfs.mu = syncutil.NewInvariantMutex(cfs.checkInvariants)
return cfs, nil
2015-03-17 04:40:51 +03:00
}
2015-03-17 05:22:33 +03:00
const (
// Inode IDs are issued such that "foo" always receives an ID that is
// congruent to fooOffset modulo numInodes, etc.
fooOffset = iota
dirOffset
barOffset
numInodes
)
type cachingFS struct {
2015-03-25 01:31:43 +03:00
fuseutil.NotImplementedFileSystem
2015-03-17 05:34:50 +03:00
/////////////////////////
// Constant data
/////////////////////////
lookupEntryTimeout time.Duration
getattrTimeout time.Duration
/////////////////////////
// Mutable state
/////////////////////////
2015-03-17 05:22:33 +03:00
mu syncutil.InvariantMutex
2015-07-29 08:52:25 +03:00
// GUARDED_BY(mu)
keepPageCache bool
2015-03-17 05:23:30 +03:00
// The current ID of the lowest numbered non-root inode.
2015-03-17 05:22:33 +03:00
//
2015-03-24 08:10:28 +03:00
// INVARIANT: baseID > fuseops.RootInodeID
2015-03-17 05:25:27 +03:00
// INVARIANT: baseID % numInodes == 0
2015-03-17 05:22:33 +03:00
//
// GUARDED_BY(mu)
2015-03-24 08:09:29 +03:00
baseID fuseops.InodeID
// GUARDED_BY(mu)
mtime time.Time
}
2015-03-17 05:32:35 +03:00
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
2015-03-17 05:25:27 +03:00
func (fs *cachingFS) checkInvariants() {
2015-03-24 08:10:28 +03:00
// INVARIANT: baseID > fuseops.RootInodeID
2015-03-17 05:25:27 +03:00
// INVARIANT: baseID % numInodes == 0
2015-03-24 08:10:28 +03:00
if fs.baseID <= fuseops.RootInodeID || fs.baseID%numInodes != 0 {
2015-03-17 05:25:27 +03:00
panic(fmt.Sprintf("Bad baseID: %v", fs.baseID))
}
}
2015-03-17 05:22:33 +03:00
2015-03-17 05:32:35 +03:00
// LOCKS_REQUIRED(fs.mu)
2015-03-24 08:09:29 +03:00
func (fs *cachingFS) fooID() fuseops.InodeID {
2015-03-17 05:33:32 +03:00
return fs.baseID + fooOffset
}
2015-03-17 05:32:35 +03:00
// LOCKS_REQUIRED(fs.mu)
2015-03-24 08:09:29 +03:00
func (fs *cachingFS) dirID() fuseops.InodeID {
2015-03-17 05:33:32 +03:00
return fs.baseID + dirOffset
}
2015-03-17 05:32:35 +03:00
// LOCKS_REQUIRED(fs.mu)
2015-03-24 08:09:29 +03:00
func (fs *cachingFS) barID() fuseops.InodeID {
2015-03-17 05:33:32 +03:00
return fs.baseID + barOffset
}
2015-03-17 05:32:35 +03:00
2015-03-17 05:46:15 +03:00
// LOCKS_REQUIRED(fs.mu)
2015-03-24 08:09:29 +03:00
func (fs *cachingFS) rootAttrs() fuseops.InodeAttributes {
return fuseops.InodeAttributes{
2015-03-17 05:46:15 +03:00
Mode: os.ModeDir | 0777,
Mtime: fs.mtime,
}
}
2015-03-17 05:32:35 +03:00
// LOCKS_REQUIRED(fs.mu)
2015-03-24 08:09:29 +03:00
func (fs *cachingFS) fooAttrs() fuseops.InodeAttributes {
return fuseops.InodeAttributes{
2015-03-18 06:14:19 +03:00
Nlink: 1,
2015-03-17 05:35:47 +03:00
Size: FooSize,
Mode: 0777,
Mtime: fs.mtime,
}
}
2015-03-17 05:32:35 +03:00
// LOCKS_REQUIRED(fs.mu)
2015-03-24 08:09:29 +03:00
func (fs *cachingFS) dirAttrs() fuseops.InodeAttributes {
return fuseops.InodeAttributes{
2015-03-18 06:14:19 +03:00
Nlink: 1,
2015-03-17 05:35:47 +03:00
Mode: os.ModeDir | 0777,
Mtime: fs.mtime,
}
}
2015-03-17 05:32:35 +03:00
// LOCKS_REQUIRED(fs.mu)
2015-03-24 08:09:29 +03:00
func (fs *cachingFS) barAttrs() fuseops.InodeAttributes {
return fuseops.InodeAttributes{
2015-03-18 06:14:19 +03:00
Nlink: 1,
2015-03-17 05:35:47 +03:00
Size: BarSize,
Mode: 0777,
Mtime: fs.mtime,
}
}
2015-03-17 05:32:35 +03:00
////////////////////////////////////////////////////////////////////////
// Public interface
////////////////////////////////////////////////////////////////////////
2015-03-17 04:55:24 +03:00
// LOCKS_EXCLUDED(fs.mu)
2015-03-24 08:09:29 +03:00
func (fs *cachingFS) FooID() fuseops.InodeID {
2015-03-17 05:23:30 +03:00
fs.mu.Lock()
defer fs.mu.Unlock()
2015-03-17 05:33:32 +03:00
return fs.fooID()
2015-03-17 05:23:30 +03:00
}
2015-03-17 04:40:51 +03:00
2015-03-17 05:22:33 +03:00
// LOCKS_EXCLUDED(fs.mu)
2015-03-24 08:09:29 +03:00
func (fs *cachingFS) DirID() fuseops.InodeID {
2015-03-17 05:23:58 +03:00
fs.mu.Lock()
defer fs.mu.Unlock()
2015-03-17 05:33:32 +03:00
return fs.dirID()
2015-03-17 05:23:58 +03:00
}
2015-03-17 05:22:33 +03:00
// LOCKS_EXCLUDED(fs.mu)
2015-03-24 08:09:29 +03:00
func (fs *cachingFS) BarID() fuseops.InodeID {
2015-03-17 05:23:58 +03:00
fs.mu.Lock()
defer fs.mu.Unlock()
2015-03-17 05:33:32 +03:00
return fs.barID()
2015-03-17 05:23:58 +03:00
}
2015-03-17 05:22:33 +03:00
// LOCKS_EXCLUDED(fs.mu)
2015-03-17 05:25:46 +03:00
func (fs *cachingFS) RenumberInodes() {
fs.mu.Lock()
defer fs.mu.Unlock()
fs.baseID += numInodes
}
2015-03-17 04:55:24 +03:00
// LOCKS_EXCLUDED(fs.mu)
func (fs *cachingFS) SetMtime(mtime time.Time) {
2015-03-17 04:40:51 +03:00
fs.mu.Lock()
defer fs.mu.Unlock()
fs.mtime = mtime
}
2015-03-17 04:55:24 +03:00
// LOCKS_EXCLUDED(fs.mu)
func (fs *cachingFS) SetKeepCache(keep bool) {
2015-07-29 08:52:25 +03:00
fs.mu.Lock()
defer fs.mu.Unlock()
fs.keepPageCache = keep
}
2015-03-17 05:32:35 +03:00
////////////////////////////////////////////////////////////////////////
2015-03-25 01:31:43 +03:00
// FileSystem methods
2015-03-17 05:32:35 +03:00
////////////////////////////////////////////////////////////////////////
2015-09-09 15:55:39 +03:00
func (fs *cachingFS) StatFS(
ctx context.Context,
op *fuseops.StatFSOp) error {
return nil
2015-09-09 15:55:39 +03:00
}
2015-03-17 05:32:35 +03:00
// LOCKS_EXCLUDED(fs.mu)
2015-03-25 01:31:43 +03:00
func (fs *cachingFS) LookUpInode(
2015-07-27 08:47:45 +03:00
ctx context.Context,
op *fuseops.LookUpInodeOp) error {
2015-03-17 05:32:35 +03:00
fs.mu.Lock()
defer fs.mu.Unlock()
// Find the ID and attributes.
2015-03-24 08:09:29 +03:00
var id fuseops.InodeID
var attrs fuseops.InodeAttributes
2015-03-17 05:32:35 +03:00
2015-03-24 08:13:01 +03:00
switch op.Name {
2015-03-17 05:32:35 +03:00
case "foo":
// Parent must be the root.
2015-03-24 08:13:01 +03:00
if op.Parent != fuseops.RootInodeID {
return fuse.ENOENT
2015-03-17 05:32:35 +03:00
}
id = fs.fooID()
attrs = fs.fooAttrs()
case "dir":
// Parent must be the root.
2015-03-24 08:13:01 +03:00
if op.Parent != fuseops.RootInodeID {
return fuse.ENOENT
2015-03-17 05:32:35 +03:00
}
id = fs.dirID()
attrs = fs.dirAttrs()
case "bar":
// Parent must be dir.
2015-03-24 08:13:01 +03:00
if op.Parent == fuseops.RootInodeID || op.Parent%numInodes != dirOffset {
return fuse.ENOENT
2015-03-17 05:32:35 +03:00
}
id = fs.barID()
attrs = fs.barAttrs()
default:
return fuse.ENOENT
2015-03-17 05:32:35 +03:00
}
// Fill in the response.
2015-03-24 08:13:01 +03:00
op.Entry.Child = id
op.Entry.Attributes = attrs
op.Entry.EntryExpiration = time.Now().Add(fs.lookupEntryTimeout)
2015-03-17 05:32:35 +03:00
return nil
2015-03-17 05:32:35 +03:00
}
2015-03-17 05:46:15 +03:00
// LOCKS_EXCLUDED(fs.mu)
2015-03-25 01:31:43 +03:00
func (fs *cachingFS) GetInodeAttributes(
2015-07-27 08:47:45 +03:00
ctx context.Context,
op *fuseops.GetInodeAttributesOp) error {
2015-03-17 05:46:15 +03:00
fs.mu.Lock()
defer fs.mu.Unlock()
// Figure out which inode the request is for.
2015-03-24 08:09:29 +03:00
var attrs fuseops.InodeAttributes
2015-03-17 05:46:15 +03:00
switch {
2015-03-24 08:13:01 +03:00
case op.Inode == fuseops.RootInodeID:
2015-03-17 05:46:15 +03:00
attrs = fs.rootAttrs()
2015-03-24 08:13:01 +03:00
case op.Inode%numInodes == fooOffset:
2015-03-17 05:46:15 +03:00
attrs = fs.fooAttrs()
2015-03-24 08:13:01 +03:00
case op.Inode%numInodes == dirOffset:
2015-03-17 05:46:15 +03:00
attrs = fs.dirAttrs()
2015-03-24 08:13:01 +03:00
case op.Inode%numInodes == barOffset:
2015-03-17 05:46:15 +03:00
attrs = fs.barAttrs()
}
// Fill in the response.
2015-03-24 08:13:01 +03:00
op.Attributes = attrs
op.AttributesExpiration = time.Now().Add(fs.getattrTimeout)
2015-03-17 05:46:15 +03:00
return nil
2015-03-17 05:46:15 +03:00
}
2015-03-17 06:23:36 +03:00
2015-03-25 01:31:43 +03:00
func (fs *cachingFS) OpenDir(
2015-07-27 08:47:45 +03:00
ctx context.Context,
op *fuseops.OpenDirOp) error {
return nil
2015-03-17 06:23:36 +03:00
}
2015-03-25 01:31:43 +03:00
func (fs *cachingFS) OpenFile(
2015-07-27 08:47:45 +03:00
ctx context.Context,
op *fuseops.OpenFileOp) error {
2015-07-29 08:52:25 +03:00
fs.mu.Lock()
defer fs.mu.Unlock()
op.KeepPageCache = fs.keepPageCache
return nil
2015-03-17 06:23:36 +03:00
}
2015-07-29 08:43:23 +03:00
func (fs *cachingFS) ReadFile(
ctx context.Context,
op *fuseops.ReadFileOp) error {
var err error
2015-07-29 08:43:23 +03:00
op.BytesRead, err = io.ReadFull(rand.Reader, op.Dst)
return err
2015-07-29 08:43:23 +03:00
}