For things that we can check if they are mounted by using their fsmagic
we should use that and for others do it the slow way.
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
| ... | ... |
@@ -1,10 +1,6 @@ |
| 1 | 1 |
package graphdriver |
| 2 | 2 |
|
| 3 |
-import ( |
|
| 4 |
- "sync" |
|
| 5 |
- |
|
| 6 |
- "github.com/docker/docker/pkg/mount" |
|
| 7 |
-) |
|
| 3 |
+import "sync" |
|
| 8 | 4 |
|
| 9 | 5 |
type minfo struct {
|
| 10 | 6 |
check bool |
| ... | ... |
@@ -13,13 +9,20 @@ type minfo struct {
|
| 13 | 13 |
|
| 14 | 14 |
// RefCounter is a generic counter for use by graphdriver Get/Put calls |
| 15 | 15 |
type RefCounter struct {
|
| 16 |
- counts map[string]*minfo |
|
| 17 |
- mu sync.Mutex |
|
| 16 |
+ counts map[string]*minfo |
|
| 17 |
+ mu sync.Mutex |
|
| 18 |
+ checker Checker |
|
| 18 | 19 |
} |
| 19 | 20 |
|
| 20 | 21 |
// NewRefCounter returns a new RefCounter |
| 21 |
-func NewRefCounter() *RefCounter {
|
|
| 22 |
- return &RefCounter{counts: make(map[string]*minfo)}
|
|
| 22 |
+func NewRefCounter(c Checker) *RefCounter {
|
|
| 23 |
+ if c == nil {
|
|
| 24 |
+ c = &defaultChecker{}
|
|
| 25 |
+ } |
|
| 26 |
+ return &RefCounter{
|
|
| 27 |
+ checker: c, |
|
| 28 |
+ counts: make(map[string]*minfo), |
|
| 29 |
+ } |
|
| 23 | 30 |
} |
| 24 | 31 |
|
| 25 | 32 |
// 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 {
|
| 35 | 35 |
// count if it is mounted as it is in use. |
| 36 | 36 |
if !m.check {
|
| 37 | 37 |
m.check = true |
| 38 |
- mntd, _ := mount.Mounted(path) |
|
| 39 |
- if mntd {
|
|
| 38 |
+ if c.checker.IsMounted(path) {
|
|
| 40 | 39 |
m.count++ |
| 41 | 40 |
} |
| 42 | 41 |
} |
| ... | ... |
@@ -58,8 +60,7 @@ func (c *RefCounter) Decrement(path string) int {
|
| 58 | 58 |
// count if it is mounted as it is in use. |
| 59 | 59 |
if !m.check {
|
| 60 | 60 |
m.check = true |
| 61 |
- mntd, _ := mount.Mounted(path) |
|
| 62 |
- if mntd {
|
|
| 61 |
+ if c.checker.IsMounted(path) {
|
|
| 63 | 62 |
m.count++ |
| 64 | 63 |
} |
| 65 | 64 |
} |
| ... | ... |
@@ -47,7 +47,7 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap |
| 47 | 47 |
home: home, |
| 48 | 48 |
uidMaps: uidMaps, |
| 49 | 49 |
gidMaps: gidMaps, |
| 50 |
- ctr: graphdriver.NewRefCounter(), |
|
| 50 |
+ ctr: graphdriver.NewRefCounter(nil), |
|
| 51 | 51 |
} |
| 52 | 52 |
|
| 53 | 53 |
return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil |
| ... | ... |
@@ -5,6 +5,8 @@ package graphdriver |
| 5 | 5 |
import ( |
| 6 | 6 |
"path/filepath" |
| 7 | 7 |
"syscall" |
| 8 |
+ |
|
| 9 |
+ "github.com/docker/docker/pkg/mount" |
|
| 8 | 10 |
) |
| 9 | 11 |
|
| 10 | 12 |
const ( |
| ... | ... |
@@ -89,6 +91,36 @@ func GetFSMagic(rootpath string) (FsMagic, error) {
|
| 89 | 89 |
return FsMagic(buf.Type), nil |
| 90 | 90 |
} |
| 91 | 91 |
|
| 92 |
+// Checker makes checks on specified filesystems. |
|
| 93 |
+type Checker interface {
|
|
| 94 |
+ // IsMounted returns true if the provided path is mounted for the specific checker |
|
| 95 |
+ IsMounted(path string) bool |
|
| 96 |
+} |
|
| 97 |
+ |
|
| 98 |
+// NewFsChecker returns a checker configured for the provied FsMagic |
|
| 99 |
+func NewFsChecker(t FsMagic) Checker {
|
|
| 100 |
+ return &fsChecker{
|
|
| 101 |
+ t: t, |
|
| 102 |
+ } |
|
| 103 |
+} |
|
| 104 |
+ |
|
| 105 |
+type fsChecker struct {
|
|
| 106 |
+ t FsMagic |
|
| 107 |
+} |
|
| 108 |
+ |
|
| 109 |
+func (c *fsChecker) IsMounted(path string) bool {
|
|
| 110 |
+ m, _ := Mounted(c.t, path) |
|
| 111 |
+ return m |
|
| 112 |
+} |
|
| 113 |
+ |
|
| 114 |
+type defaultChecker struct {
|
|
| 115 |
+} |
|
| 116 |
+ |
|
| 117 |
+func (c *defaultChecker) IsMounted(path string) bool {
|
|
| 118 |
+ m, _ := mount.Mounted(path) |
|
| 119 |
+ return m |
|
| 120 |
+} |
|
| 121 |
+ |
|
| 92 | 122 |
// Mounted checks if the given path is mounted as the fs type |
| 93 | 123 |
func Mounted(fsType FsMagic, mountPath string) (bool, error) {
|
| 94 | 124 |
var buf syscall.Statfs_t |
| ... | ... |
@@ -141,7 +141,7 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap |
| 141 | 141 |
home: home, |
| 142 | 142 |
uidMaps: uidMaps, |
| 143 | 143 |
gidMaps: gidMaps, |
| 144 |
- ctr: graphdriver.NewRefCounter(), |
|
| 144 |
+ ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicOverlay)), |
|
| 145 | 145 |
} |
| 146 | 146 |
|
| 147 | 147 |
return NaiveDiffDriverWithApply(d, uidMaps, gidMaps), nil |
| ... | ... |
@@ -105,7 +105,7 @@ func Init(base string, opt []string, uidMaps, gidMaps []idtools.IDMap) (graphdri |
| 105 | 105 |
filesystemsCache: filesystemsCache, |
| 106 | 106 |
uidMaps: uidMaps, |
| 107 | 107 |
gidMaps: gidMaps, |
| 108 |
- ctr: graphdriver.NewRefCounter(), |
|
| 108 |
+ ctr: graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicZfs)), |
|
| 109 | 109 |
} |
| 110 | 110 |
return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil |
| 111 | 111 |
} |