Support for private (adopted) volumes.

This adds support for private volumes which is just a filesystem
wrapped in a dm-crypt layer.  For now we're using the exact same
configuration as internal encryption (aes-cbc-essiv:sha256), but we
don't store any key material on the removable media.  Instead, we
store the key on internal storage, and use the GPT partition GUID
to identify which key should be used.

This means that private external storage is effectively as secure as
the internal storage of the device.  That is, if the internal storage
is encrypted, then our external storage key is also encrypted.

When partitioning disks, we now support a "private" mode which has
a PrivateVolume partition, and a currently unused 16MB metadata
partition reserved for future use.  It also supports a "mixed" mode
which creates both a PublicVolume and PrivateVolume on the same
disk.  Mixed mode is currently experimental.

For now, just add ext4 support to PrivateVolume; we'll look at f2fs
in a future change.  Add VolumeBase lifecycle for setting up crypto
mappings, and extract blkid logic into shared method.  Sprinkle some
more "static" around the cryptfs code to improve invariants.

Bug: 19993667
Change-Id: Ibd1df6250735b706959a1eb9d9f7219ea85912a0
diff --git a/PublicVolume.cpp b/PublicVolume.cpp
index e4ed9c1..f3bd905 100644
--- a/PublicVolume.cpp
+++ b/PublicVolume.cpp
@@ -37,7 +37,6 @@
 namespace android {
 namespace vold {
 
-static const char* kBlkidPath = "/system/bin/blkid";
 static const char* kFusePath = "/system/bin/sdcard";
 
 static const char* kAsecPath = "/mnt/secure/asec";
@@ -46,51 +45,13 @@
         VolumeBase(Type::kPublic), mDevice(device), mFusePid(0) {
     setId(StringPrintf("public:%u,%u", major(device), minor(device)));
     mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
-    CreateDeviceNode(mDevPath, mDevice);
 }
 
 PublicVolume::~PublicVolume() {
-    DestroyDeviceNode(mDevPath);
 }
 
 status_t PublicVolume::readMetadata() {
-    mFsType.clear();
-    mFsUuid.clear();
-    mFsLabel.clear();
-
-    std::string path(StringPrintf("%s -c /dev/null %s", kBlkidPath, mDevPath.c_str()));
-    FILE* fp = popen(path.c_str(), "r");
-    if (!fp) {
-        PLOG(ERROR) << "Failed to run " << path;
-        return -errno;
-    }
-
-    status_t res = OK;
-    char line[1024];
-    char value[128];
-    if (fgets(line, sizeof(line), fp) != nullptr) {
-        LOG(DEBUG) << "blkid identified as " << line;
-
-        char* start = strstr(line, "TYPE=");
-        if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
-            mFsType = value;
-        }
-
-        start = strstr(line, "UUID=");
-        if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
-            mFsUuid = value;
-        }
-
-        start = strstr(line, "LABEL=");
-        if (start != nullptr && sscanf(start + 6, "\"%127[^\"]\"", value) == 1) {
-            mFsLabel = value;
-        }
-    } else {
-        LOG(WARNING) << "blkid failed to identify " << mDevPath;
-        res = -ENODATA;
-    }
-
-    pclose(fp);
+    status_t res = ReadMetadata(mDevPath, mFsType, mFsUuid, mFsLabel);
 
     VolumeManager::Instance()->getBroadcaster()->sendBroadcast(
             ResponseCode::VolumeFsTypeChanged,
@@ -113,13 +74,13 @@
     if (!access(legacyPath.c_str(), R_OK | X_OK)
             && access(securePath.c_str(), R_OK | X_OK)) {
         if (rename(legacyPath.c_str(), securePath.c_str())) {
-            PLOG(WARNING) << "Failed to rename legacy ASEC dir";
+            PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
         }
     }
 
     if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
         if (errno != EEXIST) {
-            PLOG(WARNING) << "Creating ASEC stage failed";
+            PLOG(WARNING) << getId() << " creating ASEC stage failed";
             return -errno;
         }
     }
@@ -129,15 +90,23 @@
     return OK;
 }
 
+status_t PublicVolume::doCreate() {
+    return CreateDeviceNode(mDevPath, mDevice);
+}
+
+status_t PublicVolume::doDestroy() {
+    return DestroyDeviceNode(mDevPath);
+}
+
 status_t PublicVolume::doMount() {
     // TODO: expand to support mounting other filesystems
+    readMetadata();
+
     if (Fat::check(mDevPath.c_str())) {
-        LOG(ERROR) << "Failed filesystem check; not mounting";
+        LOG(ERROR) << getId() << " failed filesystem check";
         return -EIO;
     }
 
-    readMetadata();
-
     // Use UUID as stable name, if available
     std::string stableName = getId();
     if (!mFsUuid.empty()) {
@@ -149,17 +118,17 @@
     setPath(mFusePath);
 
     if (fs_prepare_dir(mRawPath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
-        PLOG(ERROR) << "Failed to create mount point " << mRawPath;
+        PLOG(ERROR) << getId() << " failed to create mount point " << mRawPath;
         return -errno;
     }
     if (fs_prepare_dir(mFusePath.c_str(), 0700, AID_ROOT, AID_ROOT)) {
-        PLOG(ERROR) << "Failed to create mount point " << mFusePath;
+        PLOG(ERROR) << getId() << " failed to create mount point " << mFusePath;
         return -errno;
     }
 
     if (Fat::doMount(mDevPath.c_str(), mRawPath.c_str(), false, false, false,
             AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) {
-        PLOG(ERROR) << "Failed to mount " << mDevPath;
+        PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
         return -EIO;
     }
 
@@ -202,7 +171,7 @@
     }
 
     if (mFusePid == -1) {
-        PLOG(ERROR) << "Failed to fork";
+        PLOG(ERROR) << getId() << " failed to fork";
         return -errno;
     }
 
@@ -220,10 +189,10 @@
     ForceUnmount(mRawPath);
 
     if (TEMP_FAILURE_RETRY(rmdir(mRawPath.c_str()))) {
-        PLOG(ERROR) << "Failed to rmdir mount point " << mRawPath;
+        PLOG(ERROR) << getId() << " failed to rmdir mount point " << mRawPath;
     }
     if (TEMP_FAILURE_RETRY(rmdir(mFusePath.c_str()))) {
-        PLOG(ERROR) << "Failed to rmdir mount point " << mFusePath;
+        PLOG(ERROR) << getId() << " failed to rmdir mount point " << mFusePath;
     }
 
     mFusePath.clear();
@@ -234,7 +203,7 @@
 
 status_t PublicVolume::doFormat() {
     if (Fat::format(mDevPath.c_str(), 0, true)) {
-        LOG(ERROR) << "Failed to format";
+        LOG(ERROR) << getId() << " failed to format";
         return -errno;
     }
     return OK;