Skip to content

Commit

Permalink
daemon/graphdriver: move RefCounter to an internal package
Browse files Browse the repository at this point in the history
The RefCounter is used in both graphdrivers and snapshotters. Move it
to a separate package to help decoupling snapshotters and graphdrivers,
and make it internal, as it's not intended to be used as a generic utility
package (we can still make it public if there would be a need).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
  • Loading branch information
thaJeztah committed Jul 1, 2024
1 parent e558979 commit 0f3273e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
5 changes: 3 additions & 2 deletions daemon/graphdriver/fuse-overlayfs/fuseoverlayfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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(),
}

Expand Down
5 changes: 3 additions & 2 deletions daemon/graphdriver/overlay2/overlay.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
5 changes: 3 additions & 2 deletions daemon/graphdriver/windows/windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions daemon/graphdriver/zfs/zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package graphdriver // import "github.com/docker/docker/daemon/graphdriver"
package mountref

import "sync"

Expand All @@ -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
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions daemon/snapshotter/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down

0 comments on commit 0f3273e

Please sign in to comment.