Skip QD1A.190821.011 in stage-aosp-master

Bug: 141248619
Change-Id: Ie1bf139b15621fc9cc1270865b03ec0f459d15ed
diff --git a/.clang-format b/.clang-format
deleted file mode 100644
index ae4a451..0000000
--- a/.clang-format
+++ /dev/null
@@ -1,11 +0,0 @@
-BasedOnStyle: Google
-AccessModifierOffset: -2
-AllowShortFunctionsOnASingleLine: Inline
-ColumnLimit: 100
-CommentPragmas: NOLINT:.*
-DerivePointerAlignment: false
-IndentWidth: 4
-PointerAlignment: Left
-TabWidth: 4
-UseTab: Never
-PenaltyExcessCharacter: 32
diff --git a/.clang-format b/.clang-format
new file mode 120000
index 0000000..ddcf5a2
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1 @@
+../../build/soong/scripts/system-clang-format
\ No newline at end of file
diff --git a/Android.bp b/Android.bp
index 1045dc2..b621f35 100644
--- a/Android.bp
+++ b/Android.bp
@@ -30,6 +30,8 @@
     static_libs: [
         "libavb",
         "libbootloader_message",
+        "libdm",
+        "libext2_uuid",
         "libfec",
         "libfec_rs",
         "libfs_avb",
@@ -54,7 +56,6 @@
         "libhardware",
         "libhardware_legacy",
         "libhidlbase",
-        "libhwbinder",
         "libkeymaster4support",
         "libkeyutils",
         "liblog",
@@ -85,6 +86,7 @@
 
 cc_library_headers {
     name: "libvold_headers",
+    recovery_available: true,
     export_include_dirs: ["."],
 }
 
@@ -189,7 +191,6 @@
 
     shared_libs: [
         "android.hardware.health.storage@1.0",
-        "libhidltransport",
     ],
 }
 
@@ -227,7 +228,6 @@
         "libhardware",
         "libhardware_legacy",
         "libhidlbase",
-        "libhwbinder",
         "libkeymaster4support",
     ],
 }
@@ -267,6 +267,5 @@
         "binder/android/os/IVoldListener.aidl",
         "binder/android/os/IVoldTaskListener.aidl",
     ],
+    path: "binder",
 }
-
-subdirs = ["tests"]
diff --git a/Benchmark.cpp b/Benchmark.cpp
index b0a3b85..0770da7 100644
--- a/Benchmark.cpp
+++ b/Benchmark.cpp
@@ -23,8 +23,8 @@
 #include <android-base/logging.h>
 
 #include <cutils/iosched_policy.h>
-#include <hardware_legacy/power.h>
 #include <private/android_filesystem_config.h>
+#include <wakelock/wakelock.h>
 
 #include <thread>
 
@@ -181,7 +181,7 @@
 void Benchmark(const std::string& path,
                const android::sp<android::os::IVoldTaskListener>& listener) {
     std::lock_guard<std::mutex> lock(kBenchmarkLock);
-    acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
+    android::wakelock::WakeLock wl{kWakeLock};
 
     PerformanceBoost boost;
     android::os::PersistableBundle extras;
@@ -190,8 +190,6 @@
     if (listener) {
         listener->onFinished(res, extras);
     }
-
-    release_wake_lock(kWakeLock);
 }
 
 }  // namespace vold
diff --git a/Checkpoint.cpp b/Checkpoint.cpp
index c8af08c..e5ef4a2 100644
--- a/Checkpoint.cpp
+++ b/Checkpoint.cpp
@@ -61,6 +61,16 @@
 namespace {
 const std::string kMetadataCPFile = "/metadata/vold/checkpoint";
 
+binder::Status error(const std::string& msg) {
+    PLOG(ERROR) << msg;
+    return binder::Status::fromServiceSpecificError(errno, String8(msg.c_str()));
+}
+
+binder::Status error(int error, const std::string& msg) {
+    LOG(ERROR) << msg;
+    return binder::Status::fromServiceSpecificError(error, String8(msg.c_str()));
+}
+
 bool setBowState(std::string const& block_device, std::string const& state) {
     std::string bow_device = fs_mgr_find_bow_device(block_device);
     if (bow_device.empty()) return false;
@@ -112,7 +122,11 @@
 }
 
 Status cp_startCheckpoint(int retry) {
-    if (retry < -1) return Status::fromExceptionCode(EINVAL, "Retry count must be more than -1");
+    bool result;
+    if (!cp_supportsCheckpoint(result).isOk() || !result)
+        return error(ENOTSUP, "Checkpoints not supported");
+
+    if (retry < -1) return error(EINVAL, "Retry count must be more than -1");
     std::string content = std::to_string(retry + 1);
     if (retry == -1) {
         sp<IBootControl> module = IBootControl::getService();
@@ -123,16 +137,22 @@
         }
     }
     if (!android::base::WriteStringToFile(content, kMetadataCPFile))
-        return Status::fromExceptionCode(errno, "Failed to write checkpoint file");
+        return error("Failed to write checkpoint file");
     return Status::ok();
 }
 
 namespace {
 
 volatile bool isCheckpointing = false;
+
+// Protects isCheckpointing and code that makes decisions based on status of
+// isCheckpointing
+std::mutex isCheckpointingLock;
 }
 
 Status cp_commitChanges() {
+    std::lock_guard<std::mutex> lock(isCheckpointingLock);
+
     if (!isCheckpointing) {
         return Status::ok();
     }
@@ -145,10 +165,8 @@
     if (module) {
         CommandResult cr;
         module->markBootSuccessful([&cr](CommandResult result) { cr = result; });
-        if (!cr.success) {
-            std::string msg = "Error marking booted successfully: " + std::string(cr.errMsg);
-            return Status::fromExceptionCode(EINVAL, String8(msg.c_str()));
-        }
+        if (!cr.success)
+            return error(EINVAL, "Error marking booted successfully: " + std::string(cr.errMsg));
         LOG(INFO) << "Marked slot as booted successfully.";
     }
     // Must take action for list of mounted checkpointed things here
@@ -159,7 +177,7 @@
 
     Fstab mounts;
     if (!ReadFstabFromFile("/proc/mounts", &mounts)) {
-        return Status::fromExceptionCode(EINVAL, "Failed to get /proc/mounts");
+        return error(EINVAL, "Failed to get /proc/mounts");
     }
 
     // Walk mounted file systems
@@ -172,19 +190,20 @@
                 std::string options = mount_rec.fs_options + ",checkpoint=enable";
                 if (mount(mount_rec.blk_device.c_str(), mount_rec.mount_point.c_str(), "none",
                           MS_REMOUNT | fstab_rec->flags, options.c_str())) {
-                    return Status::fromExceptionCode(EINVAL, "Failed to remount");
+                    return error(EINVAL, "Failed to remount");
                 }
             }
         } else if (fstab_rec->fs_mgr_flags.checkpoint_blk) {
             if (!setBowState(mount_rec.blk_device, "2"))
-                return Status::fromExceptionCode(EINVAL, "Failed to set bow state");
+                return error(EINVAL, "Failed to set bow state");
         }
     }
     SetProperty("vold.checkpoint_committed", "1");
     LOG(INFO) << "Checkpoint has been committed.";
     isCheckpointing = false;
     if (!android::base::RemoveFileIfExists(kMetadataCPFile, &err_str))
-        return Status::fromExceptionCode(errno, err_str.c_str());
+        return error(err_str.c_str());
+
     return Status::ok();
 }
 
@@ -244,10 +263,16 @@
 }
 
 bool cp_needsCheckpoint() {
+    // Make sure we only return true during boot. See b/138952436 for discussion
+    static bool called_once = false;
+    if (called_once) return isCheckpointing;
+    called_once = true;
+
     bool ret;
     std::string content;
     sp<IBootControl> module = IBootControl::getService();
 
+    std::lock_guard<std::mutex> lock(isCheckpointingLock);
     if (isCheckpointing) return isCheckpointing;
 
     if (module && module->isSlotMarkedSuccessful(module->getCurrentSlot()) == BoolResult::FALSE) {
@@ -317,13 +342,14 @@
 }  // namespace
 
 Status cp_prepareCheckpoint() {
+    std::lock_guard<std::mutex> lock(isCheckpointingLock);
     if (!isCheckpointing) {
         return Status::ok();
     }
 
     Fstab mounts;
     if (!ReadFstabFromFile("/proc/mounts", &mounts)) {
-        return Status::fromExceptionCode(EINVAL, "Failed to get /proc/mounts");
+        return error(EINVAL, "Failed to get /proc/mounts");
     }
 
     for (const auto& mount_rec : mounts) {
@@ -583,10 +609,7 @@
 
         LOG(INFO) << action << " checkpoint on " << blockDevice;
         base::unique_fd device_fd(open(blockDevice.c_str(), O_RDWR | O_CLOEXEC));
-        if (device_fd < 0) {
-            PLOG(ERROR) << "Cannot open " << blockDevice;
-            return Status::fromExceptionCode(errno, ("Cannot open " + blockDevice).c_str());
-        }
+        if (device_fd < 0) return error("Cannot open " + blockDevice);
 
         log_sector_v1_0 original_ls;
         read(device_fd, reinterpret_cast<char*>(&original_ls), sizeof(original_ls));
@@ -594,8 +617,7 @@
             validating = false;
             action = "Restoring";
         } else if (original_ls.magic != kMagic) {
-            LOG(ERROR) << "No magic";
-            return Status::fromExceptionCode(EINVAL, "No magic");
+            return error(EINVAL, "No magic");
         }
 
         LOG(INFO) << action << " " << original_ls.sequence << " log sectors";
@@ -609,23 +631,18 @@
             used_sectors[0] = false;
 
             if (ls.magic != kMagic && (ls.magic != kPartialRestoreMagic || validating)) {
-                LOG(ERROR) << "No magic!";
-                status = Status::fromExceptionCode(EINVAL, "No magic");
+                status = error(EINVAL, "No magic");
                 break;
             }
 
             if (ls.block_size != original_ls.block_size) {
-                LOG(ERROR) << "Block size mismatch!";
-                status = Status::fromExceptionCode(EINVAL, "Block size mismatch");
+                status = error(EINVAL, "Block size mismatch");
                 break;
             }
 
             if ((int)ls.sequence != sequence) {
-                LOG(ERROR) << "Expecting log sector " << sequence << " but got " << ls.sequence;
-                status = Status::fromExceptionCode(
-                    EINVAL, ("Expecting log sector " + std::to_string(sequence) + " but got " +
-                             std::to_string(ls.sequence))
-                                .c_str());
+                status = error(EINVAL, "Expecting log sector " + std::to_string(sequence) +
+                                           " but got " + std::to_string(ls.sequence));
                 break;
             }
 
@@ -646,8 +663,7 @@
                 }
 
                 if (le->checksum && checksum != le->checksum) {
-                    LOG(ERROR) << "Checksums don't match " << std::hex << checksum;
-                    status = Status::fromExceptionCode(EINVAL, "Checksums don't match");
+                    status = error(EINVAL, "Checksums don't match");
                     break;
                 }
 
@@ -657,8 +673,7 @@
                     restoreSector(device_fd, used_sectors, ls_buffer, le, buffer);
                     restore_count++;
                     if (restore_limit && restore_count >= restore_limit) {
-                        LOG(WARNING) << "Hit the test limit";
-                        status = Status::fromExceptionCode(EAGAIN, "Hit the test limit");
+                        status = error(EAGAIN, "Hit the test limit");
                         break;
                     }
                 }
