diff --git a/layer/layer_store.go b/layer/layer_store.go index 55ec2262f5b3bb37f46279f00f48da8ca8e213a7..f163d558d49bebc621420be28e77a8e64e8c6ab9 100644 --- a/layer/layer_store.go +++ b/layer/layer_store.go @@ -269,7 +269,7 @@ func (ls *layerStore) Register(ts io.Reader, parent ChainID) (Layer, error) { ls.layerL.Lock() defer ls.layerL.Unlock() - if existingLayer := ls.getAndRetainLayer(layer.chainID); existingLayer != nil { + if existingLayer := ls.getWithoutLock(layer.chainID); existingLayer != nil { // Set error for cleanup, but do not return the error err = errors.New("layer already exists") return existingLayer.getReference(), nil @@ -284,18 +284,21 @@ func (ls *layerStore) Register(ts io.Reader, parent ChainID) (Layer, error) { return layer.getReference(), nil } -func (ls *layerStore) get(l ChainID) *roLayer { - ls.layerL.Lock() - defer ls.layerL.Unlock() - - layer, ok := ls.layerMap[l] +func (ls *layerStore) getWithoutLock(layer ChainID) *roLayer { + l, ok := ls.layerMap[layer] if !ok { return nil } - layer.referenceCount++ + l.referenceCount++ + + return l +} - return layer +func (ls *layerStore) get(l ChainID) *roLayer { + ls.layerL.Lock() + defer ls.layerL.Unlock() + return ls.getWithoutLock(l) } func (ls *layerStore) Get(l ChainID) (Layer, error) { @@ -415,17 +418,6 @@ func (ls *layerStore) saveMount(mount *mountedLayer) error { return nil } -func (ls *layerStore) getAndRetainLayer(layer ChainID) *roLayer { - l, ok := ls.layerMap[layer] - if !ok { - return nil - } - - l.referenceCount++ - - return l -} - func (ls *layerStore) initMount(graphID, parent, mountLabel string, initFunc MountInit) (string, error) { // Use "-init" to maintain compatibility with graph drivers // which are expecting this layer with this special name. If all @@ -468,9 +460,7 @@ func (ls *layerStore) Mount(name string, parent ChainID, mountLabel string, init var pid string var p *roLayer if string(parent) != "" { - ls.layerL.Lock() - p = ls.getAndRetainLayer(parent) - ls.layerL.Unlock() + p = ls.get(parent) if p == nil { return nil, ErrLayerDoesNotExist } diff --git a/layer/layer_windows.go b/layer/layer_windows.go index d2e91b7980a98ed63ec1ef8993aba92919b1d755..27799959272708d545fe030dae71078a38f0aa33 100644 --- a/layer/layer_windows.go +++ b/layer/layer_windows.go @@ -93,7 +93,7 @@ func (ls *layerStore) RegisterDiffID(graphID string, size int64) (Layer, error) ls.layerL.Lock() defer ls.layerL.Unlock() - if existingLayer := ls.getAndRetainLayer(layer.chainID); existingLayer != nil { + if existingLayer := ls.getWithoutLock(layer.chainID); existingLayer != nil { // Set error for cleanup, but do not return err = errors.New("layer already exists") return existingLayer.getReference(), nil diff --git a/layer/migration.go b/layer/migration.go index db25ed9e943adb2c04ef0666ca913bdbd795e07a..bc448a59a50d649e3467a615f50b3a1fdd41dfb2 100644 --- a/layer/migration.go +++ b/layer/migration.go @@ -35,9 +35,7 @@ func (ls *layerStore) MountByGraphID(name string, graphID string, parent ChainID var p *roLayer if string(parent) != "" { - ls.layerL.Lock() - p = ls.getAndRetainLayer(parent) - ls.layerL.Unlock() + p = ls.get(parent) if p == nil { return nil, ErrLayerDoesNotExist } @@ -209,7 +207,7 @@ func (ls *layerStore) RegisterByGraphID(graphID string, parent ChainID, tarDataF ls.layerL.Lock() defer ls.layerL.Unlock() - if existingLayer := ls.getAndRetainLayer(layer.chainID); existingLayer != nil { + if existingLayer := ls.getWithoutLock(layer.chainID); existingLayer != nil { // Set error for cleanup, but do not return err = errors.New("layer already exists") return existingLayer.getReference(), nil