Merge "Create misc_ce and misc_de directories on /mnt/expand" into tm-dev
diff --git a/Android.bp b/Android.bp
index 1550264..38abdb9 100644
--- a/Android.bp
+++ b/Android.bp
@@ -115,7 +115,6 @@
         "Benchmark.cpp",
         "Checkpoint.cpp",
         "CryptoType.cpp",
-        "Devmapper.cpp",
         "EncryptInplace.cpp",
         "FileDeviceUtils.cpp",
         "FsCrypt.cpp",
diff --git a/Devmapper.cpp b/Devmapper.cpp
deleted file mode 100644
index 00fb4b3..0000000
--- a/Devmapper.cpp
+++ /dev/null
@@ -1,100 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
-
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#include <linux/kdev_t.h>
-
-#include <android-base/logging.h>
-#include <android-base/stringprintf.h>
-#include <android-base/strings.h>
-#include <libdm/dm.h>
-#include <utils/Trace.h>
-
-#include "Devmapper.h"
-
-using android::base::StringPrintf;
-using namespace android::dm;
-
-static const char* kVoldPrefix = "vold:";
-
-int Devmapper::create(const char* name_raw, const char* loopFile, const char* key,
-                      unsigned long numSectors, char* ubuffer, size_t len) {
-    auto& dm = DeviceMapper::Instance();
-    auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
-
-    DmTable table;
-    table.Emplace<DmTargetCrypt>(0, numSectors, "twofish", key, 0, loopFile, 0);
-
-    if (!dm.CreateDevice(name_string, table)) {
-        LOG(ERROR) << "Failed to create device-mapper device " << name_string;
-        return -1;
-    }
-
-    std::string path;
-    if (!dm.GetDmDevicePathByName(name_string, &path)) {
-        LOG(ERROR) << "Failed to get device-mapper device path for " << name_string;
-        return -1;
-    }
-    snprintf(ubuffer, len, "%s", path.c_str());
-    return 0;
-}
-
-int Devmapper::destroy(const char* name_raw) {
-    auto& dm = DeviceMapper::Instance();
-
-    auto name_string = StringPrintf("%s%s", kVoldPrefix, name_raw);
-    if (!dm.DeleteDevice(name_string)) {
-        if (errno != ENXIO) {
-            PLOG(ERROR) << "Failed DM_DEV_REMOVE";
-        }
-        return -1;
-    }
-    return 0;
-}
-
-int Devmapper::destroyAll() {
-    ATRACE_NAME("Devmapper::destroyAll");
-
-    auto& dm = DeviceMapper::Instance();
-    std::vector<DeviceMapper::DmBlockDevice> devices;
-    if (!dm.GetAvailableDevices(&devices)) {
-        LOG(ERROR) << "Failed to get dm devices";
-        return -1;
-    }
-
-    for (const auto& device : devices) {
-        if (android::base::StartsWith(device.name(), kVoldPrefix)) {
-            LOG(DEBUG) << "Tearing down stale dm device named " << device.name();
-            if (!dm.DeleteDevice(device.name())) {
-                if (errno != ENXIO) {
-                    PLOG(WARNING) << "Failed to destroy dm device named " << device.name();
-                }
-            }
-        }
-    }
-    return 0;
-}
diff --git a/Devmapper.h b/Devmapper.h
deleted file mode 100644
index 9d4896e..0000000
--- a/Devmapper.h
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-#ifndef _DEVMAPPER_H
-#define _DEVMAPPER_H
-
-#include <linux/dm-ioctl.h>
-#include <unistd.h>
-
-class Devmapper {
-  public:
-    static int create(const char* name, const char* loopFile, const char* key,
-                      unsigned long numSectors, char* buffer, size_t len);
-    static int destroy(const char* name);
-    static int destroyAll();
-};
-
-#endif
diff --git a/EncryptInplace.cpp b/EncryptInplace.cpp
index 057b3ef..190bb83 100644
--- a/EncryptInplace.cpp
+++ b/EncryptInplace.cpp
@@ -20,13 +20,11 @@
 #include <ext4_utils/ext4_utils.h>
 #include <f2fs_sparseblock.h>
 #include <fcntl.h>