@@ -696,20 +711,18 @@
 
     // If the file doesn't exist, we aren't managing a checkpoint retry counter
     if (result != 0) return Status::ok();
-    if (!android::base::ReadFileToString(kMetadataCPFile, &oldContent)) {
-        PLOG(ERROR) << "Failed to read checkpoint file";
-        return Status::fromExceptionCode(errno, "Failed to read checkpoint file");
-    }
+    if (!android::base::ReadFileToString(kMetadataCPFile, &oldContent))
+        return error("Failed to read checkpoint file");
     std::string retryContent = oldContent.substr(0, oldContent.find_first_of(" "));
 
     if (!android::base::ParseInt(retryContent, &retry))
-        return Status::fromExceptionCode(EINVAL, "Could not parse retry count");
+        return error(EINVAL, "Could not parse retry count");
     if (retry > 0) {
         retry--;
 
         newContent = std::to_string(retry);
         if (!android::base::WriteStringToFile(newContent, kMetadataCPFile))
-            return Status::fromExceptionCode(errno, "Could not write checkpoint file");
+            return error("Could not write checkpoint file");
     }
     return Status::ok();
 }
diff --git a/Devmapper.cpp b/Devmapper.cpp
index b42467c..d55d92d 100644
--- a/Devmapper.cpp
+++ b/Devmapper.cpp
@@ -23,7 +23,6 @@
 #include <string.h>
 #include <unistd.h>
 
-#include <sys/ioctl.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 
@@ -32,239 +31,72 @@
 #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"
 
-#define DEVMAPPER_BUFFER_SIZE 4096
-
 using android::base::StringPrintf;
+using namespace android::dm;
 
 static const char* kVoldPrefix = "vold:";
 
-void Devmapper::ioctlInit(struct dm_ioctl* io, size_t dataSize, const char* name, unsigned flags) {
-    memset(io, 0, dataSize);
-    io->data_size = dataSize;
-    io->data_start = sizeof(struct dm_ioctl);
-    io->version[0] = 4;
-    io->version[1] = 0;
-    io->version[2] = 0;
-    io->flags = flags;
-    if (name) {
-        size_t ret = strlcpy(io->name, name, sizeof(io->name));
-        if (ret >= sizeof(io->name)) abort();
-    }
-}
-
 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);
-    const char* name = name_string.c_str();
 
-    char* buffer = (char*)malloc(DEVMAPPER_BUFFER_SIZE);
-    if (!buffer) {
-        PLOG(ERROR) << "Failed malloc";
+    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;
     }
 
-    int fd;
-    if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
-        PLOG(ERROR) << "Failed open";
-        free(buffer);
+    std::string path;
+    if (!dm.GetDmDevicePathByName(name_string, &path)) {
+        LOG(ERROR) << "Failed to get device-mapper device path for " << name_string;
         return -1;
     }
-
-    struct dm_ioctl* io = (struct dm_ioctl*)buffer;
-
-    // Create the DM device
-    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
-
-    if (ioctl(fd, DM_DEV_CREATE, io)) {
-        PLOG(ERROR) << "Failed DM_DEV_CREATE";
-        free(buffer);
-        close(fd);
-        return -1;
-    }
-
-    // Set the legacy geometry
-    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
-
-    char* geoParams = buffer + sizeof(struct dm_ioctl);
-    // bps=512 spc=8 res=32 nft=2 sec=8190 mid=0xf0 spt=63 hds=64 hid=0 bspf=8 rdcl=2 infs=1 bkbs=2
-    strlcpy(geoParams, "0 64 63 0", DEVMAPPER_BUFFER_SIZE - sizeof(struct dm_ioctl));
-    geoParams += strlen(geoParams) + 1;
-    geoParams = (char*)_align(geoParams, 8);
-    if (ioctl(fd, DM_DEV_SET_GEOMETRY, io)) {
-        PLOG(ERROR) << "Failed DM_DEV_SET_GEOMETRY";
-        free(buffer);
-        close(fd);
-        return -1;
-    }
-
-    // Retrieve the device number we were allocated
-    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
-    if (ioctl(fd, DM_DEV_STATUS, io)) {
-        PLOG(ERROR) << "Failed DM_DEV_STATUS";
-        free(buffer);
-        close(fd);
-        return -1;
-    }
-
-    unsigned minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
-    snprintf(ubuffer, len, "/dev/block/dm-%u", minor);
-
-    // Load the table
-    struct dm_target_spec* tgt;
-    tgt = (struct dm_target_spec*)&buffer[sizeof(struct dm_ioctl)];
-
-    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, DM_STATUS_TABLE_FLAG);
-    io->target_count = 1;
-    tgt->status = 0;
-
-    tgt->sector_start = 0;
-    tgt->length = numSectors;
-
-    strlcpy(tgt->target_type, "crypt", sizeof(tgt->target_type));
-
-    char* cryptParams = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
-    snprintf(cryptParams,
-             DEVMAPPER_BUFFER_SIZE - (sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec)),
-             "twofish %s 0 %s 0", key, loopFile);
-    cryptParams += strlen(cryptParams) + 1;
-    cryptParams = (char*)_align(cryptParams, 8);
-    tgt->next = cryptParams - buffer;
-
-    if (ioctl(fd, DM_TABLE_LOAD, io)) {
-        PLOG(ERROR) << "Failed DM_TABLE_LOAD";
-        free(buffer);
-        close(fd);
-        return -1;
-    }
-
-    // Resume the new table
-    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
-
-    if (ioctl(fd, DM_DEV_SUSPEND, io)) {
-        PLOG(ERROR) << "Failed DM_DEV_SUSPEND";
-        free(buffer);
-        close(fd);
-        return -1;
-    }
-
-    free(buffer);
-
-    close(fd);
+    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);
-    const char* name = name_string.c_str();
-
-    char* buffer = (char*)malloc(DEVMAPPER_BUFFER_SIZE);
-    if (!buffer) {
-        PLOG(ERROR) << "Failed malloc";
-        return -1;
-    }
-
-    int fd;
-    if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
-        PLOG(ERROR) << "Failed open";
-        free(buffer);
-        return -1;
-    }
-
-    struct dm_ioctl* io = (struct dm_ioctl*)buffer;
-
-    // Create the DM device
-    ioctlInit(io, DEVMAPPER_BUFFER_SIZE, name, 0);
-
-    if (ioctl(fd, DM_DEV_REMOVE, io)) {
+    if (!dm.DeleteDevice(name_string)) {
         if (errno != ENXIO) {
             PLOG(ERROR) << "Failed DM_DEV_REMOVE";
         }
-        free(buffer);
-        close(fd);
         return -1;
     }
-
-    free(buffer);
-    close(fd);
     return 0;
 }
 
 int Devmapper::destroyAll() {
     ATRACE_NAME("Devmapper::destroyAll");
-    char* buffer = (char*)malloc(1024 * 64);
-    if (!buffer) {
-        PLOG(ERROR) << "Failed malloc";
-        return -1;
-    }
-    memset(buffer, 0, (1024 * 64));
 
-    char* buffer2 = (char*)malloc(DEVMAPPER_BUFFER_SIZE);
-    if (!buffer2) {
-        PLOG(ERROR) << "Failed malloc";
-        free(buffer);
+    auto& dm = DeviceMapper::Instance();
+    std::vector<DeviceMapper::DmBlockDevice> devices;
+    if (!dm.GetAvailableDevices(&devices)) {
+        LOG(ERROR) << "Failed to get dm devices";
         return -1;
     }
 
-    int fd;
-    if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
-        PLOG(ERROR) << "Failed open";
-        free(buffer);
-        free(buffer2);
-        return -1;
-    }
-
-    struct dm_ioctl* io = (struct dm_ioctl*)buffer;
-    ioctlInit(io, (1024 * 64), NULL, 0);
-
-    if (ioctl(fd, DM_LIST_DEVICES, io)) {
-        PLOG(ERROR) << "Failed DM_LIST_DEVICES";
-        free(buffer);
-        free(buffer2);
-        close(fd);
-        return -1;
-    }
-
-    struct dm_name_list* n = (struct dm_name_list*)(((char*)buffer) + io->data_start);
-    if (!n->dev) {
-        free(buffer);
-        free(buffer2);
-        close(fd);
-        return 0;
-    }
-
-    unsigned nxt = 0;
-    do {
-        n = (struct dm_name_list*)(((char*)n) + nxt);
-        auto name = std::string(n->name);
-        if (android::base::StartsWith(name, kVoldPrefix)) {
-            LOG(DEBUG) << "Tearing down stale dm device named " << name;
-
-            memset(buffer2, 0, DEVMAPPER_BUFFER_SIZE);
-            struct dm_ioctl* io2 = (struct dm_ioctl*)buffer2;
-            ioctlInit(io2, DEVMAPPER_BUFFER_SIZE, n->name, 0);
-            if (ioctl(fd, DM_DEV_REMOVE, io2)) {
+    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 " << name;
+                    PLOG(WARNING) << "Failed to destroy dm device named " << device.name();
                 }
             }
         } else {
-            LOG(DEBUG) << "Found unmanaged dm device named " << name;
+            LOG(DEBUG) << "Found unmanaged dm device named " << device.name();
         }
-        nxt = n->next;
-    } while (nxt);
-
-    free(buffer);
-    free(buffer2);
-    close(fd);
+    }
     return 0;
 }
-
-void* Devmapper::_align(void* ptr, unsigned int a) {
-    unsigned long agn = --a;
-
-    return (void*)(((unsigned long)ptr + agn) & ~agn);
-}
diff --git a/Devmapper.h b/Devmapper.h
index b1f6dfa..9d4896e 100644
--- a/Devmapper.h
+++ b/Devmapper.h
@@ -26,10 +26,6 @@
                       unsigned long numSectors, char* buffer, size_t len);
     static int destroy(const char* name);
     static int destroyAll();
-
-  private:
-    static void* _align(void* ptr, unsigned int a);
-    static void ioctlInit(struct dm_ioctl* io, size_t data_size, const char* name, unsigned flags);
 };
 
 #endif
