Wider volume mutation lock, move force adoptable.

We eventually should move back to per-disk locks, but use a giant
lock to keep development rolling forward.  Also move force adoptable
flag to framework since, since encrypted devices don't have persisted
properties loaded early during boot.

Bug: 19993667
Change-Id: Ifa3016ef41b038f8f71fc30bc81596cfd21dcd2a
diff --git a/CommandListener.cpp b/CommandListener.cpp
index fddee01..f47fb6a 100644
--- a/CommandListener.cpp
+++ b/CommandListener.cpp
@@ -138,6 +138,7 @@
     }
 
     VolumeManager *vm = VolumeManager::Instance();
+    std::lock_guard<std::mutex> lock(vm->getLock());
 
     // TODO: tease out methods not directly related to volumes
 
diff --git a/Disk.cpp b/Disk.cpp
index d6b748b..218c251 100644
--- a/Disk.cpp
+++ b/Disk.cpp
@@ -242,8 +242,6 @@
 }
 
 status_t Disk::readPartitions() {
-    std::lock_guard<std::mutex> lock(mLock);
-
     int8_t maxMinors = getMaxMinors();
     if (maxMinors < 0) {
         return -ENOTSUP;
@@ -337,8 +335,6 @@
 }
 
 status_t Disk::partitionPublic() {
-    std::lock_guard<std::mutex> lock(mLock);
-
     // TODO: improve this code
     destroyAllVolumes();
     mJustPartitioned = true;
@@ -385,8 +381,6 @@
 }
 
 status_t Disk::partitionMixed(int8_t ratio) {
-    std::lock_guard<std::mutex> lock(mLock);
-
     int res;
 
     destroyAllVolumes();
diff --git a/Disk.h b/Disk.h
index 5ba6bae..a8461fb 100644
--- a/Disk.h
+++ b/Disk.h
@@ -21,7 +21,6 @@
 
 #include <utils/Errors.h>
 
-#include <mutex>
 #include <vector>
 
 namespace android {
@@ -101,8 +100,6 @@
     int mFlags;
     /* Flag indicating object is created */
     bool mCreated;
-    /* Lock held while partitioning */
-    std::mutex mLock;
     /* Flag that we just partitioned and should format all volumes */
     bool mJustPartitioned;
 
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index e05da4d..cd1017c 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -257,6 +257,7 @@
 
     // Assume that we always have an emulated volume on internal
     // storage; the framework will decide if it should be mounted.
+    CHECK(mInternalEmulated == nullptr);
     mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
             new android::vold::EmulatedVolume("/data/media"));
     mInternalEmulated->create();
@@ -265,12 +266,15 @@
 }
 
 int VolumeManager::stop() {
+    CHECK(mInternalEmulated != nullptr);
     mInternalEmulated->destroy();
     mInternalEmulated = nullptr;
     return 0;
 }
 
 void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
+    std::lock_guard<std::mutex> lock(mLock);
+
     if (mDebug) {
         LOG(VERBOSE) << "----------------";
         LOG(VERBOSE) << "handleBlockEvent with action " << (int) evt->getAction();
@@ -433,6 +437,8 @@
 }
 
 int VolumeManager::unmountAll() {
+    std::lock_guard<std::mutex> lock(mLock);
+
     // First, try gracefully unmounting all known devices
     if (mInternalEmulated != nullptr) {
         mInternalEmulated->unmount();
diff --git a/VolumeManager.h b/VolumeManager.h
index 44920e0..e0b0ac8 100644
--- a/VolumeManager.h
+++ b/VolumeManager.h
@@ -23,8 +23,9 @@
 
 #ifdef __cplusplus
 
-#include <string>
 #include <list>
+#include <mutex>
+#include <string>
 
 #include <cutils/multiuser.h>
 #include <utils/List.h>
@@ -82,6 +83,9 @@
 public:
     virtual ~VolumeManager();
 
+    // TODO: pipe all requests through VM to avoid exposing this lock
+    std::mutex& getLock() { return mLock; }
+
     int start();
     int stop();
 
@@ -186,6 +190,8 @@
 
     int linkPrimary(userid_t userId);
 
+    std::mutex mLock;
+
     std::list<std::shared_ptr<DiskSource>> mDiskSources;
     std::list<std::shared_ptr<android::vold::Disk>> mDisks;
 
diff --git a/main.cpp b/main.cpp
index d159230..64cd0ee 100644
--- a/main.cpp
+++ b/main.cpp
@@ -42,8 +42,6 @@
 static void coldboot(const char *path);
 static void parse_args(int argc, char** argv);
 
-//#define DEBUG_FSTAB "/data/local/tmp/fstab.debug"
-
 struct fstab *fstab;
 
 struct selabel_handle *sehandle;
@@ -228,12 +226,6 @@
                 flags |= android::vold::Disk::Flags::kDefaultPrimary;
             }
 
-            if (property_get_bool("vold.force_adoptable", false)
-                    || property_get_bool("persist.vold.force_adoptable", false)) {
-                LOG(DEBUG) << "Forcing " << sysPattern << " to be adoptable";
-                flags |= android::vold::Disk::Flags::kAdoptable;
-            }
-
             vm->addDiskSource(std::shared_ptr<VolumeManager::DiskSource>(
                     new VolumeManager::DiskSource(sysPattern, nickname, flags)));
         }