daemon/graphdriver: simplify Checker, remove NewFsChecker, NewDefaultChecker

The Checker interface was introduced in 1ba05cdb6a
as an optimization to allow passing a simplified check for situations that
don't require mountinfo.Mounted to be executed (as that may result in parsing
 `/proc/self/mountinfo`).

The Checker was defined as an interface with a single `IsMounted` method,
possibly with the intent to allow for additional kind of checks to be added.
No new additions were made since its inception 9 Years ago, and if a need would
arrive, could probably be implemented as part of the check.

This patch simplifies the definition to a function, removing the need to
implement a wrapper struct just to satisfy the interface. The `Checker`
type definition is somewhat redundant, but is kept to have a place to
provide GoDoc.

The `NewFsChecker` and `NewDefaultChecker` utilities are removed as part
of this change, favoring a local definition for storage-drivers that
used them.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn
2024-06-30 13:13:32 +02:00
parent 554db8f113
commit e55897977c
10 changed files with 48 additions and 70 deletions

View File

@@ -9,16 +9,20 @@ type minfo struct {
// RefCounter is a generic counter for use by graphdriver Get/Put calls
type RefCounter struct {
counts map[string]*minfo
mu sync.Mutex
checker Checker
counts map[string]*minfo
mu sync.Mutex
isMounted Checker
}
// NewRefCounter returns a new RefCounter
// Checker checks whether the provided path is mounted.
type Checker func(path string) bool
// NewRefCounter returns a new RefCounter. It accepts a [Checker] to
// determine whether a path is mounted.
func NewRefCounter(c Checker) *RefCounter {
return &RefCounter{
checker: c,
counts: make(map[string]*minfo),
isMounted: c,
counts: make(map[string]*minfo),
}
}
@@ -48,7 +52,7 @@ func (c *RefCounter) incdec(path string, infoOp func(minfo *minfo)) int {
// count if it is mounted as it is in use.
if !m.check {
m.check = true
if c.checker.IsMounted(path) {
if c.isMounted(path) {
m.count++
}
}

View File

@@ -128,12 +128,6 @@ type FileGetCloser interface {
Close() error
}
// Checker makes checks on specified filesystems.
type Checker interface {
// IsMounted returns true if the provided path is mounted for the specific checker
IsMounted(path string) bool
}
func init() {
drivers = make(map[string]InitFunc)
}

View File

@@ -1,38 +1,4 @@
package graphdriver // import "github.com/docker/docker/daemon/graphdriver"
import (
"github.com/docker/docker/daemon/internal/fstype"
"github.com/moby/sys/mountinfo"
)
// List of drivers that should be used in an order
var priority = "overlay2,fuse-overlayfs,btrfs,zfs,vfs"
// NewFsChecker returns a checker configured for the provided FsMagic
func NewFsChecker(t fstype.FsMagic) Checker {
return &fsChecker{
t: t,
}
}
type fsChecker struct {
t fstype.FsMagic
}
func (c *fsChecker) IsMounted(path string) bool {
fsType, _ := fstype.GetFSMagic(path)
return fsType == c.t
}
// NewDefaultChecker returns a check that parses /proc/mountinfo to check
// if the specified path is mounted.
func NewDefaultChecker() Checker {
return &defaultChecker{}
}
type defaultChecker struct{}
func (c *defaultChecker) IsMounted(path string) bool {
m, _ := mountinfo.Mounted(path)
return m
}

View File

@@ -98,7 +98,7 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr
d := &Driver{
home: home,
idMap: idMap,
ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(fstype.FsMagicFUSE)),
ctr: graphdriver.NewRefCounter(isMounted),
locker: locker.New(),
}
@@ -107,6 +107,12 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr
return d, nil
}
// isMounted checks whether the given path is a [fstype.FsMagicFUSE] mount.
func isMounted(path string) bool {
fsType, _ := fstype.GetFSMagic(path)
return fsType == fstype.FsMagicFUSE
}
func (d *Driver) String() string {
return driverName
}

View File

@@ -179,7 +179,7 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr
d := &Driver{
home: home,
idMap: idMap,
ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(fstype.FsMagicOverlay)),
ctr: graphdriver.NewRefCounter(isMounted),
supportsDType: supportsDType,
usingMetacopy: usingMetacopy,
locker: locker.New(),
@@ -225,6 +225,12 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr
return d, nil
}
// isMounted checks whether the given path is a [fstype.FsMagicOverlay] mount.
func isMounted(path string) bool {
fsType, _ := fstype.GetFSMagic(path)
return fsType == fstype.FsMagicOverlay
}
func parseOptions(options []string) (*overlayOptions, error) {
o := &overlayOptions{}
for _, option := range options {

View File

@@ -72,12 +72,6 @@ func init() {
}
}
type checker struct{}
func (c *checker) IsMounted(path string) bool {
return false
}
type storageOptions struct {
size uint64
}
@@ -132,12 +126,18 @@ func InitFilter(home string, options []string, _ idtools.IdentityMapping) (graph
Flavour: filterDriver,
},
cache: make(map[string]string),
ctr: graphdriver.NewRefCounter(&checker{}),
ctr: graphdriver.NewRefCounter(isMounted),
defaultStorageOpts: opts,
}
return d, nil
}
// isMounted checks whether the given path is mounted. It always returns
// false for the WindowsFilter graphdriver.
func isMounted(string) bool {
return false
}
// String returns the string representation of a driver. This should match
// the name the graph driver has been registered with.
func (d *Driver) String() string {

View File

@@ -118,12 +118,19 @@ func Init(base string, opt []string, idMap idtools.IdentityMapping) (graphdriver
options: options,
filesystemsCache: filesystemsCache,
idMap: idMap,
ctr: graphdriver.NewRefCounter(graphdriver.NewDefaultChecker()),
ctr: graphdriver.NewRefCounter(isMounted),
locker: locker.New(),
}
return graphdriver.NewNaiveDiffDriver(d, idMap), nil
}
// isMounted parses /proc/mountinfo to check whether the specified path
// is mounted.
func isMounted(path string) bool {
m, _ := mountinfo.Mounted(path)
return m
}
func parseOptions(opt []string) (zfsOptions, error) {
var options zfsOptions
options.fsName = ""