diff --git a/FsCrypt.cpp b/FsCrypt.cpp
index 2a8e110..bea9328 100644
--- a/FsCrypt.cpp
+++ b/FsCrypt.cpp
@@ -57,6 +57,7 @@
 #include <android-base/logging.h>
 #include <android-base/properties.h>
 #include <android-base/stringprintf.h>
+#include <android-base/strings.h>
 #include <android-base/unique_fd.h>
 
 using android::base::StringPrintf;
@@ -70,7 +71,10 @@
 struct PolicyKeyRef {
     std::string contents_mode;
     std::string filenames_mode;
+    int policy_version;
     std::string key_raw_ref;
+
+    PolicyKeyRef() : policy_version(0) {}
 };
 
 const std::string device_key_dir = std::string() + DATA_MNT_POINT + fscrypt_unencrypted_folder;
@@ -84,7 +88,7 @@
 const std::string systemwide_volume_key_dir =
     std::string() + DATA_MNT_POINT + "/misc/vold/volume_keys";
 
-bool s_global_de_initialized = false;
+bool s_systemwide_keys_initialized = false;
 
 // Some users are ephemeral, don't try to wipe their keys from disk
 std::set<userid_t> s_ephemeral_users;
@@ -199,13 +203,66 @@
     return false;
 }
 
+// Retrieve the options to use for encryption policies on the /data filesystem.
+static void get_data_file_encryption_options(PolicyKeyRef* key_ref) {
+    auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
+    if (entry == nullptr) {
+        return;
+    }
+    key_ref->contents_mode = entry->file_contents_mode;
+    key_ref->filenames_mode = entry->file_names_mode;
+    key_ref->policy_version = entry->file_policy_version;
+}
+
+// Retrieve the version to use for encryption policies on the /data filesystem.
+static int get_data_file_policy_version(void) {
+    auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
+    if (entry == nullptr) {
+        return 0;
+    }
+    return entry->file_policy_version;
+}
+
+// Retrieve the options to use for encryption policies on adoptable storage.
+static bool get_volume_file_encryption_options(PolicyKeyRef* key_ref) {
+    key_ref->contents_mode =
+            android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
+    key_ref->filenames_mode =
+            android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
+    key_ref->policy_version = 1;
+
+    std::string raw_flags = android::base::GetProperty("ro.crypto.volume.flags", "");
+    auto flags = android::base::Split(raw_flags, "+");
+    for (const auto& flag : flags) {
+        if (flag == "v1") {
+            key_ref->policy_version = 1;
+        } else if (flag == "v2") {
+            key_ref->policy_version = 2;
+        } else {
+            LOG(ERROR) << "Unknown flag in ro.crypto.volume.flags: " << flag;
+            return false;
+        }
+    }
+    return true;
+}
+
+// Install a key for use by encrypted files on the /data filesystem.
+static bool install_data_key(const KeyBuffer& key, std::string* raw_ref) {
+    return android::vold::installKey(key, DATA_MNT_POINT, get_data_file_policy_version(), raw_ref);
+}
+
+// Evict a key for use by encrypted files on the /data filesystem.
+static bool evict_data_key(const std::string& raw_ref) {
+    return android::vold::evictKey(DATA_MNT_POINT, raw_ref, get_data_file_policy_version());
+}
+
 static bool read_and_install_user_ce_key(userid_t user_id,
                                          const android::vold::KeyAuthentication& auth) {
     if (s_ce_key_raw_refs.count(user_id) != 0) return true;
     KeyBuffer ce_key;
     if (!read_and_fixate_user_ce_key(user_id, auth, &ce_key)) return false;
     std::string ce_raw_ref;
-    if (!android::vold::installKey(ce_key, &ce_raw_ref)) return false;
+    if (!install_data_key(ce_key, &ce_raw_ref)) return false;
     s_ce_keys[user_id] = std::move(ce_key);
     s_ce_key_raw_refs[user_id] = ce_raw_ref;
     LOG(DEBUG) << "Installed ce key for user " << user_id;
@@ -255,10 +312,10 @@
             return false;
     }
     std::string de_raw_ref;
-    if (!android::vold::installKey(de_key, &de_raw_ref)) return false;
+    if (!install_data_key(de_key, &de_raw_ref)) return false;
     s_de_key_raw_refs[user_id] = de_raw_ref;
     std::string ce_raw_ref;
-    if (!android::vold::installKey(ce_key, &ce_raw_ref)) return false;
+    if (!install_data_key(ce_key, &ce_raw_ref)) return false;
     s_ce_keys[user_id] = ce_key;
     s_ce_key_raw_refs[user_id] = ce_raw_ref;
     LOG(DEBUG) << "Created keys for user " << user_id;
@@ -276,19 +333,10 @@
     return true;
 }
 
-static void get_data_file_encryption_modes(PolicyKeyRef* key_ref) {
-    auto entry = GetEntryForMountPoint(&fstab_default, DATA_MNT_POINT);
-    if (entry == nullptr) {
-        return;
-    }
-    key_ref->contents_mode = entry->file_contents_mode;
-    key_ref->filenames_mode = entry->file_names_mode;
-}
-
 static bool ensure_policy(const PolicyKeyRef& key_ref, const std::string& path) {
     return fscrypt_policy_ensure(path.c_str(), key_ref.key_raw_ref.data(),
                                  key_ref.key_raw_ref.size(), key_ref.contents_mode.c_str(),
-                                 key_ref.filenames_mode.c_str()) == 0;
+                                 key_ref.filenames_mode.c_str(), key_ref.policy_version) == 0;
 }
 
 static bool is_numeric(const char* name) {
@@ -325,7 +373,7 @@
             KeyBuffer key;
             if (!android::vold::retrieveKey(key_path, kEmptyAuthentication, &key)) return false;
             std::string raw_ref;
-            if (!android::vold::installKey(key, &raw_ref)) return false;
+            if (!install_data_key(key, &raw_ref)) return false;
             s_de_key_raw_refs[user_id] = raw_ref;
             LOG(DEBUG) << "Installed de key for user " << user_id;
         }
@@ -335,31 +383,42 @@
     return true;
 }
 
-bool fscrypt_initialize_global_de() {
-    LOG(INFO) << "fscrypt_initialize_global_de";
+bool fscrypt_initialize_systemwide_keys() {
+    LOG(INFO) << "fscrypt_initialize_systemwide_keys";
 
-    if (s_global_de_initialized) {
+    if (s_systemwide_keys_initialized) {
         LOG(INFO) << "Already initialized";
         return true;
     }
 
     PolicyKeyRef device_ref;
-    if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication, device_key_path,
-                                              device_key_temp, &device_ref.key_raw_ref))
-        return false;
-    get_data_file_encryption_modes(&device_ref);
+    get_data_file_encryption_options(&device_ref);
 
-    std::string modestring = device_ref.contents_mode + ":" + device_ref.filenames_mode;
-    std::string mode_filename = std::string("/data") + fscrypt_key_mode;
-    if (!android::vold::writeStringToFile(modestring, mode_filename)) return false;
+    if (!android::vold::retrieveAndInstallKey(true, kEmptyAuthentication, device_key_path,
+                                              device_key_temp, "", device_ref.policy_version,
+                                              &device_ref.key_raw_ref))
+        return false;
+
+    std::string options_string =
+            StringPrintf("%s:%s:v%d", device_ref.contents_mode.c_str(),
+                         device_ref.filenames_mode.c_str(), device_ref.policy_version);
+    std::string options_filename = std::string("/data") + fscrypt_key_mode;
+    if (!android::vold::writeStringToFile(options_string, options_filename)) return false;
 
     std::string ref_filename = std::string("/data") + fscrypt_key_ref;
     if (!android::vold::writeStringToFile(device_ref.key_raw_ref, ref_filename)) return false;
-
     LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
 
+    KeyBuffer per_boot_key;
+    if (!android::vold::randomKey(&per_boot_key)) return false;
+    std::string per_boot_raw_ref;
+    if (!install_data_key(per_boot_key, &per_boot_raw_ref)) return false;
+    std::string per_boot_ref_filename = std::string("/data") + fscrypt_key_per_boot_ref;
+    if (!android::vold::writeStringToFile(per_boot_raw_ref, per_boot_ref_filename)) return false;
+    LOG(INFO) << "Wrote per boot key reference to:" << per_boot_ref_filename;
+
     if (!android::vold::FsyncDirectory(device_key_dir)) return false;
-    s_global_de_initialized = true;
+    s_systemwide_keys_initialized = true;
     return true;
 }
 
@@ -411,11 +470,23 @@
     return true;
 }
 
