From 8facb73a8fa544a2324f70d406c773cc5f0dae86 Mon Sep 17 00:00:00 2001
From: Tonis Tiigi <tonistiigi@gmail.com>
Date: Sat, 26 Mar 2016 22:53:47 -0700
Subject: [PATCH] Protect aufs mounts with locks

Parallel aufs mount calls produce invalid argument error.

Fixes #21545

Signed-off-by: Tonis Tiigi <tonistiigi@gmail.com>
(cherry picked from commit 824c24e6802ad3ed7e26b4f16e5ae81869b98185)
---
 daemon/graphdriver/aufs/aufs.go | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/daemon/graphdriver/aufs/aufs.go b/daemon/graphdriver/aufs/aufs.go
index ec9454e72a..c98e83770c 100644
--- a/daemon/graphdriver/aufs/aufs.go
+++ b/daemon/graphdriver/aufs/aufs.go
@@ -67,6 +67,7 @@ func init() {
 
 // Driver contains information about the filesystem mounted.
 type Driver struct {
+	sync.Mutex
 	root          string
 	uidMaps       []idtools.IDMap
 	gidMaps       []idtools.IDMap
@@ -418,6 +419,9 @@ func (a *Driver) getParentLayerPaths(id string) ([]string, error) {
 }
 
 func (a *Driver) mount(id string, target string, mountLabel string, layers []string) error {
+	a.Lock()
+	defer a.Unlock()
+
 	// If the id is mounted or we get an error return
 	if mounted, err := a.mounted(target); err != nil || mounted {
 		return err
@@ -432,6 +436,9 @@ func (a *Driver) mount(id string, target string, mountLabel string, layers []str
 }
 
 func (a *Driver) unmount(mountPath string) error {
+	a.Lock()
+	defer a.Unlock()
+
 	if mounted, err := a.mounted(mountPath); err != nil || !mounted {
 		return err
 	}