-#include <time.h>
 
 #include <algorithm>
 #include <vector>
 
 #include <android-base/logging.h>
-#include <android-base/properties.h>
 #include <android-base/unique_fd.h>
 
 enum EncryptInPlaceError {
@@ -43,7 +41,7 @@
 class InPlaceEncrypter {
   public:
     bool EncryptInPlace(const std::string& crypto_blkdev, const std::string& real_blkdev,
-                        uint64_t nr_sec, bool set_progress_properties);
+                        uint64_t nr_sec);
     bool ProcessUsedBlock(uint64_t block_num);
 
   private:
@@ -75,19 +73,14 @@
     std::string real_blkdev_;
     std::string crypto_blkdev_;
     uint64_t nr_sec_;
-    bool set_progress_properties_;
 
     android::base::unique_fd realfd_;
     android::base::unique_fd cryptofd_;
 
-    time_t time_started_;
-    int remaining_time_;
-
     std::string fs_type_;
     uint64_t blocks_done_;
     uint64_t blocks_to_encrypt_;
     unsigned int block_size_;
-    unsigned int cur_pct_;
 
     std::vector<uint8_t> io_buffer_;
     uint64_t first_pending_block_;
@@ -108,7 +101,6 @@
     blocks_done_ = 0;
     blocks_to_encrypt_ = blocks_to_encrypt;
     block_size_ = block_size;
-    cur_pct_ = 0;
 
     // Allocate the I/O buffer.  kIOBufferSize should always be a multiple of
     // the filesystem block size, but round it up just in case.
@@ -136,46 +128,6 @@
 
     if (blocks_done_ >= blocks_next_msg)
         LOG(DEBUG) << "Encrypted " << blocks_next_msg << " of " << blocks_to_encrypt_ << " blocks";
-
-    if (!set_progress_properties_) return;
-
-    uint64_t new_pct;
-    if (done) {
-        new_pct = 100;
-    } else {
-        new_pct = (blocks_done_ * 100) / std::max<uint64_t>(blocks_to_encrypt_, 1);
-        new_pct = std::min<uint64_t>(new_pct, 99);
-    }
-    if (new_pct > cur_pct_) {
-        cur_pct_ = new_pct;
-        android::base::SetProperty("vold.encrypt_progress", std::to_string(new_pct));
-    }
-
-    if (cur_pct_ >= 5) {
-        struct timespec time_now;
-        if (clock_gettime(CLOCK_MONOTONIC, &time_now)) {
-            PLOG(WARNING) << "Error getting time while updating encryption progress";
-        } else {
-            double elapsed_time = difftime(time_now.tv_sec, time_started_);
-
-            uint64_t remaining_blocks = 0;
-            if (blocks_done_ < blocks_to_encrypt_)
-                remaining_blocks = blocks_to_encrypt_ - blocks_done_;
-
-            int remaining_time = 0;
-            if (blocks_done_ != 0)
-                remaining_time = (int)(elapsed_time * remaining_blocks / blocks_done_);
-
-            // Change time only if not yet set, lower, or a lot higher for
-            // best user experience
-            if (remaining_time_ == -1 || remaining_time < remaining_time_ ||
-                remaining_time > remaining_time_ + 60) {
-                remaining_time_ = remaining_time;
-                android::base::SetProperty("vold.encrypt_time_remaining",
-                                           std::to_string(remaining_time));
-            }
-        }
-    }
 }
 
 bool InPlaceEncrypter::EncryptPendingData() {
@@ -313,14 +265,10 @@
 }
 
 bool InPlaceEncrypter::EncryptInPlace(const std::string& crypto_blkdev,
-                                      const std::string& real_blkdev, uint64_t nr_sec,
-                                      bool set_progress_properties) {
-    struct timespec time_started = {0};
-
+                                      const std::string& real_blkdev, uint64_t nr_sec) {
     real_blkdev_ = real_blkdev;
     crypto_blkdev_ = crypto_blkdev;
     nr_sec_ = nr_sec;
-    set_progress_properties_ = set_progress_properties;
 
     realfd_.reset(open64(real_blkdev.c_str(), O_RDONLY | O_CLOEXEC));
     if (realfd_ < 0) {
@@ -334,13 +282,6 @@
         return false;
     }
 
-    if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
-        PLOG(WARNING) << "Error getting time at start of in-place encryption";
-        // Note - continue anyway - we'll run with 0
-    }
-    time_started_ = time_started.tv_sec;
-    remaining_time_ = -1;
-
     bool success = DoEncryptInPlace();
 
     if (success) success &= EncryptPendingData();