-static void drop_caches() {
-    // Clean any dirty pages (otherwise they won't be dropped).
+// "Lock" all encrypted directories whose key has been removed.  This is needed
+// in the case where the keys are being put in the session keyring (rather in
+// the newer filesystem-level keyrings), because removing a key from the session
+// keyring doesn't affect inodes in the kernel's inode cache whose per-file key
+// was already set up.  So to remove the per-file keys and make the files
+// "appear encrypted", these inodes must be evicted.
+//
+// To do this, sync() to clean all dirty inodes, then drop all reclaimable slab
+// objects systemwide.  This is overkill, but it's the best available method
+// currently.  Don't use drop_caches mode "3" because that also evicts pagecache
+// for in-use files; all files relevant here are already closed and sync'ed.
+static void drop_caches_if_needed() {
+    if (android::vold::isFsKeyringSupported()) {
+        return;
+    }
     sync();
-    // Drop inode and page caches.
-    if (!writeStringToFile("3", "/proc/sys/vm/drop_caches")) {
+    if (!writeStringToFile("2", "/proc/sys/vm/drop_caches")) {
         PLOG(ERROR) << "Failed to drop caches during key eviction";
     }
 }
@@ -426,8 +497,8 @@
     std::string raw_ref;
     // If we haven't loaded the CE key, no need to evict it.
     if (lookup_key_ref(s_ce_key_raw_refs, user_id, &raw_ref)) {
-        success &= android::vold::evictKey(raw_ref);
-        drop_caches();
+        success &= evict_data_key(raw_ref);
+        drop_caches_if_needed();
     }
     s_ce_key_raw_refs.erase(user_id);
     return success;
@@ -441,8 +512,7 @@
     bool success = true;
     std::string raw_ref;
     success &= evict_ce_key(user_id);
-    success &=
-        lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref) && android::vold::evictKey(raw_ref);
+    success &= lookup_key_ref(s_de_key_raw_refs, user_id, &raw_ref) && evict_data_key(raw_ref);
     s_de_key_raw_refs.erase(user_id);
     auto it = s_ephemeral_users.find(user_id);
     if (it != s_ephemeral_users.end()) {
@@ -532,14 +602,12 @@
         return false;
     }
     android::vold::KeyAuthentication auth("", secdiscardable_hash);
-    if (!android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
-                                              &key_ref->key_raw_ref))
-        return false;
-    key_ref->contents_mode =
-        android::base::GetProperty("ro.crypto.volume.contents_mode", "aes-256-xts");
-    key_ref->filenames_mode =
-        android::base::GetProperty("ro.crypto.volume.filenames_mode", "aes-256-heh");
-    return true;
+
+    if (!get_volume_file_encryption_options(key_ref)) return false;
+
+    return android::vold::retrieveAndInstallKey(true, auth, key_path, key_path + "_tmp",
+                                                volume_uuid, key_ref->policy_version,
+                                                &key_ref->key_raw_ref);
 }
 
 static bool destroy_volkey(const std::string& misc_path, const std::string& volume_uuid) {
@@ -687,7 +755,7 @@
             PolicyKeyRef de_ref;
             if (volume_uuid.empty()) {
                 if (!lookup_key_ref(s_de_key_raw_refs, user_id, &de_ref.key_raw_ref)) return false;
-                get_data_file_encryption_modes(&de_ref);
+                get_data_file_encryption_options(&de_ref);
                 if (!ensure_policy(de_ref, system_de_path)) return false;
                 if (!ensure_policy(de_ref, misc_de_path)) return false;
                 if (!ensure_policy(de_ref, vendor_de_path)) return false;
@@ -718,7 +786,7 @@
             PolicyKeyRef ce_ref;
             if (volume_uuid.empty()) {
                 if (!lookup_key_ref(s_ce_key_raw_refs, user_id, &ce_ref.key_raw_ref)) return false;
-                get_data_file_encryption_modes(&ce_ref);
+                get_data_file_encryption_options(&ce_ref);
                 if (!ensure_policy(ce_ref, system_ce_path)) return false;
                 if (!ensure_policy(ce_ref, misc_ce_path)) return false;
                 if (!ensure_policy(ce_ref, vendor_ce_path)) return false;
diff --git a/FsCrypt.h b/FsCrypt.h
index 16e2f9a..03ec2e1 100644
--- a/FsCrypt.h
+++ b/FsCrypt.h
@@ -18,7 +18,7 @@
 
 #include <cutils/multiuser.h>
 
-bool fscrypt_initialize_global_de();
+bool fscrypt_initialize_systemwide_keys();
 
 bool fscrypt_init_user0();
 bool fscrypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral);
diff --git a/IdleMaint.cpp b/IdleMaint.cpp
index bca22f6..2b5a8f1 100644
--- a/IdleMaint.cpp
+++ b/IdleMaint.cpp
@@ -29,8 +29,8 @@
 #include <android-base/strings.h>
 #include <android/hardware/health/storage/1.0/IStorage.h>
 #include <fs_mgr.h>
-#include <hardware_legacy/power.h>
 #include <private/android_filesystem_config.h>
+#include <wakelock/wakelock.h>
 
 #include <dirent.h>
 #include <fcntl.h>
@@ -145,7 +145,7 @@
 }
 
 void Trim(const android::sp<android::os::IVoldTaskListener>& listener) {
-    acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
+    android::wakelock::WakeLock wl{kWakeLock};
 
     // Collect both fstab and vold volumes
     std::list<std::string> paths;
@@ -195,7 +195,6 @@
         listener->onFinished(0, extras);
     }
 
-    release_wake_lock(kWakeLock);
 }
 
 static bool waitForGc(const std::list<std::string>& paths) {
@@ -370,7 +369,7 @@
 
     LOG(DEBUG) << "idle maintenance started";
 
-    acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
+    android::wakelock::WakeLock wl{kWakeLock};
 
     std::list<std::string> paths;
     addFromFstab(&paths, PathTypes::kBlkDevice);
@@ -400,13 +399,11 @@
 
     LOG(DEBUG) << "idle maintenance completed";
 
-    release_wake_lock(kWakeLock);
-
     return android::OK;
 }
 
 int AbortIdleMaint(const android::sp<android::os::IVoldTaskListener>& listener) {
-    acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
+    android::wakelock::WakeLock wl{kWakeLock};
 
     std::unique_lock<std::mutex> lk(cv_m);
     if (idle_maint_stat != IdleMaintStats::kStopped) {
@@ -424,8 +421,6 @@
         listener->onFinished(0, extras);
     }
 
-    release_wake_lock(kWakeLock);
-
     LOG(DEBUG) << "idle maintenance stopped";
 
     return android::OK;
diff --git a/KeyStorage.cpp b/KeyStorage.cpp
index 0290086..d5ac7d0 100644
--- a/KeyStorage.cpp
+++ b/KeyStorage.cpp
@@ -16,10 +16,10 @@
 
 #include "KeyStorage.h"
 
+#include "Checkpoint.h"
 #include "Keymaster.h"
 #include "ScryptParameters.h"
 #include "Utils.h"
-#include "Checkpoint.h"
 
 #include <thread>
 #include <vector>
@@ -37,8 +37,8 @@
 
 #include <android-base/file.h>
 #include <android-base/logging.h>
-#include <android-base/unique_fd.h>
 #include <android-base/properties.h>
+#include <android-base/unique_fd.h>
 
 #include <cutils/properties.h>
 
@@ -126,7 +126,13 @@
         paramBuilder.Authorization(km::TAG_USER_AUTH_TYPE, km::HardwareAuthenticatorType::PASSWORD);
         paramBuilder.Authorization(km::TAG_AUTH_TIMEOUT, AUTH_TIMEOUT);
     }
