diff --git a/daemon/graphdriver/counter.go b/daemon/graphdriver/counter.go index 1b8e20a767..2de80b7e1e 100644 --- a/daemon/graphdriver/counter.go +++ b/daemon/graphdriver/counter.go @@ -1,10 +1,6 @@ package graphdriver -import ( - "sync" - - "github.com/docker/docker/pkg/mount" -) +import "sync" type minfo struct { check bool @@ -13,13 +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 + counts map[string]*minfo + mu sync.Mutex + checker Checker } // NewRefCounter returns a new RefCounter -func NewRefCounter() *RefCounter { - return &RefCounter{counts: make(map[string]*minfo)} +func NewRefCounter(c Checker) *RefCounter { + if c == nil { + c = &defaultChecker{} + } + return &RefCounter{ + checker: c, + counts: make(map[string]*minfo), + } } // Increment increaes the ref count for the given id and returns the current count @@ -35,8 +38,7 @@ func (c *RefCounter) Increment(path string) int { // count if it is mounted as it is in use. if !m.check { m.check = true - mntd, _ := mount.Mounted(path) - if mntd { + if c.checker.IsMounted(path) { m.count++ } } @@ -58,8 +60,7 @@ func (c *RefCounter) Decrement(path string) int { // count if it is mounted as it is in use. if !m.check { m.check = true - mntd, _ := mount.Mounted(path) - if mntd { + if c.checker.IsMounted(path) { m.count++ } } diff --git a/daemon/graphdriver/devmapper/driver.go b/daemon/graphdriver/devmapper/driver.go index 85a28b59ed..9b58dba66d 100644 --- a/daemon/graphdriver/devmapper/driver.go +++ b/daemon/graphdriver/devmapper/driver.go @@ -47,7 +47,7 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap home: home, uidMaps: uidMaps, gidMaps: gidMaps, - ctr: graphdriver.NewRefCounter(), + ctr: graphdriver.NewRefCounter(nil), } return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil diff --git a/daemon/graphdriver/driver_linux.go b/daemon/graphdriver/driver_linux.go index 2ab20b01a9..10a1abcf0d 100644 --- a/daemon/graphdriver/driver_linux.go +++ b/daemon/graphdriver/driver_linux.go @@ -5,6 +5,8 @@ package graphdriver import ( "path/filepath" "syscall" + + "github.com/docker/docker/pkg/mount" ) const ( @@ -89,6 +91,36 @@ func GetFSMagic(rootpath string) (FsMagic, error) { return FsMagic(buf.Type), nil } +// 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 +} + +// NewFsChecker returns a checker configured for the provied FsMagic +func NewFsChecker(t FsMagic) Checker { + return &fsChecker{ + t: t, + } +} + +type fsChecker struct { + t FsMagic +} + +func (c *fsChecker) IsMounted(path string) bool { + m, _ := Mounted(c.t, path) + return m +} + +type defaultChecker struct { +} + +func (c *defaultChecker) IsMounted(path string) bool { + m, _ := mount.Mounted(path) + return m +} + // Mounted checks if the given path is mounted as the fs type func Mounted(fsType FsMagic, mountPath string) (bool, error) { var buf syscall.Statfs_t diff --git a/daemon/graphdriver/overlay/overlay.go b/daemon/graphdriver/overlay/overlay.go index e2b76b39b1..1abab6b265 100644 --- a/daemon/graphdriver/overlay/overlay.go +++ b/daemon/graphdriver/overlay/overlay.go @@ -141,7 +141,7 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap home: home, uidMaps: uidMaps, gidMaps: gidMaps, - ctr: graphdriver.NewRefCounter(), + ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicOverlay)), } return NaiveDiffDriverWithApply(d, uidMaps, gidMaps), nil diff --git a/daemon/graphdriver/zfs/zfs.go b/daemon/graphdriver/zfs/zfs.go index 1ce71a68d1..7a4ec1d5cf 100644 --- a/daemon/graphdriver/zfs/zfs.go +++ b/daemon/graphdriver/zfs/zfs.go @@ -105,7 +105,7 @@ func Init(base string, opt []string, uidMaps, gidMaps []idtools.IDMap) (graphdri filesystemsCache: filesystemsCache, uidMaps: uidMaps, gidMaps: gidMaps, - ctr: graphdriver.NewRefCounter(), + ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicZfs)), } return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil }