Fix deadlock between vold and init
am: e74c3ea1e2

Change-Id: Ie6f7dc8ad70d76275d156ab76dab5806c197fc1b
diff --git a/Android.mk b/Android.mk
index cabf9b3..1b38393 100644
--- a/Android.mk
+++ b/Android.mk
@@ -33,7 +33,6 @@
 	secontext.cpp \
 
 common_c_includes := \
-	system/extras/ext4_utils \
 	system/extras/f2fs_utils \
 	external/scrypt/lib/crypto \
 	frameworks/native/include \
@@ -51,6 +50,7 @@
 	liblogwrap \
 	libext4_utils \
 	libf2fs_sparseblock \
+	libcrypto_utils \
 	libcrypto \
 	libselinux \
 	libutils \
@@ -66,12 +66,22 @@
 	libfec_rs \
 	libsquashfs_utils \
 	libscrypt_static \
-	libmincrypt \
 	libbatteryservice \
+	libavb \
 
 vold_conlyflags := -std=c11
 vold_cflags := -Werror -Wall -Wno-missing-field-initializers -Wno-unused-variable -Wno-unused-parameter
 
+required_modules :=
+ifeq ($(TARGET_USERIMAGES_USE_EXT4), true)
+  ifeq ($(TARGET_USES_MKE2FS), true)
+    vold_cflags += -DTARGET_USES_MKE2FS
+    required_modules += mke2fs
+  else
+    required_modules += make_ext4fs
+  endif
+endif
+
 include $(CLEAR_VARS)
 
 LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk
@@ -84,6 +94,7 @@
 LOCAL_MODULE_TAGS := eng tests
 LOCAL_CFLAGS := $(vold_cflags)
 LOCAL_CONLYFLAGS := $(vold_conlyflags)
+LOCAL_REQUIRED_MODULES := $(required_modules)
 
 include $(BUILD_STATIC_LIBRARY)
 
@@ -110,6 +121,7 @@
 
 LOCAL_SHARED_LIBRARIES := $(common_shared_libraries)
 LOCAL_STATIC_LIBRARIES := $(common_static_libraries)
+LOCAL_REQUIRED_MODULES := $(required_modules)
 
 include $(BUILD_EXECUTABLE)
 
diff --git a/CryptCommandListener.cpp b/CryptCommandListener.cpp
index 02c2701..e4a2d3a 100644
--- a/CryptCommandListener.cpp
+++ b/CryptCommandListener.cpp
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <assert.h>
 #include <stdlib.h>
 #include <sys/socket.h>
 #include <sys/stat.h>
diff --git a/Disk.cpp b/Disk.cpp
index 1e1a63e..6562d96 100644
--- a/Disk.cpp
+++ b/Disk.cpp
@@ -136,7 +136,7 @@
 }
 
 void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