@@ -359,8 +300,11 @@
                      << ") was incorrect; we actually encrypted " << blocks_done_
                      << " blocks.  Encryption progress was inaccurate";
     }
-    // Make sure vold.encrypt_progress gets set to 100.
+    // Ensure that the final progress message is printed, so the series of log
+    // messages ends with e.g. "Encrypted 50327 of 50327 blocks" rather than
+    // "Encrypted 50000 of 50327 blocks".
     UpdateProgress(0, true);
+
     LOG(INFO) << "Successfully encrypted " << DescribeFilesystem();
     return true;
 }
@@ -371,10 +315,10 @@
 // sectors; however, if a filesystem is detected, then its size will be used
 // instead, and only the in-use blocks of the filesystem will be encrypted.
 bool encrypt_inplace(const std::string& crypto_blkdev, const std::string& real_blkdev,
-                     uint64_t nr_sec, bool set_progress_properties) {
+                     uint64_t nr_sec) {
     LOG(DEBUG) << "encrypt_inplace(" << crypto_blkdev << ", " << real_blkdev << ", " << nr_sec
-               << ", " << (set_progress_properties ? "true" : "false") << ")";
+               << ")";
 
     InPlaceEncrypter encrypter;
-    return encrypter.EncryptInPlace(crypto_blkdev, real_blkdev, nr_sec, set_progress_properties);
+    return encrypter.EncryptInPlace(crypto_blkdev, real_blkdev, nr_sec);
 }
diff --git a/EncryptInplace.h b/EncryptInplace.h
index 480a47c..ef6f848 100644
--- a/EncryptInplace.h
+++ b/EncryptInplace.h
@@ -21,6 +21,6 @@
 #include <string>
 
 bool encrypt_inplace(const std::string& crypto_blkdev, const std::string& real_blkdev,
-                     uint64_t nr_sec, bool set_progress_properties);
+                     uint64_t nr_sec);
 
 #endif
