Browse code

Add reference counting to aufs

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>

Michael Crosby authored on 2016/05/07 05:09:45
Showing 5 changed files
... ...
@@ -70,6 +70,7 @@ type Driver struct {
70 70
 	root          string
71 71
 	uidMaps       []idtools.IDMap
72 72
 	gidMaps       []idtools.IDMap
73
+	ctr           *graphdriver.RefCounter
73 74
 	pathCacheLock sync.Mutex
74 75
 	pathCache     map[string]string
75 76
 }
... ...
@@ -108,6 +109,7 @@ func Init(root string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
108 108
 		uidMaps:   uidMaps,
109 109
 		gidMaps:   gidMaps,
110 110
 		pathCache: make(map[string]string),
111
+		ctr:       graphdriver.NewRefCounter(graphdriver.NewFsChecker(graphdriver.FsMagicAufs)),
111 112
 	}
112 113
 
113 114
 	rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
... ...
@@ -320,6 +322,9 @@ func (a *Driver) Get(id, mountLabel string) (string, error) {
320 320
 			m = a.getMountpoint(id)
321 321
 		}
322 322
 	}
323
+	if count := a.ctr.Increment(m); count > 1 {
324
+		return m, nil
325
+	}
323 326
 
324 327
 	// If a dir does not have a parent ( no layers )do not try to mount
325 328
 	// just return the diff path to the data
... ...
@@ -344,6 +349,9 @@ func (a *Driver) Put(id string) error {
344 344
 		a.pathCache[id] = m
345 345
 	}
346 346
 	a.pathCacheLock.Unlock()
347
+	if count := a.ctr.Decrement(m); count > 0 {
348
+		return nil
349
+	}
347 350
 
348 351
 	err := a.unmount(m)
349 352
 	if err != nil {
... ...
@@ -16,9 +16,6 @@ type RefCounter struct {
16 16
 
17 17
 // NewRefCounter returns a new RefCounter
18 18
 func NewRefCounter(c Checker) *RefCounter {
19
-	if c == nil {
20
-		c = &defaultChecker{}
21
-	}
22 19
 	return &RefCounter{
23 20
 		checker: c,
24 21
 		counts:  make(map[string]*minfo),
... ...
@@ -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(nil),
50
+		ctr:       graphdriver.NewRefCounter(graphdriver.NewDefaultChecker()),
51 51
 	}
52 52
 
53 53
 	return graphdriver.NewNaiveDiffDriver(d, uidMaps, gidMaps), nil
... ...
@@ -113,6 +113,12 @@ type FileGetCloser interface {
113 113
 	Close() error
114 114
 }
115 115
 
116
+// Checker makes checks on specified filesystems.
117
+type Checker interface {
118
+	// IsMounted returns true if the provided path is mounted for the specific checker
119
+	IsMounted(path string) bool
120
+}
121
+
116 122
 func init() {
117 123
 	drivers = make(map[string]InitFunc)
118 124
 }
... ...
@@ -91,12 +91,6 @@ func GetFSMagic(rootpath string) (FsMagic, error) {
91 91
 	return FsMagic(buf.Type), nil
92 92
 }
93 93
 
94
-// Checker makes checks on specified filesystems.
95
-type Checker interface {
96
-	// IsMounted returns true if the provided path is mounted for the specific checker
97
-	IsMounted(path string) bool
98
-}
99
-
100 94
 // NewFsChecker returns a checker configured for the provied FsMagic
101 95
 func NewFsChecker(t FsMagic) Checker {
102 96
 	return &fsChecker{
... ...
@@ -113,6 +107,12 @@ func (c *fsChecker) IsMounted(path string) bool {
113 113
 	return m
114 114
 }
115 115
 
116
+// NewDefaultChecker returns a check that parses /proc/mountinfo to check
117
+// if the specified path is mounted.
118
+func NewDefaultChecker() Checker {
119
+	return &defaultChecker{}
120
+}
121
+
116 122
 type defaultChecker struct {
117 123
 }
118 124