diff --git a/daemon/graphdriver/overlay/copy.go b/daemon/graphdriver/overlay/copy.go index f43b117af4..835737dc79 100644 --- a/daemon/graphdriver/overlay/copy.go +++ b/daemon/graphdriver/overlay/copy.go @@ -12,10 +12,10 @@ import ( "github.com/docker/docker/pkg/system" ) -type CopyFlags int +type copyFlags int const ( - CopyHardlink CopyFlags = 1 << iota + copyHardlink copyFlags = 1 << iota ) func copyRegular(srcPath, dstPath string, mode os.FileMode) error { @@ -49,7 +49,7 @@ func copyXattr(srcPath, dstPath, attr string) error { return nil } -func copyDir(srcDir, dstDir string, flags CopyFlags) error { +func copyDir(srcDir, dstDir string, flags copyFlags) error { err := filepath.Walk(srcDir, func(srcPath string, f os.FileInfo, err error) error { if err != nil { return err @@ -75,7 +75,7 @@ func copyDir(srcDir, dstDir string, flags CopyFlags) error { switch f.Mode() & os.ModeType { case 0: // Regular file - if flags&CopyHardlink != 0 { + if flags©Hardlink != 0 { isHardlink = true if err := os.Link(srcPath, dstPath); err != nil { return err diff --git a/daemon/graphdriver/overlay/overlay.go b/daemon/graphdriver/overlay/overlay.go index bd1657e5e5..b96e941d69 100644 --- a/daemon/graphdriver/overlay/overlay.go +++ b/daemon/graphdriver/overlay/overlay.go @@ -23,11 +23,15 @@ import ( // implementation of ApplyDiff() var ( + // ErrApplyDiffFallback is returned to indicate that a normal ApplyDiff is applied as a fallback from Naive diff writer. ErrApplyDiffFallback = fmt.Errorf("Fall back to normal ApplyDiff") ) +// ApplyDiffProtoDriver wraps the ProtoDriver by extending the inteface with ApplyDiff method. type ApplyDiffProtoDriver interface { graphdriver.ProtoDriver + // ApplyDiff writes the diff to the archive for the given id and parent id. + // It returns the size in bytes written if successful, an error ErrApplyDiffFallback is returned otherwise. ApplyDiff(id, parent string, diff archive.Reader) (size int64, err error) } @@ -36,6 +40,7 @@ type naiveDiffDriverWithApply struct { applyDiff ApplyDiffProtoDriver } +// NaiveDiffDriverWithApply returns a NaiveDiff driver with custom ApplyDiff. func NaiveDiffDriverWithApply(driver ApplyDiffProtoDriver) graphdriver.Driver { return &naiveDiffDriverWithApply{ Driver: graphdriver.NaiveDiffDriver(driver), @@ -43,6 +48,7 @@ func NaiveDiffDriverWithApply(driver ApplyDiffProtoDriver) graphdriver.Driver { } } +// ApplyDiff creates a diff layer with either the NaiveDiffDriver or with a fallback. func (d *naiveDiffDriverWithApply) ApplyDiff(id, parent string, diff archive.Reader) (int64, error) { b, err := d.applyDiff.ApplyDiff(id, parent, diff) if err == ErrApplyDiffFallback { @@ -79,11 +85,15 @@ func (d *naiveDiffDriverWithApply) ApplyDiff(id, parent string, diff archive.Rea // of that. This means all child images share file (but not directory) // data with the parent. +// ActiveMount contains information about the count, path and whether is mounted or not. +// This information is part of the Driver, that contains list of active mounts taht are part of this overlay. type ActiveMount struct { count int path string mounted bool } + +// Driver contains information about the home directory and the list of active mounts that are created using this driver. type Driver struct { home string sync.Mutex // Protects concurrent modification to active @@ -96,6 +106,9 @@ func init() { graphdriver.Register("overlay", Init) } +// Init returns the NaiveDiffDriver, a native diff driver for overlay filesystem. +// If overlay filesystem is not supported on the host, graphdriver.ErrNotSupported is returned as error. +// If a overlay filesystem is not supported over a existing filesystem then error graphdriver.ErrIncompatibleFS is returned. func Init(home string, options []string) (graphdriver.Driver, error) { if err := supportsOverlay(); err != nil { @@ -161,12 +174,15 @@ func (d *Driver) String() string { return "overlay" } +// Status returns current driver information in a two dimensional string array. +// Output contains "Backing Filesystem" used in this implementation. func (d *Driver) Status() [][2]string { return [][2]string{ {"Backing Filesystem", backingFs}, } } +// GetMetadata returns meta data about the overlay driver such as root, LowerDir, UpperDir, WorkDir and MergeDir used to store data. func (d *Driver) GetMetadata(id string) (map[string]string, error) { dir := d.dir(id) if _, err := os.Stat(dir); err != nil { @@ -182,12 +198,12 @@ func (d *Driver) GetMetadata(id string) (map[string]string, error) { return metadata, nil } - lowerId, err := ioutil.ReadFile(path.Join(dir, "lower-id")) + lowerID, err := ioutil.ReadFile(path.Join(dir, "lower-id")) if err != nil { return nil, err } - metadata["LowerDir"] = path.Join(d.dir(string(lowerId)), "root") + metadata["LowerDir"] = path.Join(d.dir(string(lowerID)), "root") metadata["UpperDir"] = path.Join(dir, "upper") metadata["WorkDir"] = path.Join(dir, "work") metadata["MergedDir"] = path.Join(dir, "merged") @@ -195,10 +211,14 @@ func (d *Driver) GetMetadata(id string) (map[string]string, error) { return metadata, nil } +// Cleanup simply returns nil and do not change the existing filesystem. +// This is required to satisfy the graphdriver.Driver interface. func (d *Driver) Cleanup() error { return nil } +// Create is used to create the upper, lower, and merge directories required for overlay fs for a given id. +// The parent filesystem is used to configure these directories for the overlay. func (d *Driver) Create(id string, parent string) (retErr error) { dir := d.dir(id) if err := os.MkdirAll(path.Dir(dir), 0700); err != nil { @@ -251,12 +271,12 @@ func (d *Driver) Create(id string, parent string) (retErr error) { // Otherwise, copy the upper and the lower-id from the parent - lowerId, err := ioutil.ReadFile(path.Join(parentDir, "lower-id")) + lowerID, err := ioutil.ReadFile(path.Join(parentDir, "lower-id")) if err != nil { return err } - if err := ioutil.WriteFile(path.Join(dir, "lower-id"), lowerId, 0666); err != nil { + if err := ioutil.WriteFile(path.Join(dir, "lower-id"), lowerID, 0666); err != nil { return err } @@ -284,6 +304,7 @@ func (d *Driver) dir(id string) string { return path.Join(d.home, id) } +// Remove cleans the directories that are created for this id. func (d *Driver) Remove(id string) error { dir := d.dir(id) if _, err := os.Stat(dir); err != nil { @@ -292,6 +313,7 @@ func (d *Driver) Remove(id string) error { return os.RemoveAll(dir) } +// Get creates and mounts the required file system for the given id and returns the mount path. func (d *Driver) Get(id string, mountLabel string) (string, error) { // Protect the d.active from concurrent access d.Lock() @@ -318,11 +340,11 @@ func (d *Driver) Get(id string, mountLabel string) (string, error) { return mount.path, nil } - lowerId, err := ioutil.ReadFile(path.Join(dir, "lower-id")) + lowerID, err := ioutil.ReadFile(path.Join(dir, "lower-id")) if err != nil { return "", err } - lowerDir := path.Join(d.dir(string(lowerId)), "root") + lowerDir := path.Join(d.dir(string(lowerID)), "root") upperDir := path.Join(dir, "upper") workDir := path.Join(dir, "work") mergedDir := path.Join(dir, "merged") @@ -338,6 +360,7 @@ func (d *Driver) Get(id string, mountLabel string) (string, error) { return mount.path, nil } +// Put unmounts the mount path created for the give id. func (d *Driver) Put(id string) error { // Protect the d.active from concurrent access d.Lock() @@ -373,6 +396,7 @@ func (d *Driver) Put(id string) error { return nil } +// ApplyDiff applies the new layer on top of the root, if parent does not exist with will return a ErrApplyDiffFallback error. func (d *Driver) ApplyDiff(id string, parent string, diff archive.Reader) (size int64, err error) { dir := d.dir(id) @@ -407,7 +431,7 @@ func (d *Driver) ApplyDiff(id string, parent string, diff archive.Reader) (size } }() - if err = copyDir(parentRootDir, tmpRootDir, CopyHardlink); err != nil { + if err = copyDir(parentRootDir, tmpRootDir, copyHardlink); err != nil { return 0, err } @@ -423,6 +447,7 @@ func (d *Driver) ApplyDiff(id string, parent string, diff archive.Reader) (size return } +// Exists checks to see if the id is already mounted. func (d *Driver) Exists(id string) bool { _, err := os.Stat(d.dir(id)) return err == nil diff --git a/hack/make/validate-lint b/hack/make/validate-lint index e362f62afc..bf03f7055b 100644 --- a/hack/make/validate-lint +++ b/hack/make/validate-lint @@ -27,6 +27,7 @@ packages=( daemon/execdriver/windows daemon/graphdriver/aufs daemon/graphdriver/devmapper + daemon/graphdriver/overlay daemon/graphdriver/vfs daemon/graphdriver/zfs daemon/logger