-    return keymaster.generateKey(paramBuilder, key);
+
+    auto paramsWithRollback = paramBuilder;
+    paramsWithRollback.Authorization(km::TAG_ROLLBACK_RESISTANCE);
+
+    // Generate rollback-resistant key if possible.
+    return keymaster.generateKey(paramsWithRollback, key) ||
+           keymaster.generateKey(paramBuilder, key);
 }
 
 static std::pair<km::AuthorizationSet, km::HardwareAuthToken> beginParams(
diff --git a/KeyUtil.cpp b/KeyUtil.cpp
index 12cae9b..f822377 100644
--- a/KeyUtil.cpp
+++ b/KeyUtil.cpp
@@ -16,12 +16,14 @@
 
 #include "KeyUtil.h"
 
-#include <linux/fs.h>
 #include <iomanip>
 #include <sstream>
 #include <string>
 
+#include <fcntl.h>
+#include <linux/fs.h>
 #include <openssl/sha.h>
+#include <sys/ioctl.h>
 
 #include <android-base/file.h>
 #include <android-base/logging.h>
@@ -29,6 +31,7 @@
 
 #include "KeyStorage.h"
 #include "Utils.h"
+#include "fscrypt_uapi.h"
 
 namespace android {
 namespace vold {
@@ -45,6 +48,42 @@
     return true;
 }
 
+// Return true if the kernel supports the ioctls to add/remove fscrypt keys
+// directly to/from the filesystem.
+bool isFsKeyringSupported(void) {
+    static bool initialized = false;
+    static bool supported;
+
+    if (!initialized) {
+        android::base::unique_fd fd(open("/data", O_RDONLY | O_DIRECTORY | O_CLOEXEC));
+
+        // FS_IOC_ADD_ENCRYPTION_KEY with a NULL argument will fail with ENOTTY
+        // if the ioctl isn't supported.  Otherwise it will fail with another
+        // error code such as EFAULT.
+        errno = 0;
+        (void)ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, NULL);
+        if (errno == ENOTTY) {
+            LOG(INFO) << "Kernel doesn't support FS_IOC_ADD_ENCRYPTION_KEY.  Falling back to "
+                         "session keyring";
+            supported = false;
+        } else {
+            if (errno != EFAULT) {
+                PLOG(WARNING) << "Unexpected error from FS_IOC_ADD_ENCRYPTION_KEY";
+            }
+            LOG(DEBUG) << "Detected support for FS_IOC_ADD_ENCRYPTION_KEY";
+            supported = true;
+        }
+        // There's no need to check for FS_IOC_REMOVE_ENCRYPTION_KEY, since it's
+        // guaranteed to be available if FS_IOC_ADD_ENCRYPTION_KEY is.  There's
+        // also no need to check for support on external volumes separately from
+        // /data, since either the kernel supports the ioctls on all
+        // fscrypt-capable filesystems or it doesn't.
+
+        initialized = true;
+    }
+    return supported;
+}
+
 // Get raw keyref - used to make keyname and to pass to ioctl
 static std::string generateKeyRef(const uint8_t* key, int length) {
     SHA512_CTX c;
@@ -78,16 +117,20 @@
 
 static char const* const NAME_PREFIXES[] = {"ext4", "f2fs", "fscrypt", nullptr};
 
-static std::string keyname(const std::string& prefix, const std::string& raw_ref) {
+static std::string keyrefstring(const std::string& raw_ref) {
     std::ostringstream o;
-    o << prefix << ":";
     for (unsigned char i : raw_ref) {
         o << std::hex << std::setw(2) << std::setfill('0') << (int)i;
     }
     return o.str();
 }
 
-// Get the keyring we store all keys in
+static std::string buildLegacyKeyName(const std::string& prefix, const std::string& raw_ref) {
+    return prefix + ":" + keyrefstring(raw_ref);
+}
+
+// Get the ID of the keyring we store all fscrypt keys in when the kernel is too
+// old to support FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY.
 static bool fscryptKeyring(key_serial_t* device_keyring) {
     *device_keyring = keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "fscrypt", 0);
     if (*device_keyring == -1) {
@@ -97,19 +140,17 @@
     return true;
 }
 
-// Install password into global keyring
-// Return raw key reference for use in policy
-bool installKey(const KeyBuffer& key, std::string* raw_ref) {
+// Add an encryption key to the legacy global session keyring.
+static bool installKeyLegacy(const KeyBuffer& key, const std::string& raw_ref) {
     // Place fscrypt_key into automatically zeroing buffer.
     KeyBuffer fsKeyBuffer(sizeof(fscrypt_key));
     fscrypt_key& fs_key = *reinterpret_cast<fscrypt_key*>(fsKeyBuffer.data());
 
     if (!fillKey(key, &fs_key)) return false;
-    *raw_ref = generateKeyRef(fs_key.raw, fs_key.size);
     key_serial_t device_keyring;
     if (!fscryptKeyring(&device_keyring)) return false;
     for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
-        auto ref = keyname(*name_prefix, *raw_ref);
+        auto ref = buildLegacyKeyName(*name_prefix, raw_ref);
         key_serial_t key_id =
             add_key("logon", ref.c_str(), (void*)&fs_key, sizeof(fs_key), device_keyring);
         if (key_id == -1) {
@@ -122,12 +163,110 @@
     return true;
 }
 
-bool evictKey(const std::string& raw_ref) {
+// Build a struct fscrypt_key_specifier for use in the key management ioctls.
+static bool buildKeySpecifier(fscrypt_key_specifier* spec, const std::string& raw_ref,
+                              int policy_version) {
+    switch (policy_version) {
+        case 1:
+            if (raw_ref.size() != FSCRYPT_KEY_DESCRIPTOR_SIZE) {
+                LOG(ERROR) << "Invalid key specifier size for v1 encryption policy: "
+                           << raw_ref.size();
+                return false;
+            }
+            spec->type = FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR;
+            memcpy(spec->u.descriptor, raw_ref.c_str(), FSCRYPT_KEY_DESCRIPTOR_SIZE);
+            return true;
+        case 2:
+            if (raw_ref.size() != FSCRYPT_KEY_IDENTIFIER_SIZE) {
+                LOG(ERROR) << "Invalid key specifier size for v2 encryption policy: "
+                           << raw_ref.size();
+                return false;
+            }
+            spec->type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
+            memcpy(spec->u.identifier, raw_ref.c_str(), FSCRYPT_KEY_IDENTIFIER_SIZE);
+            return true;
+        default:
+            LOG(ERROR) << "Invalid encryption policy version: " << policy_version;
+            return false;
+    }
+}
+
+// Install a file-based encryption key to the kernel, for use by encrypted files
+// on the specified filesystem using the specified encryption policy version.
+//
+// For v1 policies, we use FS_IOC_ADD_ENCRYPTION_KEY if the kernel supports it.
+// Otherwise we add the key to the legacy global session keyring.
+//
+// For v2 policies, we always use FS_IOC_ADD_ENCRYPTION_KEY; it's the only way
+// the kernel supports.
+//
+// Returns %true on success, %false on failure.  On success also sets *raw_ref
+// to the raw key reference for use in the encryption policy.
+bool installKey(const KeyBuffer& key, const std::string& mountpoint, int policy_version,
+                std::string* raw_ref) {
+    // Put the fscrypt_add_key_arg in an automatically-zeroing buffer, since we
+    // have to copy the raw key into it.
+    KeyBuffer arg_buf(sizeof(struct fscrypt_add_key_arg) + key.size(), 0);
+    struct fscrypt_add_key_arg* arg = (struct fscrypt_add_key_arg*)arg_buf.data();
+
+    // Initialize the "key specifier", which is like a name for the key.
+    switch (policy_version) {
+        case 1:
+            // A key for a v1 policy is specified by an arbitrary 8-byte
+            // "descriptor", which must be provided by userspace.  We use the
+            // first 8 bytes from the double SHA-512 of the key itself.
+            *raw_ref = generateKeyRef((const uint8_t*)key.data(), key.size());
+            if (!isFsKeyringSupported()) {
+                return installKeyLegacy(key, *raw_ref);
+            }
+            if (!buildKeySpecifier(&arg->key_spec, *raw_ref, policy_version)) {
+                return false;
+            }
+            break;
+        case 2:
+            // A key for a v2 policy is specified by an 16-byte "identifier",
+            // which is a cryptographic hash of the key itself which the kernel
+            // computes and returns.  Any user-provided value is ignored; we
+            // just need to set the specifier type to indicate that we're adding
+            // this type of key.
+            arg->key_spec.type = FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER;
+            break;
+        default:
+            LOG(ERROR) << "Invalid encryption policy version: " << policy_version;
+            return false;
+    }
+
+    // Provide the raw key.
+    arg->raw_size = key.size();
+    memcpy(arg->raw, key.data(), key.size());
+
+    android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
+    if (fd == -1) {
+        PLOG(ERROR) << "Failed to open " << mountpoint << " to install key";
+        return false;
+    }
+
+    if (ioctl(fd, FS_IOC_ADD_ENCRYPTION_KEY, arg) != 0) {
+        PLOG(ERROR) << "Failed to install fscrypt key to " << mountpoint;
+        return false;
+    }
+
+    if (arg->key_spec.type == FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER) {
+        // Retrieve the key identifier that the kernel computed.
+        *raw_ref = std::string((char*)arg->key_spec.u.identifier, FSCRYPT_KEY_IDENTIFIER_SIZE);
+    }
+    LOG(DEBUG) << "Installed fscrypt key with ref " << keyrefstring(*raw_ref) << " to "
+               << mountpoint;
+    return true;
+}
+
+// Remove an encryption key from the legacy global session keyring.
+static bool evictKeyLegacy(const std::string& raw_ref) {
     key_serial_t device_keyring;
     if (!fscryptKeyring(&device_keyring)) return false;
     bool success = true;
     for (char const* const* name_prefix = NAME_PREFIXES; *name_prefix != nullptr; name_prefix++) {
-        auto ref = keyname(*name_prefix, raw_ref);
+        auto ref = buildLegacyKeyName(*name_prefix, raw_ref);
         auto key_serial = keyctl_search(device_keyring, "logon", ref.c_str(), 0);
 
         // Unlink the key from the keyring.  Prefer unlinking to revoking or
@@ -144,8 +283,51 @@
     return success;
 }
 
+// Evict a file-based encryption key from the kernel.
+//
+// We use FS_IOC_REMOVE_ENCRYPTION_KEY if the kernel supports it.  Otherwise we
+// remove the key from the legacy global session keyring.
+//
+// In the latter case, the caller is responsible for dropping caches.
+bool evictKey(const std::string& mountpoint, const std::string& raw_ref, int policy_version) {
+    if (policy_version == 1 && !isFsKeyringSupported()) {
+        return evictKeyLegacy(raw_ref);
+    }
+
+    android::base::unique_fd fd(open(mountpoint.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC));
+    if (fd == -1) {
+        PLOG(ERROR) << "Failed to open " << mountpoint << " to evict key";
+        return false;
+    }
+
+    struct fscrypt_remove_key_arg arg;
+    memset(&arg, 0, sizeof(arg));
+
+    if (!buildKeySpecifier(&arg.key_spec, raw_ref, policy_version)) {
+        return false;
+    }
+
+    std::string ref = keyrefstring(raw_ref);
+
+    if (ioctl(fd, FS_IOC_REMOVE_ENCRYPTION_KEY, &arg) != 0) {
+        PLOG(ERROR) << "Failed to evict fscrypt key with ref " << ref << " from " << mountpoint;
+        return false;
+    }
+
+    LOG(DEBUG) << "Evicted fscrypt key with ref " << ref << " from " << mountpoint;
+    if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS) {
+        // Should never happen because keys are only added/removed as root.
+        LOG(ERROR) << "Unexpected case: key with ref " << ref << " is still added by other users!";
+    } else if (arg.removal_status_flags & FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY) {
+        LOG(ERROR) << "Files still open after removing key with ref " << ref
+                   << ".  These files were not locked!";
+    }
+    return true;
+}
+
 bool retrieveAndInstallKey(bool create_if_absent, const KeyAuthentication& key_authentication,
                            const std::string& key_path, const std::string& tmp_path,
+                           const std::string& volume_uuid, int policy_version,
                            std::string* key_ref) {
     KeyBuffer key;
     if (pathExists(key_path)) {
@@ -161,7 +343,7 @@
         if (!storeKeyAtomically(key_path, tmp_path, key_authentication, key)) return false;
     }
 
-    if (!installKey(key, key_ref)) {
+    if (!installKey(key, BuildDataPath(volume_uuid), policy_version, key_ref)) {
         LOG(ERROR) << "Failed to install key in " << key_path;
         return false;
     }
diff --git a/KeyUtil.h b/KeyUtil.h
index 7ee6725..f6799d9 100644
--- a/KeyUtil.h
+++ b/KeyUtil.h
@@ -27,10 +27,15 @@
 namespace vold {
 
 bool randomKey(KeyBuffer* key);
-bool installKey(const KeyBuffer& key, std::string* raw_ref);
-bool evictKey(const std::string& raw_ref);
+
+bool isFsKeyringSupported(void);
+
+bool installKey(const KeyBuffer& key, const std::string& mountpoint, int policy_version,
+                std::string* raw_ref);
+bool evictKey(const std::string& mountpoint, const std::string& raw_ref, int policy_version);
 bool retrieveAndInstallKey(bool create_if_absent, const KeyAuthentication& key_authentication,
                            const std::string& key_path, const std::string& tmp_path,
+                           const std::string& volume_uuid, int policy_version,
                            std::string* key_ref);
 bool retrieveKey(bool create_if_absent, const std::string& key_path, const std::string& tmp_path,
                  KeyBuffer* key, bool keepOld = true);
diff --git a/Loop.cpp b/Loop.cpp
index fa8f8ba..9fa876c 100644
--- a/Loop.cpp
+++ b/Loop.cpp
@@ -31,16 +31,20 @@
 
 #include <linux/kdev_t.h>
 
+#include <chrono>
+
 #include <android-base/logging.h>
 #include <android-base/stringprintf.h>
 #include <android-base/strings.h>
 #include <android-base/unique_fd.h>
+#include <fs_mgr/file_wait.h>
 #include <utils/Trace.h>
 
 #include "Loop.h"
 #include "VoldUtil.h"
 #include "sehandle.h"
 
+using namespace std::literals;
 using android::base::StringPrintf;
 using android::base::unique_fd;
 
@@ -74,6 +78,10 @@
         PLOG(ERROR) << "Failed to open " << target;
         return -errno;
     }
+    if (!android::fs_mgr::WaitForFile(out_device, 2s)) {
+        LOG(ERROR) << "Failed to find " << out_device;
+        return -ENOENT;
+    }
     unique_fd device_fd(open(out_device.c_str(), O_RDWR | O_CLOEXEC));
     if (device_fd.get() == -1) {
         PLOG(ERROR) << "Failed to open " << out_device;
diff --git a/MetadataCrypt.cpp b/MetadataCrypt.cpp
index 35cf3e2..abcf6db 100644
--- a/MetadataCrypt.cpp
+++ b/MetadataCrypt.cpp
@@ -23,19 +23,17 @@
 #include <vector>
 
 #include <fcntl.h>
-#include <sys/ioctl.h>
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 
-#include <linux/dm-ioctl.h>
-
 #include <android-base/file.h>
 #include <android-base/logging.h>
 #include <android-base/properties.h>
 #include <android-base/unique_fd.h>
 #include <cutils/fs.h>
 #include <fs_mgr.h>
+#include <libdm/dm.h>
 
 #include "Checkpoint.h"
 #include "EncryptInplace.h"
@@ -45,13 +43,12 @@
 #include "Utils.h"
 #include "VoldUtil.h"
 
-#define DM_CRYPT_BUF_SIZE 4096
 #define TABLE_LOAD_RETRIES 10
-#define DEFAULT_KEY_TARGET_TYPE "default-key"
 
 using android::fs_mgr::FstabEntry;
 using android::fs_mgr::GetEntryForMountPoint;
 using android::vold::KeyBuffer;
+using namespace android::dm;
 
 static const std::string kDmNameUserdata = "userdata";
 
@@ -146,16 +143,6 @@
 }  // namespace vold
 }  // namespace android
 
-static KeyBuffer default_key_params(const std::string& real_blkdev, const KeyBuffer& key) {
-    KeyBuffer hex_key;
-    if (android::vold::StrToHex(key, hex_key) != android::OK) {
-        LOG(ERROR) << "Failed to turn key to hex";
-        return KeyBuffer();
-    }
-    auto res = KeyBuffer() + "AES-256-XTS " + hex_key + " " + real_blkdev.c_str() + " 0";
-    return res;
-}
-
 static bool get_number_of_sectors(const std::string& real_blkdev, uint64_t* nr_sec) {
     if (android::vold::GetBlockDev512Sectors(real_blkdev, nr_sec) != android::OK) {
         PLOG(ERROR) << "Unable to measure size of " << real_blkdev;
@@ -164,86 +151,35 @@
     return true;
 }
 
-static struct dm_ioctl* dm_ioctl_init(char* buffer, size_t buffer_size, const std::string& dm_name) {
-    if (buffer_size < sizeof(dm_ioctl)) {
-        LOG(ERROR) << "dm_ioctl buffer too small";
-        return nullptr;
-    }
-
-    memset(buffer, 0, buffer_size);
-    struct dm_ioctl* io = (struct dm_ioctl*)buffer;
-    io->data_size = buffer_size;
-    io->data_start = sizeof(struct dm_ioctl);
-    io->version[0] = 4;
-    io->version[1] = 0;
-    io->version[2] = 0;
-    io->flags = 0;
-    dm_name.copy(io->name, sizeof(io->name));
-    return io;
-}
-
 static bool create_crypto_blk_dev(const std::string& dm_name, uint64_t nr_sec,
-                                  const std::string& target_type, const KeyBuffer& crypt_params,
-                                  std::string* crypto_blkdev) {
-    android::base::unique_fd dm_fd(
-        TEMP_FAILURE_RETRY(open("/dev/device-mapper", O_RDWR | O_CLOEXEC, 0)));
-    if (dm_fd == -1) {
-        PLOG(ERROR) << "Cannot open device-mapper";
+                                  const std::string& real_blkdev, const KeyBuffer& key,
+                                  std::string* crypto_blkdev, bool set_dun) {
+    auto& dm = DeviceMapper::Instance();
+
+    KeyBuffer hex_key_buffer;
+    if (android::vold::StrToHex(key, hex_key_buffer) != android::OK) {
+        LOG(ERROR) << "Failed to turn key to hex";
         return false;
     }
-    alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
-    auto io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
-    if (!io || ioctl(dm_fd.get(), DM_DEV_CREATE, io) != 0) {
-        PLOG(ERROR) << "Cannot create dm-crypt device " << dm_name;
-        return false;
-    }
+    std::string hex_key(hex_key_buffer.data(), hex_key_buffer.size());
 
-    // Get the device status, in particular, the name of its device file
-    io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
-    if (ioctl(dm_fd.get(), DM_DEV_STATUS, io) != 0) {
-        PLOG(ERROR) << "Cannot retrieve dm-crypt device status " << dm_name;
-        return false;
-    }
-    *crypto_blkdev = std::string() + "/dev/block/dm-" +
-                     std::to_string((io->dev & 0xff) | ((io->dev >> 12) & 0xfff00));
-
-    io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
-    size_t paramix = io->data_start + sizeof(struct dm_target_spec);
-    size_t nullix = paramix + crypt_params.size();
-    size_t endix = (nullix + 1 + 7) & 8;  // Add room for \0 and align to 8 byte boundary
-
-    if (endix > sizeof(buffer)) {
-        LOG(ERROR) << "crypt_params too big for DM_CRYPT_BUF_SIZE";
-        return false;
-    }
-
-    io->target_count = 1;
-    auto tgt = (struct dm_target_spec*)(buffer + io->data_start);
-    tgt->status = 0;
-    tgt->sector_start = 0;
-    tgt->length = nr_sec;
-    target_type.copy(tgt->target_type, sizeof(tgt->target_type));
-    memcpy(buffer + paramix, crypt_params.data(),
-           std::min(crypt_params.size(), sizeof(buffer) - paramix));
-    buffer[nullix] = '\0';
-    tgt->next = endix;
+    DmTable table;
+    table.Emplace<DmTargetDefaultKey>(0, nr_sec, "AES-256-XTS", hex_key, real_blkdev, 0, set_dun);
 
     for (int i = 0;; i++) {
-        if (ioctl(dm_fd.get(), DM_TABLE_LOAD, io) == 0) {
+        if (dm.CreateDevice(dm_name, table)) {
             break;
         }
         if (i + 1 >= TABLE_LOAD_RETRIES) {
-            PLOG(ERROR) << "DM_TABLE_LOAD ioctl failed";
+            LOG(ERROR) << "Could not create default-key device " << dm_name;
             return false;
         }
-        PLOG(INFO) << "DM_TABLE_LOAD ioctl failed, retrying";
+        PLOG(INFO) << "Could not create default-key device, retrying";
         usleep(500000);
     }
 
-    // Resume this device to activate it
-    io = dm_ioctl_init(buffer, sizeof(buffer), dm_name);
-    if (ioctl(dm_fd.get(), DM_DEV_SUSPEND, io)) {
-        PLOG(ERROR) << "Cannot resume dm-crypt device " << dm_name;
+    if (!dm.GetDmDevicePathByName(dm_name, crypto_blkdev)) {
+        LOG(ERROR) << "Cannot retrieve default-key device status " << dm_name;
         return false;
     }
     return true;
@@ -267,9 +203,14 @@
     if (!read_key(*data_rec, needs_encrypt, &key)) return false;
     uint64_t nr_sec;
     if (!get_number_of_sectors(data_rec->blk_device, &nr_sec)) return false;
+    bool set_dun = android::base::GetBoolProperty("ro.crypto.set_dun", false);
+    if (!set_dun && data_rec->fs_mgr_flags.checkpoint_blk) {
+        LOG(ERROR) << "Block checkpoints and metadata encryption require setdun option!";
+        return false;
+    }
+
     std::string crypto_blkdev;
-    if (!create_crypto_blk_dev(kDmNameUserdata, nr_sec, DEFAULT_KEY_TARGET_TYPE,
-                               default_key_params(blk_device, key), &crypto_blkdev))
+    if (!create_crypto_blk_dev(kDmNameUserdata, nr_sec, blk_device, key, &crypto_blkdev, set_dun))
         return false;
 
     // FIXME handle the corrupt case
diff --git a/MoveStorage.cpp b/MoveStorage.cpp
index 79a47ae..2447cce 100644
--- a/MoveStorage.cpp
+++ b/MoveStorage.cpp
@@ -21,8 +21,8 @@
 #include <android-base/logging.h>
 #include <android-base/properties.h>
 #include <android-base/stringprintf.h>
-#include <hardware_legacy/power.h>
 #include <private/android_filesystem_config.h>
+#include <wakelock/wakelock.h>
 
 #include <thread>
 
@@ -258,15 +258,13 @@
 
 void MoveStorage(const std::shared_ptr<VolumeBase>& from, const std::shared_ptr<VolumeBase>& to,
                  const android::sp<android::os::IVoldTaskListener>& listener) {
-    acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
+    android::wakelock::WakeLock wl{kWakeLock};
 
     android::os::PersistableBundle extras;
     status_t res = moveStorageInternal(from, to, listener);
     if (listener) {
         listener->onFinished(res, extras);
     }
-
-    release_wake_lock(kWakeLock);
 }
 
 }  // namespace vold
diff --git a/OWNERS b/OWNERS
index 4e45284..7779c20 100644
--- a/OWNERS
+++ b/OWNERS
@@ -1,3 +1,6 @@
 jsharkey@android.com
 paulcrowley@google.com
 paullawrence@google.com
+ebiggers@google.com
+drosen@google.com
+
diff --git a/Utils.cpp b/Utils.cpp
index df50658..1616d80 100644
--- a/Utils.cpp
+++ b/Utils.cpp
@@ -43,6 +43,7 @@
 #include <sys/sysmacros.h>
 #include <sys/types.h>
 #include <sys/wait.h>
+#include <unistd.h>
 
 #include <list>
 #include <mutex>
diff --git a/VoldNativeService.cpp b/VoldNativeService.cpp
index 1762b70..7f7f289 100644
--- a/VoldNativeService.cpp
+++ b/VoldNativeService.cpp
@@ -691,7 +691,7 @@
     ENFORCE_UID(AID_SYSTEM);
     ACQUIRE_CRYPT_LOCK;
 
-    return translateBool(fscrypt_initialize_global_de());
+    return translateBool(fscrypt_initialize_systemwide_keys());
 }
 
 binder::Status VoldNativeService::mountDefaultEncrypted() {
diff --git a/cryptfs.cpp b/cryptfs.cpp
index 07617e9..403282e 100644
--- a/cryptfs.cpp
+++ b/cryptfs.cpp
@@ -44,25 +44,24 @@
 #include <f2fs_sparseblock.h>
 #include <fs_mgr.h>
 #include <fscrypt/fscrypt.h>
-#include <hardware_legacy/power.h>
+#include <libdm/dm.h>
 #include <log/log.h>
 #include <logwrap/logwrap.h>
 #include <openssl/evp.h>
 #include <openssl/sha.h>
 #include <selinux/selinux.h>
+#include <wakelock/wakelock.h>
 
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <inttypes.h>
 #include <libgen.h>
-#include <linux/dm-ioctl.h>
 #include <linux/kdev_t.h>
 #include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/ioctl.h>
 #include <sys/mount.h>
 #include <sys/param.h>
 #include <sys/stat.h>
@@ -78,12 +77,11 @@
 using android::base::ParseUint;
 using android::base::StringPrintf;
 using android::fs_mgr::GetEntryForMountPoint;
+using namespace android::dm;
 using namespace std::chrono_literals;
 
 #define UNUSED __attribute__((unused))
 
-#define DM_CRYPT_BUF_SIZE 4096
-
 #define HASH_COUNT 2000
 
 constexpr size_t INTERMEDIATE_KEY_LEN_BYTES = 16;
@@ -253,19 +251,6 @@
     return;
 }
 
-static void ioctl_init(struct dm_ioctl* io, size_t dataSize, const char* name, unsigned flags) {
-    memset(io, 0, dataSize);
-    io->data_size = dataSize;
-    io->data_start = sizeof(struct dm_ioctl);
-    io->version[0] = 4;
-    io->version[1] = 0;
-    io->version[2] = 0;
-    io->flags = flags;
-    if (name) {
-        strlcpy(io->name, name, sizeof(io->name));
-    }
-}
-
 namespace {
 
 struct CryptoType;
@@ -973,109 +958,12 @@
     master_key_ascii[a] = '\0';
 }
 
-static int load_crypto_mapping_table(struct crypt_mnt_ftr* crypt_ftr,
-                                     const unsigned char* master_key, const char* real_blk_name,
-                                     const char* name, int fd, const char* extra_params) {
-    alignas(struct dm_ioctl) char buffer[DM_CRYPT_BUF_SIZE];
-    struct dm_ioctl* io;
-    struct dm_target_spec* tgt;
-    char* crypt_params;
-    // We need two ASCII characters to represent each byte, and need space for
-    // the '\0' terminator.
-    char master_key_ascii[MAX_KEY_LEN * 2 + 1];
-    size_t buff_offset;
-    int i;
-
-    io = (struct dm_ioctl*)buffer;
-
-    /* Load the mapping table for this device */
-    tgt = (struct dm_target_spec*)&buffer[sizeof(struct dm_ioctl)];
-
-    ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
-    io->target_count = 1;
-    tgt->status = 0;
-    tgt->sector_start = 0;
-    tgt->length = crypt_ftr->fs_size;
-    strlcpy(tgt->target_type, "crypt", DM_MAX_TYPE_NAME);
-
-    crypt_params = buffer + sizeof(struct dm_ioctl) + sizeof(struct dm_target_spec);
-    convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
-
-    buff_offset = crypt_params - buffer;
-    SLOGI(
-        "Creating crypto dev \"%s\"; cipher=%s, keysize=%u, real_dev=%s, len=%llu, params=\"%s\"\n",
-        name, crypt_ftr->crypto_type_name, crypt_ftr->keysize, real_blk_name, tgt->length * 512,
-        extra_params);
-    snprintf(crypt_params, sizeof(buffer) - buff_offset, "%s %s 0 %s 0 %s",
-             crypt_ftr->crypto_type_name, master_key_ascii, real_blk_name, extra_params);
-    crypt_params += strlen(crypt_params) + 1;
-    crypt_params =
-        (char*)(((unsigned long)crypt_params + 7) & ~8); /* Align to an 8 byte boundary */
-    tgt->next = crypt_params - buffer;
-
-    for (i = 0; i < TABLE_LOAD_RETRIES; i++) {
-        if (!ioctl(fd, DM_TABLE_LOAD, io)) {
-            break;
-        }
-        usleep(500000);
-    }
-
-    if (i == TABLE_LOAD_RETRIES) {
-        /* We failed to load the table, return an error */
-        return -1;
-    } else {
-        return i + 1;
-    }
-}
-
-static int get_dm_crypt_version(int fd, const char* name, int* version) {
-    char buffer[DM_CRYPT_BUF_SIZE];
-    struct dm_ioctl* io;
-    struct dm_target_versions* v;
-
-    io = (struct dm_ioctl*)buffer;
-
-    ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
-
-    if (ioctl(fd, DM_LIST_VERSIONS, io)) {
-        return -1;
-    }
-
-    /* Iterate over the returned versions, looking for name of "crypt".
-     * When found, get and return the version.
-     */
-    v = (struct dm_target_versions*)&buffer[sizeof(struct dm_ioctl)];
-    while (v->next) {
-        if (!strcmp(v->name, "crypt")) {
-            /* We found the crypt driver, return the version, and get out */
-            version[0] = v->version[0];
-            version[1] = v->version[1];
-            version[2] = v->version[2];
-            return 0;
-        }
-        v = (struct dm_target_versions*)(((char*)v) + v->next);
-    }
-
-    return -1;
-}
-
-static std::string extra_params_as_string(const std::vector<std::string>& extra_params_vec) {
-    if (extra_params_vec.empty()) return "";
-    std::string extra_params = std::to_string(extra_params_vec.size());
-    for (const auto& p : extra_params_vec) {
-        extra_params.append(" ");
-        extra_params.append(p);
-    }
-    return extra_params;
-}
-
 /*
  * If the ro.crypto.fde_sector_size system property is set, append the
  * parameters to make dm-crypt use the specified crypto sector size and round
  * the crypto device size down to a crypto sector boundary.
  */
-static int add_sector_size_param(std::vector<std::string>* extra_params_vec,
-                                 struct crypt_mnt_ftr* ftr) {
+static int add_sector_size_param(DmTargetCrypt* target, struct crypt_mnt_ftr* ftr) {
     constexpr char DM_CRYPT_SECTOR_SIZE[] = "ro.crypto.fde_sector_size";
     char value[PROPERTY_VALUE_MAX];
 
@@ -1089,12 +977,11 @@
             return -1;
         }
 
-        std::string param = StringPrintf("sector_size:%u", sector_size);
-        extra_params_vec->push_back(std::move(param));
+        target->SetSectorSize(sector_size);
 
         // With this option, IVs will match the sector numbering, instead
         // of being hard-coded to being based on 512-byte sectors.
-        extra_params_vec->emplace_back("iv_large_sectors");
+        target->SetIvLargeSectors();
 
         // Round the crypto device size down to a crypto sector boundary.
         ftr->fs_size &= ~((sector_size / 512) - 1);
@@ -1105,112 +992,67 @@
 static int create_crypto_blk_dev(struct crypt_mnt_ftr* crypt_ftr, const unsigned char* master_key,
                                  const char* real_blk_name, char* crypto_blk_name, const char* name,
                                  uint32_t flags) {
-    char buffer[DM_CRYPT_BUF_SIZE];
-    struct dm_ioctl* io;
-    unsigned int minor;
-    int fd = 0;
-    int err;
-    int retval = -1;
-    int version[3];
-    int load_count;
-    std::vector<std::string> extra_params_vec;
+    auto& dm = DeviceMapper::Instance();
 
-    if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
-        SLOGE("Cannot open device-mapper\n");
-        goto errout;
-    }
+    // We need two ASCII characters to represent each byte, and need space for
+    // the '\0' terminator.
+    char master_key_ascii[MAX_KEY_LEN * 2 + 1];
+    convert_key_to_hex_ascii(master_key, crypt_ftr->keysize, master_key_ascii);
 
-    io = (struct dm_ioctl*)buffer;
+    auto target = std::make_unique<DmTargetCrypt>(0, crypt_ftr->fs_size,
+                                                  (const char*)crypt_ftr->crypto_type_name,
+                                                  master_key_ascii, 0, real_blk_name, 0);
+    target->AllowDiscards();
 
-    ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
-    err = ioctl(fd, DM_DEV_CREATE, io);
-    if (err) {
-        SLOGE("Cannot create dm-crypt device %s: %s\n", name, strerror(errno));
-        goto errout;
-    }
-
-    /* Get the device status, in particular, the name of it's device file */
-    ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
-    if (ioctl(fd, DM_DEV_STATUS, io)) {
-        SLOGE("Cannot retrieve dm-crypt device status\n");
-        goto errout;
-    }
-    minor = (io->dev & 0xff) | ((io->dev >> 12) & 0xfff00);
-    snprintf(crypto_blk_name, MAXPATHLEN, "/dev/block/dm-%u", minor);
-
-    if (!get_dm_crypt_version(fd, name, version)) {
-        /* Support for allow_discards was added in version 1.11.0 */
-        if ((version[0] >= 2) || ((version[0] == 1) && (version[1] >= 11))) {
-            extra_params_vec.emplace_back("allow_discards");
-        }
-    }
     if (flags & CREATE_CRYPTO_BLK_DEV_FLAGS_ALLOW_ENCRYPT_OVERRIDE) {
-        extra_params_vec.emplace_back("allow_encrypt_override");
+        target->AllowEncryptOverride();
     }
-    if (add_sector_size_param(&extra_params_vec, crypt_ftr)) {
+    if (add_sector_size_param(target.get(), crypt_ftr)) {
         SLOGE("Error processing dm-crypt sector size param\n");
-        goto errout;
+        return -1;
     }
-    load_count = load_crypto_mapping_table(crypt_ftr, master_key, real_blk_name, name, fd,
-                                           extra_params_as_string(extra_params_vec).c_str());
-    if (load_count < 0) {
+
+    DmTable table;
+    table.AddTarget(std::move(target));
+
+    int load_count = 1;
+    while (load_count < TABLE_LOAD_RETRIES) {
+        if (dm.CreateDevice(name, table)) {
+            break;
+        }
+        load_count++;
+    }
+
+    if (load_count >= TABLE_LOAD_RETRIES) {
         SLOGE("Cannot load dm-crypt mapping table.\n");
-        goto errout;
-    } else if (load_count > 1) {
+        return -1;
+    }
+    if (load_count > 1) {
         SLOGI("Took %d tries to load dmcrypt table.\n", load_count);
     }
 
-    /* Resume this device to activate it */
-    ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
-
-    if (ioctl(fd, DM_DEV_SUSPEND, io)) {
-        SLOGE("Cannot resume the dm-crypt device\n");
-        goto errout;
+    std::string path;
+    if (!dm.GetDmDevicePathByName(name, &path)) {
+        SLOGE("Cannot determine dm-crypt path for %s.\n", name);
+        return -1;
     }
+    snprintf(crypto_blk_name, MAXPATHLEN, "%s", path.c_str());
 
     /* Ensure the dm device has been created before returning. */
     if (android::vold::WaitForFile(crypto_blk_name, 1s) < 0) {
         // WaitForFile generates a suitable log message
-        goto errout;
+        return -1;
     }
-
-    /* We made it here with no errors.  Woot! */
-    retval = 0;
-
-errout:
-    close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
-
-    return retval;
+    return 0;
 }
 
-static int delete_crypto_blk_dev(const char* name) {
-    int fd;
-    char buffer[DM_CRYPT_BUF_SIZE];
-    struct dm_ioctl* io;
-    int retval = -1;
-    int err;
-
-    if ((fd = open("/dev/device-mapper", O_RDWR | O_CLOEXEC)) < 0) {
-        SLOGE("Cannot open device-mapper\n");
-        goto errout;
+static int delete_crypto_blk_dev(const std::string& name) {
+    auto& dm = DeviceMapper::Instance();
+    if (!dm.DeleteDevice(name)) {
+        SLOGE("Cannot remove dm-crypt device %s: %s\n", name.c_str(), strerror(errno));
+        return -1;
     }
-
-    io = (struct dm_ioctl*)buffer;
-
-    ioctl_init(io, DM_CRYPT_BUF_SIZE, name, 0);
-    err = ioctl(fd, DM_DEV_REMOVE, io);
-    if (err) {
-        SLOGE("Cannot remove dm-crypt device %s: %s\n", name, strerror(errno));
-        goto errout;
-    }
-
-    /* We made it here with no errors.  Woot! */
-    retval = 0;
-
-errout:
-    close(fd); /* If fd is <0 from a failed open call, it's safe to just ignore the close error */
-
-    return retval;
+    return 0;
 }
 
 static int pbkdf2(const char* passwd, const unsigned char* salt, unsigned char* ikey,
@@ -1924,7 +1766,7 @@
  * storage volume.
  */
 int cryptfs_revert_ext_volume(const char* label) {
-    return delete_crypto_blk_dev((char*)label);
+    return delete_crypto_blk_dev(label);
 }
 
 int cryptfs_crypto_complete(void) {
@@ -2165,6 +2007,7 @@
     off64_t previously_encrypted_upto = 0;
     bool rebootEncryption = false;
     bool onlyCreateHeader = false;
+    std::unique_ptr<android::wakelock::WakeLock> wakeLock = nullptr;
 
     if (get_crypt_ftr_and_key(&crypt_ftr) == 0) {
         if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
@@ -2231,7 +2074,7 @@
      * wants to keep the screen on, it can grab a full wakelock.
      */
     snprintf(lockid, sizeof(lockid), "enablecrypto%d", (int)getpid());
-    acquire_wake_lock(PARTIAL_WAKE_LOCK, lockid);
+    wakeLock = std::make_unique<android::wakelock::WakeLock>(lockid);
 
     /* The init files are setup to stop the class main and late start when
      * vold sets trigger_shutdown_framework.
@@ -2264,6 +2107,7 @@
          * /data, set a property saying we're doing inplace encryption,
          * and restart the framework.
          */
+        wait_and_unmount(DATA_MNT_POINT, true);
         if (fs_mgr_do_tmpfs_mount(DATA_MNT_POINT)) {
             goto error_shutting_down;
         }
@@ -2411,7 +2255,7 @@
                 /* default encryption - continue first boot sequence */
                 property_set("ro.crypto.state", "encrypted");
                 property_set("ro.crypto.type", "block");
-                release_wake_lock(lockid);
+                wakeLock.reset(nullptr);
                 if (rebootEncryption && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
                     // Bring up cryptkeeper that will check the password and set it
                     property_set("vold.decrypt", "trigger_shutdown_framework");
@@ -2448,7 +2292,6 @@
         } else {
             /* set property to trigger dialog */
             property_set("vold.encrypt_progress", "error_partially_encrypted");
-            release_wake_lock(lockid);
         }
         return -1;
     }
@@ -2458,14 +2301,10 @@
      * Set the property and return.  Hope the framework can deal with it.
      */
     property_set("vold.encrypt_progress", "error_reboot_failed");
-    release_wake_lock(lockid);
     return rc;
 
 error_unencrypted:
     property_set("vold.encrypt_progress", "error_not_encrypted");
-    if (lockid[0]) {
-        release_wake_lock(lockid);
-    }
     return -1;
 
 error_shutting_down:
@@ -2480,9 +2319,6 @@
 
     /* shouldn't get here */
     property_set("vold.encrypt_progress", "error_shutting_down");
-    if (lockid[0]) {
-        release_wake_lock(lockid);
-    }
     return -1;
 }
 
diff --git a/fscrypt_uapi.h b/fscrypt_uapi.h
new file mode 100644
index 0000000..3999036
--- /dev/null
+++ b/fscrypt_uapi.h
@@ -0,0 +1,48 @@
+#ifndef _UAPI_LINUX_FSCRYPT_H
+#define _UAPI_LINUX_FSCRYPT_H
+
+// Definitions for FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY
+
+// TODO: switch to <linux/fscrypt.h> once it's in Bionic
+
+#ifndef FS_IOC_ADD_ENCRYPTION_KEY
+
+#include <linux/types.h>
+
+#define FSCRYPT_KEY_DESCRIPTOR_SIZE 8
+#define FSCRYPT_KEY_IDENTIFIER_SIZE 16
+
+#define FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR 1
+#define FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER 2
+
+struct fscrypt_key_specifier {
+    __u32 type;
+    __u32 __reserved;
+    union {
+        __u8 __reserved[32];
+        __u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
+        __u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
+    } u;
+};
+
+struct fscrypt_add_key_arg {
+    struct fscrypt_key_specifier key_spec;
+    __u32 raw_size;
+    __u32 __reserved[9];
+    __u8 raw[];
+};
+
+struct fscrypt_remove_key_arg {
+    struct fscrypt_key_specifier key_spec;
+#define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY 0x00000001
+#define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS 0x00000002
+    __u32 removal_status_flags;
+    __u32 __reserved[5];
+};
+
+#define FS_IOC_ADD_ENCRYPTION_KEY _IOWR('f', 23, struct fscrypt_add_key_arg)
+#define FS_IOC_REMOVE_ENCRYPTION_KEY _IOWR('f', 24, struct fscrypt_remove_key_arg)
+
+#endif /* FS_IOC_ADD_ENCRYPTION_KEY */
+
+#endif /* _UAPI_LINUX_FSCRYPT_H */
diff --git a/main.cpp b/main.cpp
index 27a701b..7555276 100644
--- a/main.cpp
+++ b/main.cpp
@@ -152,6 +152,7 @@
         {"blkid_untrusted_context", required_argument, 0, 'B'},
         {"fsck_context", required_argument, 0, 'f'},
         {"fsck_untrusted_context", required_argument, 0, 'F'},
+        {nullptr, 0, nullptr, 0},
     };
 
     int c;
diff --git a/vdc.cpp b/vdc.cpp
index f05d2af..839e70e 100644
--- a/vdc.cpp
+++ b/vdc.cpp
@@ -32,6 +32,7 @@
 
 #include <android-base/logging.h>
 #include <android-base/parseint.h>
+#include <android-base/strings.h>
 #include <android-base/stringprintf.h>
 #include <binder/IServiceManager.h>
 #include <binder/Status.h>
@@ -55,9 +56,10 @@
     return res;
 }
 
-static void checkStatus(android::binder::Status status) {
+static void checkStatus(std::vector<std::string>& cmd, android::binder::Status status) {
     if (status.isOk()) return;
-    LOG(ERROR) << "Failed: " << status.toString8().string();
+    std::string command = ::android::base::Join(cmd, " ");
+    LOG(ERROR) << "Command: " << command << " Failed: " << status.toString8().string();
     exit(ENOTTY);
 }
 
@@ -88,63 +90,63 @@
     auto vold = android::interface_cast<android::os::IVold>(binder);
 
     if (args[0] == "cryptfs" && args[1] == "enablefilecrypto") {
-        checkStatus(vold->fbeEnable());
+        checkStatus(args, vold->fbeEnable());
     } else if (args[0] == "cryptfs" && args[1] == "init_user0") {
-        checkStatus(vold->initUser0());
+        checkStatus(args, vold->initUser0());
     } else if (args[0] == "cryptfs" && args[1] == "enablecrypto") {
         int passwordType = android::os::IVold::PASSWORD_TYPE_DEFAULT;
         int encryptionFlags = android::os::IVold::ENCRYPTION_FLAG_NO_UI;
-        checkStatus(vold->fdeEnable(passwordType, "", encryptionFlags));
+        checkStatus(args, vold->fdeEnable(passwordType, "", encryptionFlags));
     } else if (args[0] == "cryptfs" && args[1] == "mountdefaultencrypted") {
-        checkStatus(vold->mountDefaultEncrypted());
+        checkStatus(args, vold->mountDefaultEncrypted());
     } else if (args[0] == "volume" && args[1] == "shutdown") {
-        checkStatus(vold->shutdown());
+        checkStatus(args, vold->shutdown());
     } else if (args[0] == "cryptfs" && args[1] == "checkEncryption" && args.size() == 3) {
-        checkStatus(vold->checkEncryption(args[2]));
+        checkStatus(args, vold->checkEncryption(args[2]));
     } else if (args[0] == "cryptfs" && args[1] == "mountFstab" && args.size() == 4) {
-        checkStatus(vold->mountFstab(args[2], args[3]));
+        checkStatus(args, vold->mountFstab(args[2], args[3]));
     } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 4) {
-        checkStatus(vold->encryptFstab(args[2], args[3]));
+        checkStatus(args, vold->encryptFstab(args[2], args[3]));
     } else if (args[0] == "checkpoint" && args[1] == "supportsCheckpoint" && args.size() == 2) {
         bool supported = false;
-        checkStatus(vold->supportsCheckpoint(&supported));
+        checkStatus(args, vold->supportsCheckpoint(&supported));
         return supported ? 1 : 0;
     } else if (args[0] == "checkpoint" && args[1] == "supportsBlockCheckpoint" && args.size() == 2) {
         bool supported = false;
-        checkStatus(vold->supportsBlockCheckpoint(&supported));
+        checkStatus(args, vold->supportsBlockCheckpoint(&supported));
         return supported ? 1 : 0;
     } else if (args[0] == "checkpoint" && args[1] == "supportsFileCheckpoint" && args.size() == 2) {
         bool supported = false;
-        checkStatus(vold->supportsFileCheckpoint(&supported));
+        checkStatus(args, vold->supportsFileCheckpoint(&supported));
         return supported ? 1 : 0;
     } else if (args[0] == "checkpoint" && args[1] == "startCheckpoint" && args.size() == 3) {
         int retry;
         if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
-        checkStatus(vold->startCheckpoint(retry));
+        checkStatus(args, vold->startCheckpoint(retry));
     } else if (args[0] == "checkpoint" && args[1] == "needsCheckpoint" && args.size() == 2) {
         bool enabled = false;
-        checkStatus(vold->needsCheckpoint(&enabled));
+        checkStatus(args, vold->needsCheckpoint(&enabled));
         return enabled ? 1 : 0;
     } else if (args[0] == "checkpoint" && args[1] == "needsRollback" && args.size() == 2) {
         bool enabled = false;
-        checkStatus(vold->needsRollback(&enabled));
+        checkStatus(args, vold->needsRollback(&enabled));
         return enabled ? 1 : 0;
     } else if (args[0] == "checkpoint" && args[1] == "commitChanges" && args.size() == 2) {
-        checkStatus(vold->commitChanges());
+        checkStatus(args, vold->commitChanges());
     } else if (args[0] == "checkpoint" && args[1] == "prepareCheckpoint" && args.size() == 2) {
-        checkStatus(vold->prepareCheckpoint());
+        checkStatus(args, vold->prepareCheckpoint());
     } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpoint" && args.size() == 3) {
-        checkStatus(vold->restoreCheckpoint(args[2]));
+        checkStatus(args, vold->restoreCheckpoint(args[2]));
     } else if (args[0] == "checkpoint" && args[1] == "restoreCheckpointPart" && args.size() == 4) {
         int count;
         if (!android::base::ParseInt(args[3], &count)) exit(EINVAL);
-        checkStatus(vold->restoreCheckpointPart(args[2], count));
+        checkStatus(args, vold->restoreCheckpointPart(args[2], count));
     } else if (args[0] == "checkpoint" && args[1] == "markBootAttempt" && args.size() == 2) {
-        checkStatus(vold->markBootAttempt());
+        checkStatus(args, vold->markBootAttempt());
     } else if (args[0] == "checkpoint" && args[1] == "abortChanges" && args.size() == 4) {
         int retry;
         if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
-        checkStatus(vold->abortChanges(args[2], retry != 0));
+        checkStatus(args, vold->abortChanges(args[2], retry != 0));
     } else {
         LOG(ERROR) << "Raw commands are no longer supported";
         exit(EINVAL);