-    for (auto vol : mVolumes) {
+    for (const auto& vol : mVolumes) {
         if (vol->getType() == type) {
             list.push_back(vol->getId());
         }
@@ -209,7 +209,7 @@
 }
 
 void Disk::destroyAllVolumes() {
-    for (auto vol : mVolumes) {
+    for (const auto& vol : mVolumes) {
         vol->destroy();
     }
     mVolumes.clear();
@@ -305,7 +305,7 @@
 
     Table table = Table::kUnknown;
     bool foundParts = false;
-    for (auto line : output) {
+    for (const auto& line : output) {
         char* cline = (char*) line.c_str();
         char* token = strtok(cline, kSgdiskToken);
         if (token == nullptr) continue;
@@ -370,7 +370,7 @@
 }
 
 status_t Disk::unmountAll() {
-    for (auto vol : mVolumes) {
+    for (const auto& vol : mVolumes) {
         vol->unmount();
     }
     return OK;
diff --git a/EmulatedVolume.h b/EmulatedVolume.h
index 09686c1..9b0c049 100644
--- a/EmulatedVolume.h
+++ b/EmulatedVolume.h
@@ -37,7 +37,7 @@
  */
 class EmulatedVolume : public VolumeBase {
 public:
-    EmulatedVolume(const std::string& rawPath);
+    explicit EmulatedVolume(const std::string& rawPath);
     EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid);
     virtual ~EmulatedVolume();
 
diff --git a/Ext4Crypt.cpp b/Ext4Crypt.cpp
index cb41295..0063bef 100644
--- a/Ext4Crypt.cpp
+++ b/Ext4Crypt.cpp
@@ -40,13 +40,13 @@
 #include <private/android_filesystem_config.h>
 
 #include "cryptfs.h"
-#include "ext4_crypt.h"
-#include "key_control.h"
 
 #define EMULATED_USES_SELINUX 0
 #define MANAGE_MISC_DIRS 0
 
 #include <cutils/fs.h>
+#include <ext4_utils/ext4_crypt.h>
+#include <ext4_utils/key_control.h>
 
 #include <android-base/file.h>
 #include <android-base/logging.h>
@@ -75,8 +75,7 @@
 // Map user ids to key references
 std::map<userid_t, std::string> s_de_key_raw_refs;
 std::map<userid_t, std::string> s_ce_key_raw_refs;
-// TODO abolish this map. Keys should not be long-lived in user memory, only kernel memory.
-// See b/26948053
+// TODO abolish this map, per b/26948053
 std::map<userid_t, std::string> s_ce_keys;
 
 // ext4enc:TODO get this const from somewhere good
diff --git a/MoveTask.cpp b/MoveTask.cpp
index a0522d2..0a60c4e 100644
--- a/MoveTask.cpp
+++ b/MoveTask.cpp
@@ -27,7 +27,7 @@
 #include <dirent.h>
 #include <sys/wait.h>
 
-#define CONSTRAIN(amount, low, high) (amount < low ? low : (amount > high ? high : amount))
+#define CONSTRAIN(amount, low, high) ((amount) < (low) ? (low) : ((amount) > (high) ? (high) : (amount)))
 
 using android::base::StringPrintf;
 
diff --git a/NetlinkHandler.h b/NetlinkHandler.h
index 00a31c8..56eb23c 100644
--- a/NetlinkHandler.h
+++ b/NetlinkHandler.h
@@ -22,7 +22,7 @@
 class NetlinkHandler: public NetlinkListener {
 
 public:
-    NetlinkHandler(int listenerSocket);
+    explicit NetlinkHandler(int listenerSocket);
     virtual ~NetlinkHandler();
 
     int start(void);
diff --git a/ScryptParameters.cpp b/ScryptParameters.cpp
index 669809b..c0e2030 100644
--- a/ScryptParameters.cpp
+++ b/ScryptParameters.cpp
@@ -20,7 +20,7 @@
 #include <string.h>
 
 bool parse_scrypt_parameters(const char* paramstr, int *Nf, int *rf, int *pf) {
-    int params[3];
+    int params[3] = {};
     char *token;
     char *saveptr;
     int i;
diff --git a/TrimTask.cpp b/TrimTask.cpp
index d7bfda7..6c141f2 100644
--- a/TrimTask.cpp
+++ b/TrimTask.cpp
@@ -53,7 +53,7 @@
     VolumeManager* vm = VolumeManager::Instance();
     std::list<std::string> privateIds;
     vm->listVolumes(VolumeBase::Type::kPrivate, privateIds);
-    for (auto id : privateIds) {
+    for (const auto& id : privateIds) {
         auto vol = vm->findVolume(id);
         if (vol != nullptr && vol->getState() == VolumeBase::State::kMounted) {
             mPaths.push_back(vol->getPath());
@@ -114,7 +114,7 @@
 void TrimTask::run() {
     acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
 
-    for (auto path : mPaths) {
+    for (const auto& path : mPaths) {
         LOG(DEBUG) << "Starting trim of " << path;
 
         int fd = open(path.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC | O_NOFOLLOW);
diff --git a/TrimTask.h b/TrimTask.h
index 57be802..2ade7b5 100644
--- a/TrimTask.h
+++ b/TrimTask.h
@@ -27,7 +27,7 @@
 
 class TrimTask {
 public:
-    TrimTask(int flags);
+    explicit TrimTask(int flags);
     virtual ~TrimTask();
 
     enum Flags {
diff --git a/Utils.cpp b/Utils.cpp
index 014055b..17e9ffd 100644
--- a/Utils.cpp
+++ b/Utils.cpp
@@ -208,10 +208,10 @@
     }
 
     char value[128];
-    for (auto line : output) {
+    for (const auto& line : output) {
         // Extract values from blkid output, if defined
         const char* cline = line.c_str();
-        char* start = strstr(cline, "TYPE=");
+        const char* start = strstr(cline, "TYPE=");
         if (start != nullptr && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) {
             fsType = value;
         }
diff --git a/VoldCommand.h b/VoldCommand.h
index 5ddc666..e435159 100644
--- a/VoldCommand.h
+++ b/VoldCommand.h
@@ -21,7 +21,7 @@
 
 class VoldCommand : public FrameworkCommand {
 public:
-    VoldCommand(const char *cmd);
+    explicit VoldCommand(const char *cmd);
     virtual ~VoldCommand() {}
 };
 
diff --git a/VolumeBase.cpp b/VolumeBase.cpp
index ea4d372..3f27d87 100644
--- a/VolumeBase.cpp
+++ b/VolumeBase.cpp
@@ -30,8 +30,6 @@
 
 using android::base::StringPrintf;
 
-#define DEBUG 1
-
 namespace android {
 namespace vold {
 
@@ -219,7 +217,7 @@
     }
 
     setState(State::kEjecting);
-    for (auto vol : mVolumes) {
+    for (const auto& vol : mVolumes) {
         if (vol->destroy()) {
             LOG(WARNING) << getId() << " failed to destroy " << vol->getId()
                     << " stacked above";
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index 5cc60a1..2085ca8 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -63,8 +63,8 @@
 
 #define MASS_STORAGE_FILE_PATH  "/sys/class/android_usb/android0/f_mass_storage/lun/file"
 
-#define ROUND_UP_POWER_OF_2(number, po2) (((!!(number & ((1U << po2) - 1))) << po2)\
-                                         + (number & (~((1U << po2) - 1))))
+#define ROUND_UP_POWER_OF_2(number, po2) (((!!((number) & ((1U << (po2)) - 1))) << (po2))\
+                                         + ((number) & (~((1U << (po2)) - 1))))
 
 using android::base::StringPrintf;
 
@@ -296,7 +296,7 @@
 
     switch (evt->getAction()) {
     case NetlinkEvent::Action::kAdd: {
-        for (auto source : mDiskSources) {
+        for (const auto& source : mDiskSources) {
             if (source->matches(eventPath)) {
                 // For now, assume that MMC and virtio-blk (the latter is
                 // emulator-specific; see Disk.cpp for details) devices are SD,
@@ -322,7 +322,7 @@
     }
     case NetlinkEvent::Action::kChange: {
         LOG(DEBUG) << "Disk at " << major << ":" << minor << " changed";
-        for (auto disk : mDisks) {
+        for (const auto& disk : mDisks) {
             if (disk->getDevice() == device) {
                 disk->readMetadata();
                 disk->readPartitions();
@@ -366,7 +366,7 @@
     if (mInternalEmulated->getId() == id) {
         return mInternalEmulated;
     }
-    for (auto disk : mDisks) {
+    for (const auto& disk : mDisks) {
         auto vol = disk->findVolume(id);
         if (vol != nullptr) {
             return vol;
@@ -378,7 +378,7 @@
 void VolumeManager::listVolumes(android::vold::VolumeBase::Type type,
         std::list<std::string>& list) {
     list.clear();
-    for (auto disk : mDisks) {
+    for (const auto& disk : mDisks) {
         disk->listVolumes(type, list);
     }
 }
@@ -497,7 +497,7 @@
     }
     endmntent(fp);
 
-    for (auto path : toUnmount) {
+    for (const auto& path : toUnmount) {
         if (umount2(path.c_str(), MNT_DETACH)) {
             ALOGW("Failed to unmount %s: %s", path.c_str(), strerror(errno));
         }
@@ -623,7 +623,7 @@
     // newly connected framework hears all events.
     mInternalEmulated->destroy();
     mInternalEmulated->create();
-    for (auto disk : mDisks) {
+    for (const auto& disk : mDisks) {
         disk->destroy();
         disk->create();
     }
@@ -634,7 +634,7 @@
 
 int VolumeManager::shutdown() {
     mInternalEmulated->destroy();
-    for (auto disk : mDisks) {
+    for (const auto& disk : mDisks) {
         disk->destroy();
     }
     mDisks.clear();
@@ -648,7 +648,7 @@
     if (mInternalEmulated != nullptr) {
         mInternalEmulated->unmount();
     }
-    for (auto disk : mDisks) {
+    for (const auto& disk : mDisks) {
         disk->unmountAll();
     }
 
@@ -672,7 +672,7 @@
     }
     endmntent(fp);
 
-    for (auto path : toUnmount) {
+    for (const auto& path : toUnmount) {
         SLOGW("Tearing down stale mount %s", path.c_str());
         android::vold::ForceUnmount(path);
     }
diff --git a/VolumeManager.h b/VolumeManager.h
index 39fc8f9..dd9f09d 100644
--- a/VolumeManager.h
+++ b/VolumeManager.h
@@ -217,7 +217,7 @@
 
 extern "C" {
 #endif /* __cplusplus */
-#define UNMOUNT_NOT_MOUNTED_ERR -2
+#define UNMOUNT_NOT_MOUNTED_ERR (-2)
     int vold_unmountAll(void);
 #ifdef __cplusplus
 }
diff --git a/cryptfs.c b/cryptfs.c
index 2a52336..f6698f6 100644
--- a/cryptfs.c
+++ b/cryptfs.c
@@ -38,7 +38,8 @@
 #include <openssl/evp.h>
 #include <openssl/sha.h>
 #include <errno.h>
-#include <ext4.h>
+#include <ext4_utils/ext4.h>
+#include <ext4_utils/ext4_utils.h>
 #include <linux/kdev_t.h>
 #include <fs_mgr.h>
 #include <time.h>
@@ -57,7 +58,6 @@
 #include "VoldUtil.h"
 #include "crypto_scrypt.h"
 #include "Ext4Crypt.h"
-#include "ext4_utils.h"
 #include "f2fs_sparseblock.h"
 #include "CheckBattery.h"
 #include "Process.h"
@@ -2185,6 +2185,19 @@
     int rc = -1;
 
     if (type == EXT4_FS) {
+#ifdef TARGET_USES_MKE2FS
+        args[0] = "/system/bin/mke2fs";
+        args[1] = "-M";
+        args[2] = "/data";
+        args[3] = "-b";
+        args[4] = "4096";
+        args[5] = "-t";
+        args[6] = "ext4";
+        args[7] = crypto_blkdev;
+        snprintf(size_str, sizeof(size_str), "%" PRId64, size / (4096 / 512));
+        args[8] = size_str;
+        num_args = 9;
+#else
         args[0] = "/system/bin/make_ext4fs";
         args[1] = "-a";
         args[2] = "/data";
@@ -2193,6 +2206,7 @@
         args[4] = size_str;
         args[5] = crypto_blkdev;
         num_args = 6;
+#endif
         SLOGI("Making empty filesystem with command %s %s %s %s %s %s\n",
               args[0], args[1], args[2], args[3], args[4], args[5]);
     } else if (type == F2FS_FS) {
diff --git a/cryptfs.h b/cryptfs.h
index 11d9bb7..bf158de 100644
--- a/cryptfs.h
+++ b/cryptfs.h
@@ -191,32 +191,32 @@
 /* Return values for cryptfs_crypto_complete */
 #define CRYPTO_COMPLETE_NOT_ENCRYPTED  1
 #define CRYPTO_COMPLETE_ENCRYPTED      0
-#define CRYPTO_COMPLETE_BAD_METADATA  -1
-#define CRYPTO_COMPLETE_PARTIAL       -2
-#define CRYPTO_COMPLETE_INCONSISTENT  -3
-#define CRYPTO_COMPLETE_CORRUPT       -4
+#define CRYPTO_COMPLETE_BAD_METADATA  (-1)
+#define CRYPTO_COMPLETE_PARTIAL       (-2)
+#define CRYPTO_COMPLETE_INCONSISTENT  (-3)
+#define CRYPTO_COMPLETE_CORRUPT       (-4)
 
 /* Return values for cryptfs_enable_inplace*() */
 #define ENABLE_INPLACE_OK 0
-#define ENABLE_INPLACE_ERR_OTHER -1
-#define ENABLE_INPLACE_ERR_DEV -2  /* crypto_blkdev issue */
+#define ENABLE_INPLACE_ERR_OTHER (-1)
+#define ENABLE_INPLACE_ERR_DEV (-2)  /* crypto_blkdev issue */
 
 /* Return values for cryptfs_getfield */
 #define CRYPTO_GETFIELD_OK                   0
-#define CRYPTO_GETFIELD_ERROR_NO_FIELD      -1
-#define CRYPTO_GETFIELD_ERROR_OTHER         -2
-#define CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL -3
+#define CRYPTO_GETFIELD_ERROR_NO_FIELD      (-1)
+#define CRYPTO_GETFIELD_ERROR_OTHER         (-2)
+#define CRYPTO_GETFIELD_ERROR_BUF_TOO_SMALL (-3)
 
 /* Return values for cryptfs_setfield */
 #define CRYPTO_SETFIELD_OK                    0
-#define CRYPTO_SETFIELD_ERROR_OTHER          -1
-#define CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG -2
-#define CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG -3
+#define CRYPTO_SETFIELD_ERROR_OTHER          (-1)
+#define CRYPTO_SETFIELD_ERROR_FIELD_TOO_LONG (-2)
+#define CRYPTO_SETFIELD_ERROR_VALUE_TOO_LONG (-3)
 
 /* Return values for persist_del_key */
 #define PERSIST_DEL_KEY_OK                 0
-#define PERSIST_DEL_KEY_ERROR_OTHER       -1
-#define PERSIST_DEL_KEY_ERROR_NO_FIELD    -2
+#define PERSIST_DEL_KEY_ERROR_OTHER       (-1)
+#define PERSIST_DEL_KEY_ERROR_NO_FIELD    (-2)
 
 #ifdef __cplusplus
 extern "C" {
diff --git a/fs/Ext4.cpp b/fs/Ext4.cpp
index 0bd5b0c..0670bb5 100644
--- a/fs/Ext4.cpp
+++ b/fs/Ext4.cpp
@@ -55,7 +55,11 @@
 namespace ext4 {
 
 static const char* kResizefsPath = "/system/bin/resize2fs";
+#ifdef TARGET_USES_MKE2FS
+static const char* kMkfsPath = "/system/bin/mke2fs";
+#else
 static const char* kMkfsPath = "/system/bin/make_ext4fs";
+#endif
 static const char* kFsckPath = "/system/bin/e2fsck";
 
 bool IsSupported() {
@@ -165,6 +169,25 @@
         const std::string& target) {
     std::vector<std::string> cmd;
     cmd.push_back(kMkfsPath);
+
+#ifdef TARGET_USES_MKE2FS
+    cmd.push_back("-b");
+    cmd.push_back("4096");
+
+    cmd.push_back("-t");
+    cmd.push_back("ext4");
+
+    cmd.push_back("-M");
+    cmd.push_back(target);
+
+    cmd.push_back("-O");
+    cmd.push_back("^has_journal");
+
+    cmd.push_back(source);
+
+    if (numSectors)
+        cmd.push_back(StringPrintf("%lu", numSectors * (4096 / 512)));
+#else
     cmd.push_back("-J");
 
     cmd.push_back("-a");
@@ -178,6 +201,7 @@
     // Always generate a real UUID
     cmd.push_back("-u");
     cmd.push_back(source);
+#endif
 
     return ForkExecvp(cmd);
 }