diff --git a/MetadataCrypt.cpp b/MetadataCrypt.cpp
index 6550be4..bd3c0ef 100644
--- a/MetadataCrypt.cpp
+++ b/MetadataCrypt.cpp
@@ -314,7 +314,7 @@
             }
             LOG(DEBUG) << "Format of " << crypto_blkdev << " for " << mount_point << " succeeded.";
         } else {
-            if (!encrypt_inplace(crypto_blkdev, blk_device, nr_sec, false)) {
+            if (!encrypt_inplace(crypto_blkdev, blk_device, nr_sec)) {
                 LOG(ERROR) << "encrypt_inplace failed in mountFstab";
                 return false;
             }
diff --git a/VoldNativeService.cpp b/VoldNativeService.cpp
index 1903063..f5fb908 100644
--- a/VoldNativeService.cpp
+++ b/VoldNativeService.cpp
@@ -414,16 +414,13 @@
     return translate(VolumeManager::Instance()->fixupAppDir(path, appUid));
 }
 
-binder::Status VoldNativeService::createObb(const std::string& sourcePath,
-                                            const std::string& sourceKey, int32_t ownerGid,
+binder::Status VoldNativeService::createObb(const std::string& sourcePath, int32_t ownerGid,
                                             std::string* _aidl_return) {
     ENFORCE_SYSTEM_OR_ROOT;
     CHECK_ARGUMENT_PATH(sourcePath);
-    CHECK_ARGUMENT_HEX(sourceKey);
     ACQUIRE_LOCK;
 
-    return translate(
-            VolumeManager::Instance()->createObb(sourcePath, sourceKey, ownerGid, _aidl_return));
+    return translate(VolumeManager::Instance()->createObb(sourcePath, ownerGid, _aidl_return));
 }
 
 binder::Status VoldNativeService::destroyObb(const std::string& volId) {
diff --git a/VoldNativeService.h b/VoldNativeService.h
index 653165f..88fc9e7 100644
--- a/VoldNativeService.h
+++ b/VoldNativeService.h
@@ -73,8 +73,8 @@
     binder::Status setupAppDir(const std::string& path, int32_t appUid);
     binder::Status fixupAppDir(const std::string& path, int32_t appUid);
 
-    binder::Status createObb(const std::string& sourcePath, const std::string& sourceKey,
-                             int32_t ownerGid, std::string* _aidl_return);
+    binder::Status createObb(const std::string& sourcePath, int32_t ownerGid,
+                             std::string* _aidl_return);
     binder::Status destroyObb(const std::string& volId);
 
     binder::Status createStubVolume(const std::string& sourcePath, const std::string& mountPath,
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index 9311321..bc51042 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -55,7 +55,6 @@
 #include <fscrypt/fscrypt.h>
 
 #include "AppFuseUtil.h"
-#include "Devmapper.h"
 #include "FsCrypt.h"
 #include "Loop.h"
 #include "NetlinkManager.h"
@@ -179,7 +178,6 @@
     // directories that we own, in case we crashed.
     unmountAll();
 
-    Devmapper::destroyAll();
     Loop::destroyAll();
 
     // Assume that we always have an emulated volume on internal
@@ -1075,8 +1073,8 @@
     return setupAppDir(path, appUid, true /* fixupExistingOnly */);
 }
 
-int VolumeManager::createObb(const std::string& sourcePath, const std::string& sourceKey,
-                             int32_t ownerGid, std::string* outVolId) {
+int VolumeManager::createObb(const std::string& sourcePath, int32_t ownerGid,
+                             std::string* outVolId) {
     int id = mNextObbId++;
 
     std::string lowerSourcePath;
@@ -1114,7 +1112,7 @@
     }
 
     auto vol = std::shared_ptr<android::vold::VolumeBase>(
-            new android::vold::ObbVolume(id, lowerSourcePath, sourceKey, ownerGid));
+            new android::vold::ObbVolume(id, lowerSourcePath, ownerGid));
     vol->create();
 
     mObbVolumes.push_back(vol);
diff --git a/VolumeManager.h b/VolumeManager.h
index 3573b1a..a8117c9 100644
--- a/VolumeManager.h
+++ b/VolumeManager.h
@@ -186,8 +186,7 @@
     // Called before zygote starts to ensure dir exists so zygote can bind mount them.
     int ensureAppDirsCreated(const std::vector<std::string>& paths, int32_t appUid);
 
-    int createObb(const std::string& path, const std::string& key, int32_t ownerGid,
-                  std::string* outVolId);
+    int createObb(const std::string& path, int32_t ownerGid, std::string* outVolId);
     int destroyObb(const std::string& volId);
 
     int createStubVolume(const std::string& sourcePath, const std::string& mountPath,
diff --git a/binder/android/os/IVold.aidl b/binder/android/os/IVold.aidl
index 7c2e88b..aee75f5 100644
--- a/binder/android/os/IVold.aidl
+++ b/binder/android/os/IVold.aidl
@@ -60,8 +60,7 @@
     void fixupAppDir(@utf8InCpp String path, int appUid);
     void ensureAppDirsCreated(in @utf8InCpp String[] paths, int appUid);
 
-    @utf8InCpp String createObb(@utf8InCpp String sourcePath, @utf8InCpp String sourceKey,
-                                int ownerGid);
+    @utf8InCpp String createObb(@utf8InCpp String sourcePath, int ownerGid);
     void destroyObb(@utf8InCpp String volId);
 
     void fstrim(int fstrimFlags, IVoldTaskListener listener);
diff --git a/model/ObbVolume.cpp b/model/ObbVolume.cpp
index 21479c4..b64c1ba 100644
--- a/model/ObbVolume.cpp
+++ b/model/ObbVolume.cpp
@@ -15,7 +15,6 @@
  */
 
 #include "ObbVolume.h"
-#include "Devmapper.h"
 #include "Loop.h"
 #include "Utils.h"
 #include "VoldUtil.h"
@@ -39,12 +38,10 @@
 namespace android {
 namespace vold {
 
-ObbVolume::ObbVolume(int id, const std::string& sourcePath, const std::string& sourceKey,
-                     gid_t ownerGid)
+ObbVolume::ObbVolume(int id, const std::string& sourcePath, gid_t ownerGid)
     : VolumeBase(Type::kObb) {
     setId(StringPrintf("obb:%d", id));
     mSourcePath = sourcePath;
-    mSourceKey = sourceKey;
     mOwnerGid = ownerGid;
 }
 
@@ -55,36 +52,13 @@
         PLOG(ERROR) << getId() << " failed to create loop";
         return -1;
     }
-
-    if (!mSourceKey.empty()) {
-        uint64_t nr_sec = 0;
-        if (GetBlockDev512Sectors(mLoopPath, &nr_sec) != OK) {
-            PLOG(ERROR) << getId() << " failed to get loop size";
-            return -1;
-        }
-
-        char tmp[PATH_MAX];
-        if (Devmapper::create(getId().c_str(), mLoopPath.c_str(), mSourceKey.c_str(), nr_sec, tmp,
-                              PATH_MAX)) {
-            PLOG(ERROR) << getId() << " failed to create dm";
-            return -1;
-        }
-        mDmPath = tmp;
-        mMountPath = mDmPath;
-    } else {
-        mMountPath = mLoopPath;
-    }
     return OK;
 }
 
 status_t ObbVolume::doDestroy() {
-    if (!mDmPath.empty() && Devmapper::destroy(getId().c_str())) {
-        PLOG(WARNING) << getId() << " failed to destroy dm";
-    }
     if (!mLoopPath.empty() && Loop::destroyByDevice(mLoopPath.c_str())) {
         PLOG(WARNING) << getId() << " failed to destroy loop";
     }
-    mDmPath.clear();
     mLoopPath.clear();
     return OK;
 }
@@ -98,7 +72,7 @@
         return -1;
     }
     // clang-format off
-    if (android::vold::vfat::Mount(mMountPath, path, true, false, true,
+    if (android::vold::vfat::Mount(mLoopPath, path, true, false, true,
                                    0, mOwnerGid, 0227, false)) {
         // clang-format on
         PLOG(ERROR) << getId() << " failed to mount";
diff --git a/model/ObbVolume.h b/model/ObbVolume.h
index 8f7ee94..bfcd3d2 100644
--- a/model/ObbVolume.h
+++ b/model/ObbVolume.h
@@ -29,7 +29,7 @@
  */
 class ObbVolume : public VolumeBase {
   public:
-    ObbVolume(int id, const std::string& sourcePath, const std::string& sourceKey, gid_t ownerGid);
+    ObbVolume(int id, const std::string& sourcePath, gid_t ownerGid);
     virtual ~ObbVolume();
 
   protected:
@@ -40,12 +40,9 @@
 
   private:
     std::string mSourcePath;
-    std::string mSourceKey;
     gid_t mOwnerGid;
 
     std::string mLoopPath;
-    std::string mDmPath;
-    std::string mMountPath;
 
     DISALLOW_COPY_AND_ASSIGN(ObbVolume);
 };