mounted_layer.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package layer
  2. import (
  3. "io"
  4. "sync"
  5. "github.com/docker/docker/pkg/archive"
  6. )
  7. type mountedLayer struct {
  8. name string
  9. mountID string
  10. initID string
  11. parent *roLayer
  12. layerStore *layerStore
  13. references map[RWLayer]*referencedRWLayer
  14. }
  15. func (ml *mountedLayer) cacheParent() string {
  16. if ml.initID != "" {
  17. return ml.initID
  18. }
  19. if ml.parent != nil {
  20. return ml.parent.cacheID
  21. }
  22. return ""
  23. }
  24. func (ml *mountedLayer) TarStream() (io.ReadCloser, error) {
  25. archiver, err := ml.layerStore.driver.Diff(ml.mountID, ml.cacheParent())
  26. if err != nil {
  27. return nil, err
  28. }
  29. return archiver, nil
  30. }
  31. func (ml *mountedLayer) Name() string {
  32. return ml.name
  33. }
  34. func (ml *mountedLayer) Parent() Layer {
  35. if ml.parent != nil {
  36. return ml.parent
  37. }
  38. // Return a nil interface instead of an interface wrapping a nil
  39. // pointer.
  40. return nil
  41. }
  42. func (ml *mountedLayer) Mount(mountLabel string) (string, error) {
  43. return ml.layerStore.driver.Get(ml.mountID, mountLabel)
  44. }
  45. func (ml *mountedLayer) Unmount() error {
  46. return ml.layerStore.driver.Put(ml.mountID)
  47. }
  48. func (ml *mountedLayer) Size() (int64, error) {
  49. return ml.layerStore.driver.DiffSize(ml.mountID, ml.cacheParent())
  50. }
  51. func (ml *mountedLayer) Changes() ([]archive.Change, error) {
  52. return ml.layerStore.driver.Changes(ml.mountID, ml.cacheParent())
  53. }
  54. func (ml *mountedLayer) Metadata() (map[string]string, error) {
  55. return ml.layerStore.driver.GetMetadata(ml.mountID)
  56. }
  57. func (ml *mountedLayer) getReference() RWLayer {
  58. ref := &referencedRWLayer{
  59. mountedLayer: ml,
  60. }
  61. ml.references[ref] = ref
  62. return ref
  63. }
  64. func (ml *mountedLayer) hasReferences() bool {
  65. return len(ml.references) > 0
  66. }
  67. func (ml *mountedLayer) deleteReference(ref RWLayer) error {
  68. rl, ok := ml.references[ref]
  69. if !ok {
  70. return ErrLayerNotRetained
  71. }
  72. if err := rl.release(); err != nil {
  73. return err
  74. }
  75. delete(ml.references, ref)
  76. return nil
  77. }
  78. func (ml *mountedLayer) retakeReference(r RWLayer) {
  79. if ref, ok := r.(*referencedRWLayer); ok {
  80. ref.activityCount = 0
  81. ml.references[ref] = ref
  82. }
  83. }
  84. type referencedRWLayer struct {
  85. *mountedLayer
  86. activityL sync.Mutex
  87. activityCount int
  88. }
  89. func (rl *referencedRWLayer) release() error {
  90. rl.activityL.Lock()
  91. defer rl.activityL.Unlock()
  92. if rl.activityCount > 0 {
  93. return ErrActiveMount
  94. }
  95. rl.activityCount = -1
  96. return nil
  97. }
  98. func (rl *referencedRWLayer) Mount(mountLabel string) (string, error) {
  99. rl.activityL.Lock()
  100. defer rl.activityL.Unlock()
  101. if rl.activityCount == -1 {
  102. return "", ErrLayerNotRetained
  103. }
  104. rl.activityCount++
  105. return rl.mountedLayer.Mount(mountLabel)
  106. }
  107. func (rl *referencedRWLayer) Unmount() error {
  108. rl.activityL.Lock()
  109. defer rl.activityL.Unlock()
  110. if rl.activityCount == 0 {
  111. return ErrNotMounted
  112. }
  113. if rl.activityCount == -1 {
  114. return ErrLayerNotRetained
  115. }
  116. rl.activityCount--
  117. return rl.mountedLayer.Unmount()
  118. }