2015-11-18 17:15:00 -05:00
|
|
|
package layer
|
|
|
|
|
2015-12-16 17:13:50 -05:00
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/archive"
|
|
|
|
)
|
2015-11-18 17:15:00 -05:00
|
|
|
|
|
|
|
type mountedLayer struct {
|
2015-12-16 17:13:50 -05:00
|
|
|
name string
|
|
|
|
mountID string
|
|
|
|
initID string
|
|
|
|
parent *roLayer
|
2016-03-09 16:23:04 -05:00
|
|
|
path string
|
2015-12-16 17:13:50 -05:00
|
|
|
layerStore *layerStore
|
|
|
|
|
|
|
|
references map[RWLayer]*referencedRWLayer
|
2015-11-18 17:15:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mountedLayer) cacheParent() string {
|
|
|
|
if ml.initID != "" {
|
|
|
|
return ml.initID
|
|
|
|
}
|
|
|
|
if ml.parent != nil {
|
|
|
|
return ml.parent.cacheID
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2015-11-25 19:39:54 -05:00
|
|
|
func (ml *mountedLayer) TarStream() (io.ReadCloser, error) {
|
2015-11-18 17:15:00 -05:00
|
|
|
archiver, err := ml.layerStore.driver.Diff(ml.mountID, ml.cacheParent())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-25 19:39:54 -05:00
|
|
|
return archiver, nil
|
2015-11-18 17:15:00 -05:00
|
|
|
}
|
|
|
|
|
2015-12-16 17:13:50 -05:00
|
|
|
func (ml *mountedLayer) Name() string {
|
|
|
|
return ml.name
|
2015-11-18 17:15:00 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mountedLayer) Parent() Layer {
|
|
|
|
if ml.parent != nil {
|
|
|
|
return ml.parent
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a nil interface instead of an interface wrapping a nil
|
|
|
|
// pointer.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-16 17:13:50 -05:00
|
|
|
func (ml *mountedLayer) Mount(mountLabel string) (string, error) {
|
|
|
|
return ml.layerStore.driver.Get(ml.mountID, mountLabel)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mountedLayer) Unmount() error {
|
|
|
|
return ml.layerStore.driver.Put(ml.mountID)
|
|
|
|
}
|
|
|
|
|
2015-11-18 17:15:00 -05:00
|
|
|
func (ml *mountedLayer) Size() (int64, error) {
|
|
|
|
return ml.layerStore.driver.DiffSize(ml.mountID, ml.cacheParent())
|
|
|
|
}
|
2015-12-16 17:13:50 -05:00
|
|
|
|
|
|
|
func (ml *mountedLayer) Changes() ([]archive.Change, error) {
|
|
|
|
return ml.layerStore.driver.Changes(ml.mountID, ml.cacheParent())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mountedLayer) Metadata() (map[string]string, error) {
|
|
|
|
return ml.layerStore.driver.GetMetadata(ml.mountID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mountedLayer) getReference() RWLayer {
|
|
|
|
ref := &referencedRWLayer{
|
|
|
|
mountedLayer: ml,
|
|
|
|
}
|
|
|
|
ml.references[ref] = ref
|
|
|
|
|
|
|
|
return ref
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ml *mountedLayer) hasReferences() bool {
|
|
|
|
return len(ml.references) > 0
|
|
|
|
}
|
|
|
|
|
2016-03-04 17:41:53 -05:00
|
|
|
func (ml *mountedLayer) incActivityCount(ref RWLayer) error {
|
|
|
|
rl, ok := ml.references[ref]
|
|
|
|
if !ok {
|
|
|
|
return ErrLayerNotRetained
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rl.acquire(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-16 17:13:50 -05:00
|
|
|
func (ml *mountedLayer) deleteReference(ref RWLayer) error {
|
|
|
|
rl, ok := ml.references[ref]
|
|
|
|
if !ok {
|
|
|
|
return ErrLayerNotRetained
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := rl.release(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
delete(ml.references, ref)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-19 13:42:29 -05:00
|
|
|
func (ml *mountedLayer) retakeReference(r RWLayer) {
|
|
|
|
if ref, ok := r.(*referencedRWLayer); ok {
|
|
|
|
ref.activityCount = 0
|
|
|
|
ml.references[ref] = ref
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-16 17:13:50 -05:00
|
|
|
type referencedRWLayer struct {
|
|
|
|
*mountedLayer
|
|
|
|
|
|
|
|
activityL sync.Mutex
|
|
|
|
activityCount int
|
|
|
|
}
|
|
|
|
|
2016-03-04 17:41:53 -05:00
|
|
|
func (rl *referencedRWLayer) acquire() error {
|
|
|
|
rl.activityL.Lock()
|
|
|
|
defer rl.activityL.Unlock()
|
|
|
|
|
|
|
|
rl.activityCount++
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-12-16 17:13:50 -05:00
|
|
|
func (rl *referencedRWLayer) release() error {
|
|
|
|
rl.activityL.Lock()
|
|
|
|
defer rl.activityL.Unlock()
|
|
|
|
|
|
|
|
if rl.activityCount > 0 {
|
|
|
|
return ErrActiveMount
|
|
|
|
}
|
|
|
|
|
|
|
|
rl.activityCount = -1
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rl *referencedRWLayer) Mount(mountLabel string) (string, error) {
|
|
|
|
rl.activityL.Lock()
|
|
|
|
defer rl.activityL.Unlock()
|
|
|
|
|
|
|
|
if rl.activityCount == -1 {
|
|
|
|
return "", ErrLayerNotRetained
|
|
|
|
}
|
|
|
|
|
2016-03-09 16:23:04 -05:00
|
|
|
if rl.activityCount > 0 {
|
|
|
|
rl.activityCount++
|
|
|
|
return rl.path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
m, err := rl.mountedLayer.Mount(mountLabel)
|
|
|
|
if err == nil {
|
|
|
|
rl.activityCount++
|
|
|
|
rl.path = m
|
|
|
|
}
|
|
|
|
return m, err
|
2015-12-16 17:13:50 -05:00
|
|
|
}
|
|
|
|
|
2016-03-09 16:23:04 -05:00
|
|
|
// Unmount decrements the activity count and unmounts the underlying layer
|
|
|
|
// Callers should only call `Unmount` once per call to `Mount`, even on error.
|
2015-12-16 17:13:50 -05:00
|
|
|
func (rl *referencedRWLayer) Unmount() error {
|
|
|
|
rl.activityL.Lock()
|
|
|
|
defer rl.activityL.Unlock()
|
|
|
|
|
|
|
|
if rl.activityCount == 0 {
|
|
|
|
return ErrNotMounted
|
|
|
|
}
|
|
|
|
if rl.activityCount == -1 {
|
|
|
|
return ErrLayerNotRetained
|
|
|
|
}
|
2016-03-09 16:23:04 -05:00
|
|
|
|
2015-12-16 17:13:50 -05:00
|
|
|
rl.activityCount--
|
2016-03-09 16:23:04 -05:00
|
|
|
if rl.activityCount > 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2015-12-16 17:13:50 -05:00
|
|
|
|
|
|
|
return rl.mountedLayer.Unmount()
|
|
|
|
}
|