1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Remove overlay pathCache

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2016-05-02 16:36:20 -07:00
parent 009ee16bef
commit 290be017c5

View file

@ -9,7 +9,6 @@ import (
"os" "os"
"os/exec" "os/exec"
"path" "path"
"sync"
"syscall" "syscall"
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
@ -93,8 +92,6 @@ func (d *naiveDiffDriverWithApply) ApplyDiff(id, parent string, diff archive.Rea
// Driver contains information about the home directory and the list of active mounts that are created using this driver. // Driver contains information about the home directory and the list of active mounts that are created using this driver.
type Driver struct { type Driver struct {
home string home string
pathCacheLock sync.Mutex
pathCache map[string]string
uidMaps []idtools.IDMap uidMaps []idtools.IDMap
gidMaps []idtools.IDMap gidMaps []idtools.IDMap
ctr *graphdriver.RefCounter ctr *graphdriver.RefCounter
@ -142,7 +139,6 @@ func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (grap
d := &Driver{ d := &Driver{
home: home, home: home,
pathCache: make(map[string]string),
uidMaps: uidMaps, uidMaps: uidMaps,
gidMaps: gidMaps, gidMaps: gidMaps,
ctr: graphdriver.NewRefCounter(), ctr: graphdriver.NewRefCounter(),
@ -328,14 +324,11 @@ func (d *Driver) Remove(id string) error {
if err := os.RemoveAll(d.dir(id)); err != nil && !os.IsNotExist(err) { if err := os.RemoveAll(d.dir(id)); err != nil && !os.IsNotExist(err) {
return err return err
} }
d.pathCacheLock.Lock()
delete(d.pathCache, id)
d.pathCacheLock.Unlock()
return nil return nil
} }
// Get creates and mounts the required file system for the given id and returns the mount path. // 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) { func (d *Driver) Get(id string, mountLabel string) (s string, err error) {
dir := d.dir(id) dir := d.dir(id)
if _, err := os.Stat(dir); err != nil { if _, err := os.Stat(dir); err != nil {
return "", err return "", err
@ -344,13 +337,16 @@ func (d *Driver) Get(id string, mountLabel string) (string, error) {
if count := d.ctr.Increment(mergedDir); count > 1 { if count := d.ctr.Increment(mergedDir); count > 1 {
return mergedDir, nil return mergedDir, nil
} }
defer func() {
if err != nil {
d.ctr.Decrement(mergedDir)
syscall.Unmount(mergedDir, 0)
}
}()
// If id has a root, just return it // If id has a root, just return it
rootDir := path.Join(dir, "root") rootDir := path.Join(dir, "root")
if _, err := os.Stat(rootDir); err == nil { if _, err := os.Stat(rootDir); err == nil {
d.pathCacheLock.Lock()
d.pathCache[id] = rootDir
d.pathCacheLock.Unlock()
return rootDir, nil return rootDir, nil
} }
@ -358,35 +354,24 @@ func (d *Driver) Get(id string, mountLabel string) (string, error) {
if err != nil { if err != nil {
return "", err return "", err
} }
lowerDir := path.Join(d.dir(string(lowerID)), "root") var (
upperDir := path.Join(dir, "upper") lowerDir = path.Join(d.dir(string(lowerID)), "root")
workDir := path.Join(dir, "work") upperDir = path.Join(dir, "upper")
workDir = path.Join(dir, "work")
opts := fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", lowerDir, upperDir, workDir) opts = fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", lowerDir, upperDir, workDir)
)
if err := syscall.Mount("overlay", mergedDir, "overlay", 0, label.FormatMountLabel(opts, mountLabel)); err != nil { if err := syscall.Mount("overlay", mergedDir, "overlay", 0, label.FormatMountLabel(opts, mountLabel)); err != nil {
d.ctr.Decrement(mergedDir)
return "", fmt.Errorf("error creating overlay mount to %s: %v", mergedDir, err) return "", fmt.Errorf("error creating overlay mount to %s: %v", mergedDir, err)
} }
// chown "workdir/work" to the remapped root UID/GID. Overlay fs inside a // chown "workdir/work" to the remapped root UID/GID. Overlay fs inside a
// user namespace requires this to move a directory from lower to upper. // user namespace requires this to move a directory from lower to upper.
rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps) rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
if err != nil { if err != nil {
d.ctr.Decrement(mergedDir)
syscall.Unmount(mergedDir, 0)
return "", err return "", err
} }
if err := os.Chown(path.Join(workDir, "work"), rootUID, rootGID); err != nil { if err := os.Chown(path.Join(workDir, "work"), rootUID, rootGID); err != nil {
d.ctr.Decrement(mergedDir)
syscall.Unmount(mergedDir, 0)
return "", err return "", err
} }
d.pathCacheLock.Lock()
d.pathCache[id] = mergedDir
d.pathCacheLock.Unlock()
return mergedDir, nil return mergedDir, nil
} }
@ -396,32 +381,13 @@ func (d *Driver) mounted(dir string) (bool, error) {
// Put unmounts the mount path created for the give id. // Put unmounts the mount path created for the give id.
func (d *Driver) Put(id string) error { func (d *Driver) Put(id string) error {
d.pathCacheLock.Lock() mountpoint := path.Join(d.dir(id), "merged")
mountpoint, exists := d.pathCache[id]
d.pathCacheLock.Unlock()
if count := d.ctr.Decrement(mountpoint); count > 0 { if count := d.ctr.Decrement(mountpoint); count > 0 {
return nil return nil
} }
if err := syscall.Unmount(mountpoint, 0); err != nil {
if !exists {
logrus.Debugf("Put on a non-mounted device %s", id)
// but it might be still here
if d.Exists(id) {
mountpoint = path.Join(d.dir(id), "merged")
}
d.pathCacheLock.Lock()
d.pathCache[id] = mountpoint
d.pathCacheLock.Unlock()
}
if mounted, err := d.mounted(mountpoint); mounted || err != nil {
if err = syscall.Unmount(mountpoint, 0); err != nil {
logrus.Debugf("Failed to unmount %s overlay: %v", id, err) logrus.Debugf("Failed to unmount %s overlay: %v", id, err)
} }
return err
}
return nil return nil
} }