diff --git a/daemon/graphdriver/fuse-overlayfs/fuseoverlayfs.go b/daemon/graphdriver/fuse-overlayfs/fuseoverlayfs.go index 1b713fb7c2b43..905af955b685e 100644 --- a/daemon/graphdriver/fuse-overlayfs/fuseoverlayfs.go +++ b/daemon/graphdriver/fuse-overlayfs/fuseoverlayfs.go @@ -18,6 +18,7 @@ import ( "github.com/docker/docker/daemon/graphdriver" "github.com/docker/docker/daemon/graphdriver/overlayutils" "github.com/docker/docker/daemon/internal/fstype" + "github.com/docker/docker/daemon/internal/mountref" "github.com/docker/docker/internal/containerfs" "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/chrootarchive" @@ -59,7 +60,7 @@ const ( type Driver struct { home string idMap idtools.IdentityMapping - ctr *graphdriver.RefCounter + ctr *mountref.Counter naiveDiff graphdriver.DiffDriver locker *locker.Locker } @@ -98,7 +99,7 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr d := &Driver{ home: home, idMap: idMap, - ctr: graphdriver.NewRefCounter(isMounted), + ctr: mountref.NewCounter(isMounted), locker: locker.New(), } diff --git a/daemon/graphdriver/overlay2/overlay.go b/daemon/graphdriver/overlay2/overlay.go index ab3090550bf21..f563fa194ac27 100644 --- a/daemon/graphdriver/overlay2/overlay.go +++ b/daemon/graphdriver/overlay2/overlay.go @@ -20,6 +20,7 @@ import ( "github.com/docker/docker/daemon/graphdriver" "github.com/docker/docker/daemon/graphdriver/overlayutils" "github.com/docker/docker/daemon/internal/fstype" + "github.com/docker/docker/daemon/internal/mountref" "github.com/docker/docker/internal/containerfs" "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/chrootarchive" @@ -93,7 +94,7 @@ type overlayOptions struct { type Driver struct { home string idMap idtools.IdentityMapping - ctr *graphdriver.RefCounter + ctr *mountref.Counter quotaCtl *quota.Control options overlayOptions naiveDiff graphdriver.DiffDriver @@ -179,7 +180,7 @@ func Init(home string, options []string, idMap idtools.IdentityMapping) (graphdr d := &Driver{ home: home, idMap: idMap, - ctr: graphdriver.NewRefCounter(isMounted), + ctr: mountref.NewCounter(isMounted), supportsDType: supportsDType, usingMetacopy: usingMetacopy, locker: locker.New(), diff --git a/daemon/graphdriver/windows/windows.go b/daemon/graphdriver/windows/windows.go index 0d4ace6bc8ce1..8b85bad204664 100644 --- a/daemon/graphdriver/windows/windows.go +++ b/daemon/graphdriver/windows/windows.go @@ -26,6 +26,7 @@ import ( "github.com/Microsoft/hcsshim/osversion" "github.com/containerd/log" "github.com/docker/docker/daemon/graphdriver" + "github.com/docker/docker/daemon/internal/mountref" "github.com/docker/docker/pkg/archive" "github.com/docker/docker/pkg/idtools" "github.com/docker/docker/pkg/ioutils" @@ -80,7 +81,7 @@ type storageOptions struct { type Driver struct { // info stores the shim driver information info hcsshim.DriverInfo - ctr *graphdriver.RefCounter + ctr *mountref.Counter // it is safe for windows to use a cache here because it does not support // restoring containers when the daemon dies. cacheMu sync.Mutex @@ -126,7 +127,7 @@ func InitFilter(home string, options []string, _ idtools.IdentityMapping) (graph Flavour: filterDriver, }, cache: make(map[string]string), - ctr: graphdriver.NewRefCounter(isMounted), + ctr: mountref.NewCounter(isMounted), defaultStorageOpts: opts, } return d, nil diff --git a/daemon/graphdriver/zfs/zfs.go b/daemon/graphdriver/zfs/zfs.go index f562975f9d674..cca34d6284739 100644 --- a/daemon/graphdriver/zfs/zfs.go +++ b/daemon/graphdriver/zfs/zfs.go @@ -15,6 +15,7 @@ import ( "github.com/containerd/log" "github.com/docker/docker/daemon/graphdriver" + "github.com/docker/docker/daemon/internal/mountref" "github.com/docker/docker/pkg/idtools" "github.com/docker/docker/pkg/parsers" zfs "github.com/mistifyio/go-zfs/v3" @@ -118,7 +119,7 @@ func Init(base string, opt []string, idMap idtools.IdentityMapping) (graphdriver options: options, filesystemsCache: filesystemsCache, idMap: idMap, - ctr: graphdriver.NewRefCounter(isMounted), + ctr: mountref.NewCounter(isMounted), locker: locker.New(), } return graphdriver.NewNaiveDiffDriver(d, idMap), nil @@ -182,7 +183,7 @@ type Driver struct { sync.Mutex // protects filesystem cache against concurrent access filesystemsCache map[string]bool idMap idtools.IdentityMapping - ctr *graphdriver.RefCounter + ctr *mountref.Counter locker *locker.Locker } diff --git a/daemon/graphdriver/counter.go b/daemon/internal/mountref/counter.go similarity index 69% rename from daemon/graphdriver/counter.go rename to daemon/internal/mountref/counter.go index 3dc363f92a314..f005b19ca7312 100644 --- a/daemon/graphdriver/counter.go +++ b/daemon/internal/mountref/counter.go @@ -1,4 +1,4 @@ -package graphdriver // import "github.com/docker/docker/daemon/graphdriver" +package mountref import "sync" @@ -7,8 +7,8 @@ type minfo struct { count int } -// RefCounter is a generic counter for use by graphdriver Get/Put calls -type RefCounter struct { +// Counter is a generic counter for use by graphdriver Get/Put calls +type Counter struct { counts map[string]*minfo mu sync.Mutex isMounted Checker @@ -17,30 +17,30 @@ type RefCounter struct { // Checker checks whether the provided path is mounted. type Checker func(path string) bool -// NewRefCounter returns a new RefCounter. It accepts a [Checker] to +// NewCounter returns a new Counter. It accepts a [Checker] to // determine whether a path is mounted. -func NewRefCounter(c Checker) *RefCounter { - return &RefCounter{ +func NewCounter(c Checker) *Counter { + return &Counter{ isMounted: c, counts: make(map[string]*minfo), } } // Increment increases the ref count for the given id and returns the current count -func (c *RefCounter) Increment(path string) int { +func (c *Counter) Increment(path string) int { return c.incdec(path, func(minfo *minfo) { minfo.count++ }) } // Decrement decreases the ref count for the given id and returns the current count -func (c *RefCounter) Decrement(path string) int { +func (c *Counter) Decrement(path string) int { return c.incdec(path, func(minfo *minfo) { minfo.count-- }) } -func (c *RefCounter) incdec(path string, infoOp func(minfo *minfo)) int { +func (c *Counter) incdec(path string, infoOp func(minfo *minfo)) int { c.mu.Lock() m := c.counts[path] if m == nil { diff --git a/daemon/snapshotter/mount.go b/daemon/snapshotter/mount.go index f1faee3d5a5ba..1f94901c9255e 100644 --- a/daemon/snapshotter/mount.go +++ b/daemon/snapshotter/mount.go @@ -7,7 +7,7 @@ import ( "github.com/containerd/containerd/mount" "github.com/containerd/log" - "github.com/docker/docker/daemon/graphdriver" + "github.com/docker/docker/daemon/internal/mountref" "github.com/docker/docker/pkg/idtools" "github.com/moby/locker" "github.com/moby/sys/mountinfo" @@ -32,13 +32,13 @@ func NewMounter(home string, snapshotter string, idMap idtools.IdentityMapping) snapshotter: snapshotter, idMap: idMap, }, - rc: graphdriver.NewRefCounter(isMounted), + rc: mountref.NewCounter(isMounted), locker: locker.New(), } } type refCountMounter struct { - rc *graphdriver.RefCounter + rc *mountref.Counter locker *locker.Locker base mounter }