resolve merge conflicts of 2b6f9ce823 to master.

Change-Id: I69f36f560334b11b099f2eb15999603dd2469d4f
diff --git a/Android.mk b/Android.mk
index 8c0771d..bea18b2 100644
--- a/Android.mk
+++ b/Android.mk
@@ -27,6 +27,8 @@
 	MoveTask.cpp \
 	Benchmark.cpp \
 	TrimTask.cpp \
+	Keymaster.cpp \
+	KeyStorage.cpp \
 	secontext.cpp \
 
 common_c_includes := \
@@ -53,7 +55,8 @@
 	libutils \
 	libhardware \
 	libsoftkeymaster \
-	libbase
+	libbase \
+	libkeymaster_messages \
 
 common_static_libraries := \
 	libfs_mgr \
@@ -62,7 +65,7 @@
 	libsquashfs_utils \
 	libscrypt_static \
 	libmincrypt \
-	libbatteryservice
+	libbatteryservice \
 
 vold_conlyflags := -std=c11
 vold_cflags := -Werror -Wall -Wno-missing-field-initializers -Wno-unused-variable -Wno-unused-parameter
@@ -127,7 +130,7 @@
 LOCAL_CLANG := true
 LOCAL_SRC_FILES:= secdiscard.cpp
 LOCAL_MODULE:= secdiscard
-LOCAL_SHARED_LIBRARIES := libcutils
+LOCAL_SHARED_LIBRARIES := libbase
 LOCAL_CFLAGS := $(vold_cflags)
 LOCAL_CONLYFLAGS := $(vold_conlyflags)
 
diff --git a/AutoCloseFD.h b/AutoCloseFD.h
new file mode 100644
index 0000000..9b68469
--- /dev/null
+++ b/AutoCloseFD.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
+#include <string>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <android-base/logging.h>
+
+// File descriptor which is automatically closed when this object is destroyed.
+// Cannot be copied, since that would cause double-closes.
+class AutoCloseFD {
+public:
+    AutoCloseFD(const char *path, int flags = O_RDONLY, int mode = 0):
+        fd{TEMP_FAILURE_RETRY(open(path, flags | O_CLOEXEC, mode))} {}
+    AutoCloseFD(const std::string &path, int flags = O_RDONLY, int mode = 0):
+        AutoCloseFD(path.c_str(), flags, mode) {}
+    ~AutoCloseFD() {
+        if (fd != -1) {
+            int preserve_errno = errno;
+            if (close(fd) == -1) {
+                PLOG(ERROR) << "close(2) failed";
+            };
+            errno = preserve_errno;
+        }
+    }
+    AutoCloseFD(const AutoCloseFD&) = delete;
+    AutoCloseFD& operator=(const AutoCloseFD&) = delete;
+    explicit operator bool() {return fd != -1;}
+    int get() const {return fd;}
+private:
+    const int fd;
+};
+
diff --git a/CommandListener.cpp b/CommandListener.cpp
index 4a8ed75..edbb776 100644
--- a/CommandListener.cpp
+++ b/CommandListener.cpp
@@ -15,7 +15,9 @@
  */
 
 #include <stdlib.h>
+#include <sys/mount.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
 #include <sys/types.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -27,6 +29,7 @@
 #include <string.h>
 #include <stdint.h>
 #include <inttypes.h>
+#include <ctype.h>
 
 #define LOG_TAG "VoldCmdListener"
 
@@ -44,8 +47,6 @@
 #include "Process.h"
 #include "Loop.h"
 #include "Devmapper.h"
-#include "Ext4Crypt.h"
-#include "cryptfs.h"
 #include "MoveTask.h"
 #include "TrimTask.h"
 
@@ -59,6 +60,7 @@
     registerCmd(new ObbCmd());
     registerCmd(new StorageCmd());
     registerCmd(new FstrimCmd());
+    registerCmd(new AppFuseCmd());
 }
 
 #if DUMP_ARGS
@@ -621,3 +623,143 @@
     (new android::vold::TrimTask(flags))->start();
     return sendGenericOkFail(cli, 0);
 }
+
+static size_t kAppFuseMaxMountPointName = 32;
+
+static bool isValidAppFuseMountName(const std::string& name) {
+    if (name.size() > kAppFuseMaxMountPointName) {
+        return false;
+    }
+    for (size_t i = 0; i < name.size(); i++) {
+        if (!isalnum(name[i])) {
+            return false;
+        }
+    }
+    return true;
+}
+
+CommandListener::AppFuseCmd::AppFuseCmd() : VoldCommand("appfuse") {}
+
+int CommandListener::AppFuseCmd::runCommand(SocketClient *cli,
+                                            int argc,
+                                            char **argv) {
+    if (argc < 2) {
+        cli->sendMsg(
+            ResponseCode::CommandSyntaxError, "Missing argument", false);
+        return 0;
+    }
+
+    const std::string command(argv[1]);
+
+    if (command == "mount" && argc == 4) {
+        const uid_t uid = atoi(argv[2]);
+        const std::string name(argv[3]);
+
+        // Check mount point name.
+        if (!isValidAppFuseMountName(name)) {
+            return cli->sendMsg(
+                    ResponseCode::CommandParameterError,
+                    "Invalid mount point name.",
+                    false);
+        }
+
+        // Create directories.
+        char path[PATH_MAX];
+        {
+            snprintf(path, PATH_MAX, "/mnt/appfuse/%d_%s", uid, name.c_str());
+            umount2(path, UMOUNT_NOFOLLOW | MNT_DETACH);
+            const int result = android::vold::PrepareDir(path, 0700, 0, 0);
+            if (result != 0) {
+                return sendGenericOkFail(cli, result);
+            }
+        }
+
+        // Open device FD.
+        const int device_fd = open("/dev/fuse", O_RDWR);
+        if (device_fd < 0) {
+            sendGenericOkFail(cli, device_fd);
+            return 0;
+        }
+
+        // Mount.
+        {
+            char opts[256];
+            snprintf(
+                    opts,
+                    sizeof(opts),
+                    "fd=%i,"
+                    "rootmode=40000,"
+                    "default_permissions,"
+                    "allow_other,"
+                    "user_id=%d,group_id=%d",
+                    device_fd,
+                    uid,
+                    uid);
+            // TODO: Make it bound mount in application namespace.
+            // TODO: Add context= option to opts.
+            const int result = mount(
+                    "/dev/fuse", path, "fuse",
+                    MS_NOSUID | MS_NODEV | MS_NOEXEC | MS_NOATIME, opts);
+            if (result != 0) {
+                sendGenericOkFail(cli, 1);
+                return 0;
+            }
+        }
+
+        const int result = sendFd(cli, device_fd);
+        close(device_fd);
+        return result;
+    } else if (command == "unmount" && argc == 4) {
+        const uid_t uid = atoi(argv[2]);
+        const std::string name(argv[3]);
+
+        // Check mount point name.
+        if (!isValidAppFuseMountName(name)) {
+            return cli->sendMsg(
+                    ResponseCode::CommandParameterError,
+                    "Invalid mount point name.",
+                    false);
+        }
+
+        // Unmount directory.
+        char path[PATH_MAX];
+        snprintf(path, PATH_MAX, "/mnt/appfuse/%d_%s", uid, name.c_str());
+        umount2(path, UMOUNT_NOFOLLOW | MNT_DETACH);
+
+        return sendGenericOkFail(cli, /* success */ 0);
+    }
+
+    return cli->sendMsg(
+            ResponseCode::CommandSyntaxError,  "Unknown appfuse cmd", false);
+}
+
+int CommandListener::AppFuseCmd::sendFd(SocketClient *cli, int fd) {
+    struct iovec data;
+    char dataBuffer[128];
+    char controlBuffer[CMSG_SPACE(sizeof(int))];
+    struct msghdr message;
+
+    // Message.
+    memset(&message, 0, sizeof(struct msghdr));
+    message.msg_iov = &data;
+    message.msg_iovlen = 1;
+    message.msg_control = controlBuffer;
+    message.msg_controllen = CMSG_SPACE(sizeof(int));
+
+    // Data.
+    data.iov_base = dataBuffer;
+    data.iov_len = snprintf(dataBuffer,
+                            sizeof(dataBuffer),
+                            "200 %d AppFuse command succeeded",
+                            cli->getCmdNum()) + 1;
+
+    // Control.
+    struct cmsghdr* const controlMessage = CMSG_FIRSTHDR(&message);
+    memset(controlBuffer, 0, CMSG_SPACE(sizeof(int)));
+    controlMessage->cmsg_level = SOL_SOCKET;
+    controlMessage->cmsg_type = SCM_RIGHTS;
+    controlMessage->cmsg_len = CMSG_LEN(sizeof(int));
+    *((int *) CMSG_DATA(controlMessage)) = fd;
+
+    return TEMP_FAILURE_RETRY(sendmsg(cli->getSocket(), &message, 0));
+}
diff --git a/CommandListener.h b/CommandListener.h
index 6ed099b..aede465 100644
--- a/CommandListener.h
+++ b/CommandListener.h
@@ -73,6 +73,15 @@
         virtual ~FstrimCmd() {}
         int runCommand(SocketClient *c, int argc, char ** argv);
     };
+
+    class AppFuseCmd : public VoldCommand {
+    public:
+        AppFuseCmd();
+        virtual ~AppFuseCmd() {}
+        int runCommand(SocketClient *c, int argc, char ** argv);
+    private:
+        int sendFd(SocketClient *c, int fd);
+    };
 };
 
 #endif
diff --git a/CryptCommandListener.cpp b/CryptCommandListener.cpp
index 1babef7..2eac60e 100644
--- a/CryptCommandListener.cpp
+++ b/CryptCommandListener.cpp
@@ -16,6 +16,7 @@
 
 #include <stdlib.h>
 #include <sys/socket.h>
+#include <sys/stat.h>
 #include <sys/types.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
@@ -28,9 +29,13 @@
 #include <stdint.h>
 #include <inttypes.h>
 
+#include <algorithm>
+
 #define LOG_TAG "VoldCryptCmdListener"
 
+#include <android-base/logging.h>
 #include <android-base/stringprintf.h>
+
 #include <cutils/fs.h>
 #include <cutils/log.h>
 #include <cutils/sockets.h>
@@ -43,6 +48,7 @@
 #include "ResponseCode.h"
 #include "cryptfs.h"
 #include "Ext4Crypt.h"
+#include "Utils.h"
 
 #define DUMP_ARGS 0
 
@@ -110,6 +116,34 @@
     }
 }
 
+static char* parseNull(char* arg) {
+    if (strcmp(arg, "!") == 0) {
+        return nullptr;
+    } else {
+        return arg;
+    }
+}
+
+static bool check_argc(SocketClient *cli, const std::string &subcommand, int argc,
+        int expected, std::string usage) {
+    assert(expected >= 2);
+    if (expected == 2) {
+        assert(usage.empty());
+    } else {
+        assert(!usage.empty());
+        assert(std::count(usage.begin(), usage.end(), ' ') + 3 == expected);
+    }
+    if (argc == expected) {
+        return true;
+    }
+    auto message = std::string() + "Usage: cryptfs " + subcommand;
+    if (!usage.empty()) {
+        message += " " + usage;
+    }
+    cli->sendMsg(ResponseCode::CommandSyntaxError, message.c_str(), false);
+    return false;
+}
+
 int CryptCommandListener::CryptfsCmd::runCommand(SocketClient *cli,
                                                  int argc, char **argv) {
     if ((cli->getUid() != 0) && (cli->getUid() != AID_SYSTEM)) {
@@ -118,34 +152,26 @@
     }
 
     if (argc < 2) {
-        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing Argument", false);
+        cli->sendMsg(ResponseCode::CommandSyntaxError, "Missing subcommand", false);
         return 0;
     }
 
     int rc = 0;
 
-    if (!strcmp(argv[1], "checkpw")) {
-        if (argc != 3) {
-            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs checkpw <passwd>", false);
-            return 0;
-        }
+    std::string subcommand(argv[1]);
+    if (subcommand == "checkpw") {
+        if (!check_argc(cli, subcommand, argc, 3, "<passwd>")) return 0;
         dumpArgs(argc, argv, 2);
         rc = cryptfs_check_passwd(argv[2]);
-    } else if (!strcmp(argv[1], "restart")) {
-        if (argc != 2) {
-            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs restart", false);
-            return 0;
-        }
+    } else if (subcommand == "restart") {
+        if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
         dumpArgs(argc, argv, -1);
         rc = cryptfs_restart();
-    } else if (!strcmp(argv[1], "cryptocomplete")) {
-        if (argc != 2) {
-            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs cryptocomplete", false);
-            return 0;
-        }
+    } else if (subcommand == "cryptocomplete") {
+        if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
         dumpArgs(argc, argv, -1);
         rc = cryptfs_crypto_complete();
-    } else if (!strcmp(argv[1], "enablecrypto")) {
+    } else if (subcommand == "enablecrypto") {
         const char* syntax = "Usage: cryptfs enablecrypto <wipe|inplace> "
                              "default|password|pin|pattern [passwd] [noui]";
 
@@ -215,15 +241,11 @@
                 Process::killProcessesWithOpenFiles(DATA_MNT_POINT, SIGKILL);
             }
         }
-    } else if (!strcmp(argv[1], "enablefilecrypto")) {
-        const char* syntax = "Usage: cryptfs enablefilecrypto";
-        if (argc != 2) {
-            cli->sendMsg(ResponseCode::CommandSyntaxError, syntax, false);
-            return 0;
-        }
+    } else if (subcommand == "enablefilecrypto") {
+        if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
         dumpArgs(argc, argv, -1);
         rc = cryptfs_enable_file();
-    } else if (!strcmp(argv[1], "changepw")) {
+    } else if (subcommand == "changepw") {
         const char* syntax = "Usage: cryptfs changepw "
                              "default|password|pin|pattern [newpasswd]";
         const char* password;
@@ -242,21 +264,15 @@
         }
         SLOGD("cryptfs changepw %s {}", argv[2]);
         rc = cryptfs_changepw(type, password);
-    } else if (!strcmp(argv[1], "verifypw")) {
-        if (argc != 3) {
-            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs verifypw <passwd>", false);
-            return 0;
-        }
+    } else if (subcommand == "verifypw") {
+        if (!check_argc(cli, subcommand, argc, 3, "<passwd>")) return 0;
         SLOGD("cryptfs verifypw {}");
         rc = cryptfs_verify_passwd(argv[2]);
-    } else if (!strcmp(argv[1], "getfield")) {
+    } else if (subcommand == "getfield") {
+        if (!check_argc(cli, subcommand, argc, 3, "<fieldname>")) return 0;
         char *valbuf;
         int valbuf_len = PROPERTY_VALUE_MAX;
 
-        if (argc != 3) {
-            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs getfield <fieldname>", false);
-            return 0;
-        }
         dumpArgs(argc, argv, -1);
 
         // Increase the buffer size until it is big enough for the field value stored.
@@ -277,18 +293,17 @@
             cli->sendMsg(ResponseCode::CryptfsGetfieldResult, valbuf, false);
         }
         free(valbuf);
-    } else if (!strcmp(argv[1], "setfield")) {
-        if (argc != 4) {
-            cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: cryptfs setfield <fieldname> <value>", false);
-            return 0;
-        }
+    } else if (subcommand == "setfield") {
+        if (!check_argc(cli, subcommand, argc, 4, "<fieldname> <value>")) return 0;
         dumpArgs(argc, argv, -1);
         rc = cryptfs_setfield(argv[2], argv[3]);
-    } else if (!strcmp(argv[1], "mountdefaultencrypted")) {
+    } else if (subcommand == "mountdefaultencrypted") {
+        if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
         SLOGD("cryptfs mountdefaultencrypted");
         dumpArgs(argc, argv, -1);
         rc = cryptfs_mount_default_encrypted();
-    } else if (!strcmp(argv[1], "getpwtype")) {
+    } else if (subcommand == "getpwtype") {
+        if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
         SLOGD("cryptfs getpwtype");
         dumpArgs(argc, argv, -1);
         switch(cryptfs_get_password_type()) {
@@ -309,7 +324,8 @@
             cli->sendMsg(ResponseCode::OpFailedStorageNotFound, "Error", false);
             return 0;
         }
-    } else if (!strcmp(argv[1], "getpw")) {
+    } else if (subcommand == "getpw") {
+        if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
         SLOGD("cryptfs getpw");
         dumpArgs(argc, argv, -1);
         const char* password = cryptfs_get_password();
@@ -324,43 +340,55 @@
             }
         }
         rc = -1;
-    } else if (!strcmp(argv[1], "clearpw")) {
+    } else if (subcommand == "clearpw") {
+        if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
         SLOGD("cryptfs clearpw");
         dumpArgs(argc, argv, -1);
         cryptfs_clear_password();
         rc = 0;
-    } else if (!strcmp(argv[1], "setusercryptopolicies")) {
-        if (argc != 3) {
-            cli->sendMsg(ResponseCode::CommandSyntaxError,
-                "Usage: cryptfs setusercryptopolicies <path>", false);
-            return 0;
-        }
-        SLOGD("cryptfs setusercryptopolicies");
-        dumpArgs(argc, argv, -1);
-        rc = e4crypt_set_user_crypto_policies(argv[2]);
-    } else if (!strcmp(argv[1], "createnewuserdir")) {
-        if (argc != 4) {
-            cli->sendMsg(ResponseCode::CommandSyntaxError,
-                "Usage: cryptfs createnewuserdir <userHandle> <path>", false);
-            return 0;
-        }
+
+    } else if (subcommand == "isConvertibleToFBE") {
+        if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
         // ext4enc:TODO: send a CommandSyntaxError if argv[2] not an integer
-        SLOGD("cryptfs createnewuserdir");
+        SLOGD("cryptfs isConvertibleToFBE");
         dumpArgs(argc, argv, -1);
-        rc = e4crypt_create_new_user_dir(argv[2], argv[3]);
-    } else if (!strcmp(argv[1], "deleteuserkey")) {
-        if (argc != 3) {
-            cli->sendMsg(ResponseCode::CommandSyntaxError,
-                "Usage: cryptfs deleteuserkey <userHandle>", false);
-            return 0;
-        }
-        // ext4enc:TODO: send a CommandSyntaxError if argv[2] not an integer
-        SLOGD("cryptfs deleteuserkey");
-        dumpArgs(argc, argv, -1);
-        rc = e4crypt_delete_user_key(argv[2]);
+        rc = cryptfs_isConvertibleToFBE();
+
+    } else if (subcommand == "init_user0") {
+        if (!check_argc(cli, subcommand, argc, 2, "")) return 0;
+        return sendGenericOkFail(cli, e4crypt_init_user0());
+
+    } else if (subcommand == "create_user_key") {
+        if (!check_argc(cli, subcommand, argc, 5, "<user> <serial> <ephemeral>")) return 0;
+        return sendGenericOkFail(cli,
+                                 e4crypt_vold_create_user_key(atoi(argv[2]),
+                                                              atoi(argv[3]),
+                                                              atoi(argv[4]) != 0));
+
+    } else if (subcommand == "destroy_user_key") {
+        if (!check_argc(cli, subcommand, argc, 3, "<user>")) return 0;
+        return sendGenericOkFail(cli, e4crypt_destroy_user_key(atoi(argv[2])));
+
+    } else if (subcommand == "unlock_user_key") {
+        if (!check_argc(cli, subcommand, argc, 5, "<user> <serial> <token>")) return 0;
+        return sendGenericOkFail(cli, e4crypt_unlock_user_key(
+            atoi(argv[2]), atoi(argv[3]), parseNull(argv[4])));
+
+    } else if (subcommand == "lock_user_key") {
+        if (!check_argc(cli, subcommand, argc, 3, "<user>")) return 0;
+        return sendGenericOkFail(cli, e4crypt_lock_user_key(atoi(argv[2])));
+
+    } else if (subcommand == "prepare_user_storage") {
+        if (!check_argc(cli, subcommand, argc, 6, "<uuid> <user> <serial> <ephemeral>")) return 0;
+        return sendGenericOkFail(cli,
+                                 e4crypt_prepare_user_storage(parseNull(argv[2]),
+                                                              atoi(argv[3]),
+                                                              atoi(argv[4]),
+                                                              atoi(argv[5]) != 0));
+
     } else {
         dumpArgs(argc, argv, -1);
-        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs cmd", false);
+        cli->sendMsg(ResponseCode::CommandSyntaxError, "Unknown cryptfs subcommand", false);
         return 0;
     }
 
diff --git a/EmulatedVolume.cpp b/EmulatedVolume.cpp
index 230fa8b..581c322 100644
--- a/EmulatedVolume.cpp
+++ b/EmulatedVolume.cpp
@@ -107,17 +107,21 @@
 }
 
 status_t EmulatedVolume::doUnmount() {
+    // Unmount the storage before we kill the FUSE process. If we kill
+    // the FUSE process first, most file system operations will return
+    // ENOTCONN until the unmount completes. This is an exotic and unusual
+    // error code and might cause broken behaviour in applications.
+    KillProcessesUsingPath(getPath());
+    ForceUnmount(mFuseDefault);
+    ForceUnmount(mFuseRead);
+    ForceUnmount(mFuseWrite);
+
     if (mFusePid > 0) {
         kill(mFusePid, SIGTERM);
         TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
         mFusePid = 0;
     }
 
-    KillProcessesUsingPath(getPath());
-    ForceUnmount(mFuseDefault);
-    ForceUnmount(mFuseRead);
-    ForceUnmount(mFuseWrite);
-
     rmdir(mFuseDefault.c_str());
     rmdir(mFuseRead.c_str());
     rmdir(mFuseWrite.c_str());
diff --git a/Ext4Crypt.cpp b/Ext4Crypt.cpp
index 0807c2c..72c79fa 100644
--- a/Ext4Crypt.cpp
+++ b/Ext4Crypt.cpp
@@ -1,11 +1,31 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
 #include "Ext4Crypt.h"
 
+#include "KeyStorage.h"
+#include "Utils.h"
+
 #include <iomanip>
 #include <map>
-#include <fstream>
+#include <set>
 #include <string>
 #include <sstream>
 
+#include <stdio.h>
 #include <errno.h>
 #include <dirent.h>
 #include <sys/mount.h>
@@ -14,6 +34,7 @@
 #include <fcntl.h>
 #include <cutils/properties.h>
 #include <openssl/sha.h>
+#include <selinux/android.h>
 
 #include <private/android_filesystem_config.h>
 
@@ -23,11 +44,29 @@
 #include "ext4_crypt_init_extensions.h"
 
 #define LOG_TAG "Ext4Crypt"
-#include "cutils/log.h"
+
+#define EMULATED_USES_SELINUX 0
+
+#include <cutils/fs.h>
+#include <cutils/log.h>
 #include <cutils/klog.h>
+
 #include <android-base/file.h>
+#include <android-base/logging.h>
 #include <android-base/stringprintf.h>
 
+using android::base::StringPrintf;
+
+static bool e4crypt_is_native() {
+    char value[PROPERTY_VALUE_MAX];
+    property_get("ro.crypto.type", value, "none");
+    return !strcmp(value, "file");
+}
+
+static bool e4crypt_is_emulated() {
+    return property_get_bool("persist.sys.emulate_fbe", false);
+}
+
 namespace {
     // Key length in bits
     const int key_length = 128;
@@ -37,6 +76,9 @@
     // How long do we store passwords for?
     const int password_max_age_seconds = 60;
 
+    const std::string user_key_dir = std::string() + DATA_MNT_POINT + "/misc/vold/user_keys";
+    const std::string user_key_temp = user_key_dir + "/temp";
+
     // How is device encrypted
     struct keys {
         std::string master_key;
@@ -44,9 +86,13 @@
         time_t expiry_time;
     };
     std::map<std::string, keys> s_key_store;
+    // Some users are ephemeral, don't try to wipe their keys from disk
+    std::set<userid_t> s_ephemeral_users;
+    // Map user ids to key references
+    std::map<userid_t, std::string> s_de_key_raw_refs;
+    std::map<userid_t, std::string> s_ce_key_raw_refs;
 
-    // ext4enc:TODO get these consts from somewhere good
-    const int SHA512_LENGTH = 64;
+    // ext4enc:TODO get this const from somewhere good
     const int EXT4_KEY_DESCRIPTOR_SIZE = 8;
 
     // ext4enc:TODO Include structure from somewhere sensible
@@ -78,7 +124,7 @@
     }
 }
 
-static std::string e4crypt_install_key(const std::string &key);
+static bool install_key(const std::string &key, std::string &raw_ref);
 
 static int put_crypt_ftr_and_key(const crypt_mnt_ftr& crypt_ftr,
                                  UnencryptedProperties& props)
@@ -294,12 +340,12 @@
 
     SHA512_Init(&c);
     SHA512_Update(&c, key, length);
-    unsigned char key_ref1[SHA512_LENGTH];
+    unsigned char key_ref1[SHA512_DIGEST_LENGTH];
     SHA512_Final(key_ref1, &c);
 
     SHA512_Init(&c);
-    SHA512_Update(&c, key_ref1, SHA512_LENGTH);
-    unsigned char key_ref2[SHA512_LENGTH];
+    SHA512_Update(&c, key_ref1, SHA512_DIGEST_LENGTH);
+    unsigned char key_ref2[SHA512_DIGEST_LENGTH];
     SHA512_Final(key_ref2, &c);
 
     return std::string((char*)key_ref2, EXT4_KEY_DESCRIPTOR_SIZE);
@@ -340,10 +386,11 @@
     clock_gettime(CLOCK_BOOTTIME, &now);
     s_key_store[path] = keys{master_key, password,
                              now.tv_sec + password_max_age_seconds};
-    auto raw_ref = e4crypt_install_key(master_key);
-    if (raw_ref.empty()) {
+    std::string raw_ref;
+    if (!install_key(master_key, raw_ref)) {
         return -1;
     }
+    SLOGD("Installed master key");
 
     // Save reference to key so we can set policy later
     if (!props.Set(properties::ref, raw_ref)) {
@@ -385,34 +432,28 @@
     return keyctl_search(KEY_SPEC_SESSION_KEYRING, "keyring", "e4crypt", 0);
 }
 
-static int e4crypt_install_key(const ext4_encryption_key &ext4_key, const std::string &ref)
+// Install password into global keyring
+// Return raw key reference for use in policy
+static bool install_key(const std::string &key, std::string &raw_ref)
 {
+    if (key.size() != key_length/8) {
+        LOG(ERROR) << "Wrong size key " << key.size();
+        return false;
+    }
+    auto ext4_key = fill_key(key);
+    raw_ref = generate_key_ref(ext4_key.raw, ext4_key.size);
+    auto ref = keyname(raw_ref);
     key_serial_t device_keyring = e4crypt_keyring();
-    SLOGI("Found device_keyring - id is %d", device_keyring);
     key_serial_t key_id = add_key("logon", ref.c_str(),
                                   (void*)&ext4_key, sizeof(ext4_key),
                                   device_keyring);
     if (key_id == -1) {
-        SLOGE("Failed to insert key into keyring with error %s",
-              strerror(errno));
-        return -1;
+        PLOG(ERROR) << "Failed to insert key into keyring " << device_keyring;
+        return false;
     }
-    SLOGI("Added key %d (%s) to keyring %d in process %d",
-          key_id, ref.c_str(), device_keyring, getpid());
-    return 0;
-}
-
-// Install password into global keyring
-// Return raw key reference for use in policy
-static std::string e4crypt_install_key(const std::string &key)
-{
-    auto ext4_key = fill_key(key);
-    auto raw_ref = generate_key_ref(ext4_key.raw, ext4_key.size);
-    auto ref = keyname(raw_ref);
-    if (e4crypt_install_key(ext4_key, ref) == -1) {
-        return "";
-    }
-    return raw_ref;
+    LOG(INFO) << "Added key " << key_id << " (" << ref << ") to keyring "
+        << device_keyring << " in process " << getpid();
+    return true;
 }
 
 int e4crypt_restart(const char* path)
@@ -509,92 +550,114 @@
         .Set(fieldname, std::string(value)) ? 0 : -1;
 }
 
-static std::string get_key_path(
-    const char *mount_path,
-    const char *user_handle)
-{
-    // ext4enc:TODO get the path properly
-    auto key_dir = android::base::StringPrintf("%s/misc/vold/user_keys",
-        mount_path);
-    if (mkdir(key_dir.c_str(), 0700) < 0 && errno != EEXIST) {
-        SLOGE("Unable to create %s (%s)", key_dir.c_str(), strerror(errno));
-        return "";
-    }
-    return key_dir + "/" + user_handle;
+static std::string get_de_key_path(userid_t user_id) {
+    return StringPrintf("%s/de/%d", user_key_dir.c_str(), user_id);
 }
 
-// ext4enc:TODO this can't be the only place keys are read from /dev/urandom
-// we should unite those places.
-static std::string e4crypt_get_key(
-    const std::string &key_path,
-    bool create_if_absent)
-{
-    std::string content;
-    if (android::base::ReadFileToString(key_path, &content)) {
-        if (content.size() != key_length/8) {
-            SLOGE("Wrong size key %zu in  %s", content.size(), key_path.c_str());
-            return "";
-        }
-        return content;
-    }
-    if (!create_if_absent) {
-        SLOGE("No key found in %s", key_path.c_str());
-        return "";
-    }
-    std::ifstream urandom("/dev/urandom");
-    if (!urandom) {
-        SLOGE("Unable to open /dev/urandom (%s)", strerror(errno));
-        return "";
-    }
-    char key_bytes[key_length / 8];
-    errno = 0;
-    urandom.read(key_bytes, sizeof(key_bytes));
-    if (!urandom) {
-        SLOGE("Unable to read key from /dev/urandom (%s)", strerror(errno));
-        return "";
-    }
-    std::string key(key_bytes, sizeof(key_bytes));
-    if (!android::base::WriteStringToFile(key, key_path)) {
-        SLOGE("Unable to write key to %s (%s)",
-                key_path.c_str(), strerror(errno));
-        return "";
-    }
-    return key;
+static std::string get_ce_key_path(userid_t user_id) {
+    return StringPrintf("%s/ce/%d/current", user_key_dir.c_str(), user_id);
 }
 
-static int e4crypt_set_user_policy(const char *mount_path, const char *user_handle,
-                            const char *path, bool create_if_absent)
+static bool read_and_install_key(const std::string &key_path, std::string &raw_ref)
 {
-    SLOGD("e4crypt_set_user_policy for %s", user_handle);
-    auto user_key = e4crypt_get_key(
-        get_key_path(mount_path, user_handle),
-        create_if_absent);
-    if (user_key.empty()) {
-        return -1;
-    }
-    auto raw_ref = e4crypt_install_key(user_key);
-    if (raw_ref.empty()) {
-        return -1;
-    }
-    return do_policy_set(path, raw_ref.c_str(), raw_ref.size());
+    std::string key;
+    if (!android::vold::retrieveKey(key_path, key)) return false;
+    if (!install_key(key, raw_ref)) return false;
+    return true;
 }
 
-int e4crypt_create_new_user_dir(const char *user_handle, const char *path) {
-    SLOGD("e4crypt_create_new_user_dir(\"%s\", \"%s\")", user_handle, path);
-    if (mkdir(path, S_IRWXU | S_IRWXG | S_IXOTH) < 0) {
-        return -1;
+static bool read_and_install_user_ce_key(userid_t user_id)
+{
+    if (s_ce_key_raw_refs.count(user_id) != 0) return true;
+    const auto key_path = get_ce_key_path(user_id);
+    std::string raw_ref;
+    if (!read_and_install_key(key_path, raw_ref)) return false;
+    s_ce_key_raw_refs[user_id] = raw_ref;
+    LOG(DEBUG) << "Installed ce key for user " << user_id;
+    return true;
+}
+
+static bool prepare_dir(const std::string &dir, mode_t mode, uid_t uid, gid_t gid) {
+    LOG(DEBUG) << "Preparing: " << dir;
+    if (fs_prepare_dir(dir.c_str(), mode, uid, gid) != 0) {
+        PLOG(ERROR) << "Failed to prepare " << dir;
+        return false;
     }
-    if (chmod(path, S_IRWXU | S_IRWXG | S_IXOTH) < 0) {
-        return -1;
+    return true;
+}
+
+static bool random_key(std::string &key) {
+    if (android::vold::ReadRandomBytes(key_length / 8, key) != 0) {
+        // TODO status_t plays badly with PLOG, fix it.
+        LOG(ERROR) << "Random read failed";
+        return false;
     }
-    if (chown(path, AID_SYSTEM, AID_SYSTEM) < 0) {
-        return -1;
+    return true;
+}
+
+static bool path_exists(const std::string &path) {
+    return access(path.c_str(), F_OK) == 0;
+}
+
+// NB this assumes that there is only one thread listening for crypt commands, because
+// it creates keys in a fixed location.
+static bool store_key(const std::string &key_path, const std::string &key) {
+    if (path_exists(key_path)) {
+        LOG(ERROR) << "Already exists, cannot create key at: " << key_path;
+        return false;
     }
-    if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
-        // ext4enc:TODO handle errors from this.
-        e4crypt_set_user_policy(DATA_MNT_POINT, user_handle, path, true);
+    if (path_exists(user_key_temp)) {
+        android::vold::destroyKey(user_key_temp);
     }
-    return 0;
+    if (!android::vold::storeKey(user_key_temp, key)) return false;
+    if (rename(user_key_temp.c_str(), key_path.c_str()) != 0) {
+        PLOG(ERROR) << "Unable to move new key to location: " << key_path;
+        return false;
+    }
+    LOG(DEBUG) << "Created key " << key_path;
+    return true;
+}
+
+static bool create_and_install_user_keys(userid_t user_id, bool create_ephemeral) {
+    std::string de_key, ce_key;
+    if (!random_key(de_key)) return false;
+    if (!random_key(ce_key)) return false;
+    if (create_ephemeral) {
+        // If the key should be created as ephemeral, don't store it.
+        s_ephemeral_users.insert(user_id);
+    } else {
+        if (!store_key(get_de_key_path(user_id), de_key)) return false;
+        if (!prepare_dir(user_key_dir + "/ce/" + std::to_string(user_id),
+            0700, AID_ROOT, AID_ROOT)) return false;
+        if (!store_key(get_ce_key_path(user_id), ce_key)) return false;
+    }
+    std::string de_raw_ref;
+    if (!install_key(de_key, de_raw_ref)) return false;
+    s_de_key_raw_refs[user_id] = de_raw_ref;
+    std::string ce_raw_ref;
+    if (!install_key(ce_key, ce_raw_ref)) return false;
+    s_ce_key_raw_refs[user_id] = ce_raw_ref;
+    LOG(DEBUG) << "Created keys for user " << user_id;
+    return true;
+}
+
+static bool lookup_key_ref(const std::map<userid_t, std::string> &key_map,
+        userid_t user_id, std::string &raw_ref) {
+    auto refi = key_map.find(user_id);
+    if (refi == key_map.end()) {
+        LOG(ERROR) << "Cannot find key for " << user_id;
+        return false;
+    }
+    raw_ref = refi->second;
+    return true;
+}
+
+static bool set_policy(const std::string &raw_ref, const std::string& path) {
+    if (do_policy_set(path.c_str(), raw_ref.data(), raw_ref.size()) != 0) {
+        LOG(ERROR) << "Failed to set policy on: " << path;
+        return false;
+    }
+    return true;
 }
 
 static bool is_numeric(const char *name) {
@@ -605,67 +668,213 @@
     return true;
 }
 
-int e4crypt_set_user_crypto_policies(const char *dir)
-{
-    if (e4crypt_crypto_complete(DATA_MNT_POINT) != 0) {
-        return 0;
-    }
-    SLOGD("e4crypt_set_user_crypto_policies");
-    std::unique_ptr<DIR, int(*)(DIR*)> dirp(opendir(dir), closedir);
+static bool load_all_de_keys() {
+    auto de_dir = user_key_dir + "/de";
+    auto dirp = std::unique_ptr<DIR, int(*)(DIR*)>(opendir(de_dir.c_str()), closedir);
     if (!dirp) {
-        SLOGE("Unable to read directory %s, error %s\n",
-            dir, strerror(errno));
-        return -1;
+        PLOG(ERROR) << "Unable to read de key directory";
+        return false;
     }
     for (;;) {
-        struct dirent *result = readdir(dirp.get());
-        if (!result) {
-            // ext4enc:TODO check errno
+        errno = 0;
+        auto entry = readdir(dirp.get());
+        if (!entry) {
+            if (errno) {
+                PLOG(ERROR) << "Unable to read de key directory";
+                return false;
+            }
             break;
         }
-        if (result->d_type != DT_DIR || !is_numeric(result->d_name)) {
-            continue; // skips user 0, which is a symlink
+        if (entry->d_type != DT_DIR || !is_numeric(entry->d_name)) {
+            LOG(DEBUG) << "Skipping non-de-key " << entry->d_name;
+            continue;
         }
-        auto user_dir = std::string() + dir + "/" + result->d_name;
-        // ext4enc:TODO don't hardcode /data
-        if (e4crypt_set_user_policy("/data", result->d_name,
-                user_dir.c_str(), false)) {
-            // ext4enc:TODO If this function fails, stop the boot: we must
-            // deliver on promised encryption.
-            SLOGE("Unable to set policy on %s\n", user_dir.c_str());
+        userid_t user_id = atoi(entry->d_name);
+        if (s_de_key_raw_refs.count(user_id) == 0) {
+            std::string raw_ref;
+            if (!read_and_install_key(de_dir + "/" + entry->d_name, raw_ref)) return false;
+            s_de_key_raw_refs[user_id] = raw_ref;
+            LOG(DEBUG) << "Installed de key for user " << user_id;
+        }
+    }
+    // ext4enc:TODO: go through all DE directories, ensure that all user dirs have the
+    // correct policy set on them, and that no rogue ones exist.
+    return true;
+}
+
+int e4crypt_init_user0() {
+    LOG(DEBUG) << "e4crypt_init_user0";
+    if (e4crypt_is_native()) {
+        if (!prepare_dir(user_key_dir, 0700, AID_ROOT, AID_ROOT)) return -1;
+        if (!prepare_dir(user_key_dir + "/ce", 0700, AID_ROOT, AID_ROOT)) return -1;
+        if (!prepare_dir(user_key_dir + "/de", 0700, AID_ROOT, AID_ROOT)) return -1;
+        auto de_path = get_de_key_path(0);
+        auto ce_path = get_ce_key_path(0);
+        if (!path_exists(de_path) || !path_exists(ce_path)) {
+            if (path_exists(de_path)) {
+                android::vold::destroyKey(de_path); // Ignore failure
+            }
+            if (path_exists(ce_path)) {
+                android::vold::destroyKey(ce_path); // Ignore failure
+            }
+            if (!create_and_install_user_keys(0, false)) return -1;
+        }
+        if (!load_all_de_keys()) return -1;
+    }
+    // Ignore failures. FIXME this is horrid
+    // FIXME: we need an idempotent policy-setting call, which simply verifies the
+    // policy is already set on a second run, even if the directory is nonempty.
+    // Then we need to call it all the time.
+    e4crypt_prepare_user_storage(nullptr, 0, 0, false);
+    return 0;
+}
+
+int e4crypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral) {
+    LOG(DEBUG) << "e4crypt_vold_create_user_key for " << user_id << " serial " << serial;
+    if (!e4crypt_is_native()) {
+        return 0;
+    }
+    // FIXME test for existence of key that is not loaded yet
+    if (s_ce_key_raw_refs.count(user_id) != 0) {
+        LOG(ERROR) << "Already exists, can't e4crypt_vold_create_user_key for "
+            << user_id << " serial " << serial;
+        // FIXME should we fail the command?
+        return 0;
+    }
+    if (!create_and_install_user_keys(user_id, ephemeral)) {
+        return -1;
+    }
+    // TODO: create second key for user_de data
+    return 0;
+}
+
+static bool evict_key(const std::string &raw_ref) {
+    auto ref = keyname(raw_ref);
+    auto key_serial = keyctl_search(e4crypt_keyring(), "logon", ref.c_str(), 0);
+    if (keyctl_revoke(key_serial) != 0) {
+        PLOG(ERROR) << "Failed to revoke key with serial " << key_serial << " ref " << ref;
+        return false;
+    }
+    LOG(DEBUG) << "Revoked key with serial " << key_serial << " ref " << ref;
+    return true;
+}
+
+int e4crypt_destroy_user_key(userid_t user_id) {
+    LOG(DEBUG) << "e4crypt_destroy_user_key(" << user_id << ")";
+    if (!e4crypt_is_native()) {
+        return 0;
+    }
+    bool success = true;
+    std::string raw_ref;
+    success &= lookup_key_ref(s_ce_key_raw_refs, user_id, raw_ref) && evict_key(raw_ref);
+    success &= lookup_key_ref(s_de_key_raw_refs, user_id, raw_ref) && evict_key(raw_ref);
+    auto it = s_ephemeral_users.find(user_id);
+    if (it != s_ephemeral_users.end()) {
+        s_ephemeral_users.erase(it);
+    } else {
+        success &= android::vold::destroyKey(get_ce_key_path(user_id));
+        success &= android::vold::destroyKey(get_de_key_path(user_id));
+    }
+    return success ? 0 : -1;
+}
+
+static int emulated_lock(const std::string& path) {
+    if (chmod(path.c_str(), 0000) != 0) {
+        PLOG(ERROR) << "Failed to chmod " << path;
+        return -1;
+    }
+#if EMULATED_USES_SELINUX
+    if (setfilecon(path.c_str(), "u:object_r:storage_stub_file:s0") != 0) {
+        PLOG(WARNING) << "Failed to setfilecon " << path;
+        return -1;
+    }
+#endif
+    return 0;
+}
+
+static int emulated_unlock(const std::string& path, mode_t mode) {
+    if (chmod(path.c_str(), mode) != 0) {
+        PLOG(ERROR) << "Failed to chmod " << path;
+        // FIXME temporary workaround for b/26713622
+        if (e4crypt_is_emulated()) return -1;
+    }
+#if EMULATED_USES_SELINUX
+    if (selinux_android_restorecon(path.c_str(), SELINUX_ANDROID_RESTORECON_FORCE) != 0) {
+        PLOG(WARNING) << "Failed to restorecon " << path;
+        // FIXME temporary workaround for b/26713622
+        if (e4crypt_is_emulated()) return -1;
+    }
+#endif
+    return 0;
+}
+
+int e4crypt_unlock_user_key(userid_t user_id, int serial, const char* token) {
+    LOG(DEBUG) << "e4crypt_unlock_user_key " << user_id << " " << (token != nullptr);
+    if (e4crypt_is_native()) {
+        if (!read_and_install_user_ce_key(user_id)) {
+            LOG(ERROR) << "Couldn't read key for " << user_id;
+            return -1;
+        }
+    } else {
+        // When in emulation mode, we just use chmod. However, we also
+        // unlock directories when not in emulation mode, to bring devices
+        // back into a known-good state.
+        if (emulated_unlock(android::vold::BuildDataSystemCePath(user_id), 0771) ||
+                emulated_unlock(android::vold::BuildDataMediaPath(nullptr, user_id), 0770) ||
+                emulated_unlock(android::vold::BuildDataUserPath(nullptr, user_id), 0771)) {
+            LOG(ERROR) << "Failed to unlock user " << user_id;
+            return -1;
         }
     }
     return 0;
 }
 
-int e4crypt_delete_user_key(const char *user_handle) {
-    SLOGD("e4crypt_delete_user_key(\"%s\")", user_handle);
-    auto key_path = get_key_path(DATA_MNT_POINT, user_handle);
-    auto key = e4crypt_get_key(key_path, false);
-    auto ext4_key = fill_key(key);
-    auto ref = keyname(generate_key_ref(ext4_key.raw, ext4_key.size));
-    auto key_serial = keyctl_search(e4crypt_keyring(), "logon", ref.c_str(), 0);
-    if (keyctl_revoke(key_serial) == 0) {
-        SLOGD("Revoked key with serial %ld ref %s\n", key_serial, ref.c_str());
+int e4crypt_lock_user_key(userid_t user_id) {
+    if (e4crypt_is_native()) {
+        // TODO: remove from kernel keyring
+    } else if (e4crypt_is_emulated()) {
+        // When in emulation mode, we just use chmod
+        if (emulated_lock(android::vold::BuildDataSystemCePath(user_id)) ||
+                emulated_lock(android::vold::BuildDataMediaPath(nullptr, user_id)) ||
+                emulated_lock(android::vold::BuildDataUserPath(nullptr, user_id))) {
+            PLOG(ERROR) << "Failed to lock user " << user_id;
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
+int e4crypt_prepare_user_storage(const char* volume_uuid,
+                                 userid_t user_id,
+                                 int serial,
+                                 bool ephemeral) {
+    if (volume_uuid) {
+        LOG(DEBUG) << "e4crypt_prepare_user_storage " << volume_uuid << " " << user_id;
     } else {
-        SLOGE("Failed to revoke key with serial %ld ref %s: %s\n",
-            key_serial, ref.c_str(), strerror(errno));
+        LOG(DEBUG) << "e4crypt_prepare_user_storage, null volume " << user_id;
     }
-    int pid = fork();
-    if (pid < 0) {
-        SLOGE("Unable to fork: %s", strerror(errno));
-        return -1;
+    auto system_ce_path = android::vold::BuildDataSystemCePath(user_id);
+    auto media_ce_path = android::vold::BuildDataMediaPath(volume_uuid, user_id);
+    auto user_ce_path = android::vold::BuildDataUserPath(volume_uuid, user_id);
+    auto user_de_path = android::vold::BuildDataUserDePath(volume_uuid, user_id);
+
+    // FIXME: should this be 0770 or 0700?
+    if (!prepare_dir(system_ce_path, 0770, AID_SYSTEM, AID_SYSTEM)) return -1;
+    if (!prepare_dir(media_ce_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW)) return -1;
+    if (!prepare_dir(user_ce_path, 0771, AID_SYSTEM, AID_SYSTEM)) return -1;
+    if (!prepare_dir(user_de_path, 0771, AID_SYSTEM, AID_SYSTEM)) return -1;
+
+    if (e4crypt_crypto_complete(DATA_MNT_POINT) == 0) {
+        std::string ce_raw_ref, de_raw_ref;
+        if (!lookup_key_ref(s_ce_key_raw_refs, user_id, ce_raw_ref)) return -1;
+        if (!lookup_key_ref(s_de_key_raw_refs, user_id, de_raw_ref)) return -1;
+        if (!set_policy(ce_raw_ref, system_ce_path)) return -1;
+        if (!set_policy(ce_raw_ref, media_ce_path)) return -1;
+        if (!set_policy(ce_raw_ref, user_ce_path)) return -1;
+        if (!set_policy(de_raw_ref, user_de_path)) return -1;
+        // FIXME I thought there were more DE directories than this
     }
-    if (pid == 0) {
-        SLOGD("Forked for secdiscard");
-        execl("/system/bin/secdiscard",
-            "/system/bin/secdiscard",
-            key_path.c_str(),
-            NULL);
-        SLOGE("Unable to launch secdiscard on %s: %s\n", key_path.c_str(),
-            strerror(errno));
-        exit(-1);
-    }
-    // ext4enc:TODO reap the zombie
+
     return 0;
 }
diff --git a/Ext4Crypt.h b/Ext4Crypt.h
index f5c2871..9005f4a 100644
--- a/Ext4Crypt.h
+++ b/Ext4Crypt.h
@@ -1,11 +1,28 @@
+/*
+ * Copyright (C) 2015 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.
+ */
+
 #include <stddef.h>
 #include <sys/cdefs.h>
 
+#include <cutils/multiuser.h>
+
 __BEGIN_DECLS
 
 // General functions
 int e4crypt_enable(const char* path);
-int e4crypt_main(int argc, char* argv[]);
 int e4crypt_change_password(const char* path, int crypt_type,
                             const char* password);
 int e4crypt_crypto_complete(const char* path);
@@ -18,8 +35,17 @@
                       char* value, size_t len);
 int e4crypt_set_field(const char* path, const char* fieldname,
                       const char* value);
-int e4crypt_set_user_crypto_policies(const char *path);
-int e4crypt_create_new_user_dir(const char *user_handle, const char *path);
-int e4crypt_delete_user_key(const char *user_handle);
+
+int e4crypt_init_user0();
+int e4crypt_vold_create_user_key(userid_t user_id, int serial, bool ephemeral);
+int e4crypt_destroy_user_key(userid_t user_id);
+
+int e4crypt_unlock_user_key(userid_t user_id, int serial, const char* token);
+int e4crypt_lock_user_key(userid_t user_id);
+
+int e4crypt_prepare_user_storage(const char* volume_uuid,
+                                 userid_t user_id,
+                                 int serial,
+                                 bool ephemeral);
 
 __END_DECLS
diff --git a/KeyStorage.cpp b/KeyStorage.cpp
new file mode 100644
index 0000000..070b79d
--- /dev/null
+++ b/KeyStorage.cpp
@@ -0,0 +1,240 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+#include "KeyStorage.h"
+
+#include "Keymaster.h"
+#include "Utils.h"
+
+#include <vector>
+
+#include <errno.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include <openssl/sha.h>
+
+#include <android-base/file.h>
+#include <android-base/logging.h>
+
+#include <keymaster/authorization_set.h>
+
+namespace android {
+namespace vold {
+
+static constexpr size_t AES_KEY_BYTES = 32;
+static constexpr size_t GCM_NONCE_BYTES = 12;
+static constexpr size_t GCM_MAC_BYTES = 16;
+// FIXME: better name than "secdiscardable" sought!
+static constexpr size_t SECDISCARDABLE_BYTES = 1<<14;
+
+static const char* kRmPath = "/system/bin/rm";
+static const char* kSecdiscardPath = "/system/bin/secdiscard";
+static const char* kFn_keymaster_key_blob = "keymaster_key_blob";
+static const char* kFn_encrypted_key = "encrypted_key";
+static const char* kFn_secdiscardable = "secdiscardable";
+
+static bool checkSize(const std::string& kind, size_t actual, size_t expected) {
+    if (actual != expected) {
+        LOG(ERROR) << "Wrong number of bytes in " << kind << ", expected " << expected
+            << " got " << actual;
+        return false;
+    }
+    return true;
+}
+
+static std::string hashSecdiscardable(const std::string &secdiscardable) {
+    SHA512_CTX c;
+
+    SHA512_Init(&c);
+    // Personalise the hashing by introducing a fixed prefix.
+    // Hashing applications should use personalization except when there is a
+    // specific reason not to; see section 4.11 of https://www.schneier.com/skein1.3.pdf
+    std::string secdiscardableHashingPrefix = "Android secdiscardable SHA512";
+    secdiscardableHashingPrefix.resize(SHA512_CBLOCK);
+    SHA512_Update(&c, secdiscardableHashingPrefix.data(), secdiscardableHashingPrefix.size());
+    SHA512_Update(&c, secdiscardable.data(), secdiscardable.size());
+    std::string res(SHA512_DIGEST_LENGTH, '\0');
+    SHA512_Final(reinterpret_cast<uint8_t *>(&res[0]), &c);
+    return res;
+}
+
+static bool generateKeymasterKey(Keymaster &keymaster,
+        const keymaster::AuthorizationSet &extraParams,
+        std::string &key) {
+    auto params = keymaster::AuthorizationSetBuilder()
+        .AesEncryptionKey(AES_KEY_BYTES * 8)
+        .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
+        .Authorization(keymaster::TAG_MIN_MAC_LENGTH, GCM_MAC_BYTES * 8)
+        .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE)
+        .Authorization(keymaster::TAG_NO_AUTH_REQUIRED) // FIXME integrate with gatekeeper
+        .build();
+    params.push_back(extraParams);
+    return keymaster.generateKey(params, key);
+}
+
+static bool encryptWithKeymasterKey(
+        Keymaster &keymaster,
+        const std::string &key,
+        const keymaster::AuthorizationSet &extraParams,
+        const std::string &message,
+        std::string &ciphertext) {
+    // FIXME fix repetition
+    auto params = keymaster::AuthorizationSetBuilder()
+        .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
+        .Authorization(keymaster::TAG_MAC_LENGTH, GCM_MAC_BYTES * 8)
+        .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE)
+        .build();
+    params.push_back(extraParams);
+    keymaster::AuthorizationSet outParams;
+    auto opHandle = keymaster.begin(KM_PURPOSE_ENCRYPT, key, params, outParams);
+    if (!opHandle) return false;
+    keymaster_blob_t nonceBlob;
+    if (!outParams.GetTagValue(keymaster::TAG_NONCE, &nonceBlob)) {
+        LOG(ERROR) << "GCM encryption but no nonce generated";
+        return false;
+    }
+    // nonceBlob here is just a pointer into existing data, must not be freed
+    std::string nonce(reinterpret_cast<const char *>(nonceBlob.data), nonceBlob.data_length);
+    if (!checkSize("nonce", nonce.size(), GCM_NONCE_BYTES)) return false;
+    std::string body;
+    if (!opHandle.updateCompletely(message, body)) return false;
+
+    std::string mac;
+    if (!opHandle.finishWithOutput(mac)) return false;
+    if (!checkSize("mac", mac.size(), GCM_MAC_BYTES)) return false;
+    ciphertext = nonce + body + mac;
+    return true;
+}
+
+static bool decryptWithKeymasterKey(
+        Keymaster &keymaster, const std::string &key,
+        const keymaster::AuthorizationSet &extraParams,
+        const std::string &ciphertext,
+        std::string &message) {
+    auto nonce = ciphertext.substr(0, GCM_NONCE_BYTES);
+    auto bodyAndMac = ciphertext.substr(GCM_NONCE_BYTES);
+    // FIXME fix repetition
+    auto params = addStringParam(keymaster::AuthorizationSetBuilder(), keymaster::TAG_NONCE, nonce)
+        .Authorization(keymaster::TAG_BLOCK_MODE, KM_MODE_GCM)
+        .Authorization(keymaster::TAG_MAC_LENGTH, GCM_MAC_BYTES * 8)
+        .Authorization(keymaster::TAG_PADDING, KM_PAD_NONE)
+        .build();
+    params.push_back(extraParams);
+
+    auto opHandle = keymaster.begin(KM_PURPOSE_DECRYPT, key, params);
+    if (!opHandle) return false;
+    if (!opHandle.updateCompletely(bodyAndMac, message)) return false;
+    if (!opHandle.finish()) return false;
+    return true;
+}
+
+static bool readFileToString(const std::string &filename, std::string &result) {
+    if (!android::base::ReadFileToString(filename, &result)) {
+         PLOG(ERROR) << "Failed to read from " << filename;
+         return false;
+    }
+    return true;
+}
+
+static bool writeStringToFile(const std::string &payload, const std::string &filename) {
+    if (!android::base::WriteStringToFile(payload, filename)) {
+         PLOG(ERROR) << "Failed to write to " << filename;
+         return false;
+    }
+    return true;
+}
+
+bool storeKey(const std::string &dir, const std::string &key) {
+    if (TEMP_FAILURE_RETRY(mkdir(dir.c_str(), 0700)) == -1) {
+        PLOG(ERROR) << "key mkdir " << dir;
+        return false;
+    }
+    std::string secdiscardable;
+    if (ReadRandomBytes(SECDISCARDABLE_BYTES, secdiscardable) != OK) {
+        // TODO status_t plays badly with PLOG, fix it.
+        LOG(ERROR) << "Random read failed";
+        return false;
+    }
+    if (!writeStringToFile(secdiscardable, dir + "/" + kFn_secdiscardable)) return false;
+    auto extraParams = addStringParam(keymaster::AuthorizationSetBuilder(),
+            keymaster::TAG_APPLICATION_ID, hashSecdiscardable(secdiscardable)).build();
+    Keymaster keymaster;
+    if (!keymaster) return false;
+    std::string kmKey;
+    if (!generateKeymasterKey(keymaster, extraParams, kmKey)) return false;
+    std::string encryptedKey;
+    if (!encryptWithKeymasterKey(
+        keymaster, kmKey, extraParams, key, encryptedKey)) return false;
+    if (!writeStringToFile(kmKey, dir + "/" + kFn_keymaster_key_blob)) return false;
+    if (!writeStringToFile(encryptedKey, dir + "/" + kFn_encrypted_key)) return false;
+    return true;
+}
+
+bool retrieveKey(const std::string &dir, std::string &key) {
+    std::string secdiscardable;
+    if (!readFileToString(dir + "/" + kFn_secdiscardable, secdiscardable)) return false;
+    auto extraParams = addStringParam(keymaster::AuthorizationSetBuilder(),
+            keymaster::TAG_APPLICATION_ID, hashSecdiscardable(secdiscardable)).build();
+    std::string kmKey;
+    if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, kmKey)) return false;
+    std::string encryptedMessage;
+    if (!readFileToString(dir + "/" + kFn_encrypted_key, encryptedMessage)) return false;
+    Keymaster keymaster;
+    if (!keymaster) return false;
+    return decryptWithKeymasterKey(keymaster, kmKey, extraParams, encryptedMessage, key);
+}
+
+static bool deleteKey(const std::string &dir) {
+    std::string kmKey;
+    if (!readFileToString(dir + "/" + kFn_keymaster_key_blob, kmKey)) return false;
+    Keymaster keymaster;
+    if (!keymaster) return false;
+    if (!keymaster.deleteKey(kmKey)) return false;
+    return true;
+}
+
+static bool secdiscardSecdiscardable(const std::string &dir) {
+    if (ForkExecvp(std::vector<std::string> {
+            kSecdiscardPath, "--", dir + "/" + kFn_secdiscardable}) != 0) {
+        LOG(ERROR) << "secdiscard failed";
+        return false;
+    }
+    return true;
+}
+
+static bool recursiveDeleteKey(const std::string &dir) {
+    if (ForkExecvp(std::vector<std::string> {
+            kRmPath, "-rf", dir}) != 0) {
+        LOG(ERROR) << "recursive delete failed";
+        return false;
+    }
+    return true;
+}
+
+bool destroyKey(const std::string &dir) {
+    bool success = true;
+    // Try each thing, even if previous things failed.
+    success &= deleteKey(dir);
+    success &= secdiscardSecdiscardable(dir);
+    success &= recursiveDeleteKey(dir);
+    return success;
+}
+
+}  // namespace vold
+}  // namespace android
diff --git a/KeyStorage.h b/KeyStorage.h
new file mode 100644
index 0000000..a35349c
--- /dev/null
+++ b/KeyStorage.h
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2016 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 ANDROID_VOLD_KEYSTORAGE_H
+#define ANDROID_VOLD_KEYSTORAGE_H
+
+#include <string>
+
+namespace android {
+namespace vold {
+
+// Create a directory at the named path, and store "key" in it,
+// in such a way that it can only be retrieved via Keymaster and
+// can be securely deleted.
+// It's safe to move/rename the directory after creation.
+bool storeKey(const std::string &dir, const std::string &key);
+
+// Retrieve the key from the named directory.
+bool retrieveKey(const std::string &dir, std::string &key);
+
+// Securely destroy the key stored in the named directory and delete the directory.
+bool destroyKey(const std::string &dir);
+
+}  // namespace vold
+}  // namespace android
+
+#endif
diff --git a/Keymaster.cpp b/Keymaster.cpp
new file mode 100644
index 0000000..0fde8fa
--- /dev/null
+++ b/Keymaster.cpp
@@ -0,0 +1,159 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+#include "Keymaster.h"
+
+#include <android-base/logging.h>
+
+namespace android {
+namespace vold {
+
+bool KeymasterOperation::updateCompletely(
+        const std::string &input,
+        std::string &output) {
+    output.clear();
+    auto it = input.begin();
+    while (it != input.end()) {
+        size_t toRead = static_cast<size_t>(input.end() - it);
+        keymaster_blob_t inputBlob {reinterpret_cast<const uint8_t *>(&*it),  toRead};
+        keymaster_blob_t outputBlob;
+        size_t inputConsumed;
+        auto error = mDevice->update(mDevice, mOpHandle,
+            nullptr, &inputBlob, &inputConsumed, nullptr, &outputBlob);
+        if (error != KM_ERROR_OK) {
+            LOG(ERROR) << "update failed, code " << error;
+            mDevice = nullptr;
+            return false;
+        }
+        output.append(reinterpret_cast<const char *>(outputBlob.data), outputBlob.data_length);
+        free(const_cast<uint8_t *>(outputBlob.data));
+        if (inputConsumed > toRead) {
+            LOG(ERROR) << "update reported too much input consumed";
+            mDevice = nullptr;
+            return false;
+        }
+        it += inputConsumed;
+    }
+    return true;
+}
+
+bool KeymasterOperation::finish() {
+    auto error = mDevice->finish(mDevice, mOpHandle,
+        nullptr, nullptr, nullptr, nullptr);
+    mDevice = nullptr;
+    if (error != KM_ERROR_OK) {
+        LOG(ERROR) << "finish failed, code " << error;
+        return false;
+    }
+    return true;
+}
+
+bool KeymasterOperation::finishWithOutput(std::string &output) {
+    keymaster_blob_t outputBlob;
+    auto error = mDevice->finish(mDevice, mOpHandle,
+        nullptr, nullptr, nullptr, &outputBlob);
+    mDevice = nullptr;
+    if (error != KM_ERROR_OK) {
+        LOG(ERROR) << "finish failed, code " << error;
+        return false;
+    }
+    output.assign(reinterpret_cast<const char *>(outputBlob.data), outputBlob.data_length);
+    free(const_cast<uint8_t *>(outputBlob.data));
+    return true;
+}
+
+Keymaster::Keymaster() {
+    mDevice = nullptr;
+    const hw_module_t *module;
+    int ret = hw_get_module_by_class(KEYSTORE_HARDWARE_MODULE_ID, NULL, &module);
+    if (ret != 0) {
+        LOG(ERROR) << "hw_get_module_by_class returned " << ret;
+        return;
+    }
+    // TODO: This will need to be updated to support keymaster2.
+    if (module->module_api_version != KEYMASTER_MODULE_API_VERSION_1_0) {
+        LOG(ERROR) << "module_api_version is " << module->module_api_version;
+        return;
+    }
+    ret = keymaster1_open(module, &mDevice);
+    if (ret != 0) {
+        LOG(ERROR) << "keymaster1_open returned " << ret;
+        mDevice = nullptr;
+        return;
+    }
+}
+
+bool Keymaster::generateKey(
+        const keymaster::AuthorizationSet &inParams,
+        std::string &key) {
+    keymaster_key_blob_t keyBlob;
+    auto error = mDevice->generate_key(mDevice, &inParams, &keyBlob, nullptr);
+    if (error != KM_ERROR_OK) {
+        LOG(ERROR) << "generate_key failed, code " << error;
+        return false;
+    }
+    key.assign(reinterpret_cast<const char *>(keyBlob.key_material), keyBlob.key_material_size);
+    return true;
+}
+
+bool Keymaster::deleteKey(const std::string &key) {
+    if (mDevice->delete_key == nullptr) return true;
+    keymaster_key_blob_t keyBlob { reinterpret_cast<const uint8_t *>(key.data()), key.size() };
+    auto error = mDevice->delete_key(mDevice, &keyBlob);
+    if (error != KM_ERROR_OK) {
+        LOG(ERROR) << "delete_key failed, code " << error;
+        return false;
+    }
+    return true;
+}
+
+KeymasterOperation Keymaster::begin(
+        keymaster_purpose_t purpose,
+        const std::string &key,
+        const keymaster::AuthorizationSet &inParams,
+        keymaster::AuthorizationSet &outParams) {
+    keymaster_key_blob_t keyBlob { reinterpret_cast<const uint8_t *>(key.data()), key.size() };
+    keymaster_operation_handle_t mOpHandle;
+    keymaster_key_param_set_t outParams_set;
+    auto error = mDevice->begin(mDevice, purpose,
+        &keyBlob, &inParams, &outParams_set, &mOpHandle);
+    if (error != KM_ERROR_OK) {
+        LOG(ERROR) << "begin failed, code " << error;
+        return KeymasterOperation(nullptr, mOpHandle);
+    }
+    outParams.Clear();
+    outParams.push_back(outParams_set);
+    keymaster_free_param_set(&outParams_set);
+    return KeymasterOperation(mDevice, mOpHandle);
+}
+
+KeymasterOperation Keymaster::begin(
+        keymaster_purpose_t purpose,
+        const std::string &key,
+        const keymaster::AuthorizationSet &inParams) {
+    keymaster_key_blob_t keyBlob { reinterpret_cast<const uint8_t *>(key.data()), key.size() };
+    keymaster_operation_handle_t mOpHandle;
+    auto error = mDevice->begin(mDevice, purpose,
+        &keyBlob, &inParams, nullptr, &mOpHandle);
+    if (error != KM_ERROR_OK) {
+        LOG(ERROR) << "begin failed, code " << error;
+        return KeymasterOperation(nullptr, mOpHandle);
+    }
+    return KeymasterOperation(mDevice, mOpHandle);
+}
+
+}  // namespace vold
+}  // namespace android
diff --git a/Keymaster.h b/Keymaster.h
new file mode 100644
index 0000000..003baa6
--- /dev/null
+++ b/Keymaster.h
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2016 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 ANDROID_VOLD_KEYMASTER1_H
+#define ANDROID_VOLD_KEYMASTER1_H
+
+#include <string>
+
+#include <hardware/hardware.h>
+#include <hardware/keymaster1.h>
+
+#include <keymaster/authorization_set.h>
+
+namespace android {
+namespace vold {
+
+using namespace keymaster;
+
+// C++ wrappers to the keymaster1 C interface.
+// This is tailored to the needs of KeyStorage, but could be extended to be
+// a more general interface.
+
+
+// Wrapper for a keymaster_operation_handle_t representing an
+// ongoing Keymaster operation.  Aborts the operation
+// in the destructor if it is unfinished. Methods log failures
+// to LOG(ERROR).
+class KeymasterOperation {
+public:
+    ~KeymasterOperation() { if (mDevice) mDevice->abort(mDevice, mOpHandle); }
+    // Is this instance valid? This is false if creation fails, and becomes
+    // false on finish or if an update fails.
+    explicit operator bool() {return mDevice != nullptr;}
+    // Call "update" repeatedly until all of the input is consumed, and
+    // concatenate the output. Return true on success.
+    bool updateCompletely(const std::string &input, std::string &output);
+    // Finish; pass nullptr for the "output" param.
+    bool finish();
+    // Finish and write the output to this string.
+    bool finishWithOutput(std::string &output);
+    // Move constructor
+    KeymasterOperation(KeymasterOperation&& rhs) {
+        mOpHandle = rhs.mOpHandle;
+        mDevice = rhs.mDevice;
+        rhs.mDevice = nullptr;
+    }
+private:
+    KeymasterOperation(keymaster1_device_t *d, keymaster_operation_handle_t h):
+        mDevice {d}, mOpHandle {h} {}
+    keymaster1_device_t *mDevice;
+    keymaster_operation_handle_t mOpHandle;
+    DISALLOW_COPY_AND_ASSIGN(KeymasterOperation);
+    friend class Keymaster;
+};
+
+// Wrapper for a keymaster1_device_t representing an open connection
+// to the keymaster, which is closed in the destructor.
+class Keymaster {
+public:
+    Keymaster();
+    ~Keymaster() { if (mDevice) keymaster1_close(mDevice); }
+    // false if we failed to open the keymaster device.
+    explicit operator bool() {return mDevice != nullptr;}
+    // Generate a key in the keymaster from the given params.
+    bool generateKey(const AuthorizationSet &inParams, std::string &key);
+    // If the keymaster supports it, permanently delete a key.
+    bool deleteKey(const std::string &key);
+    // Begin a new cryptographic operation, collecting output parameters.
+    KeymasterOperation begin(
+            keymaster_purpose_t purpose,
+            const std::string &key,
+            const AuthorizationSet &inParams,
+            AuthorizationSet &outParams);
+    // Begin a new cryptographic operation; don't collect output parameters.
+    KeymasterOperation begin(
+            keymaster_purpose_t purpose,
+            const std::string &key,
+            const AuthorizationSet &inParams);
+private:
+    keymaster1_device_t *mDevice;
+    DISALLOW_COPY_AND_ASSIGN(Keymaster);
+};
+
+template <keymaster_tag_t Tag>
+inline AuthorizationSetBuilder& addStringParam(AuthorizationSetBuilder &&params,
+        TypedTag<KM_BYTES, Tag> tag, const std::string& val) {
+    return params.Authorization(tag, val.data(), val.size());
+}
+
+}  // namespace vold
+}  // namespace android
+
+#endif
diff --git a/PrivateVolume.cpp b/PrivateVolume.cpp
index a106481..21746b2 100644
--- a/PrivateVolume.cpp
+++ b/PrivateVolume.cpp
@@ -158,6 +158,7 @@
     // Verify that common directories are ready to roll
     if (PrepareDir(mPath + "/app", 0771, AID_SYSTEM, AID_SYSTEM) ||
             PrepareDir(mPath + "/user", 0711, AID_SYSTEM, AID_SYSTEM) ||
+            PrepareDir(mPath + "/user_de", 0711, AID_SYSTEM, AID_SYSTEM) ||
             PrepareDir(mPath + "/media", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
             PrepareDir(mPath + "/media/0", 0770, AID_MEDIA_RW, AID_MEDIA_RW) ||
             PrepareDir(mPath + "/local", 0751, AID_ROOT, AID_ROOT) ||
diff --git a/Utils.cpp b/Utils.cpp
index e4f473a..aba3a53 100644
--- a/Utils.cpp
+++ b/Utils.cpp
@@ -547,10 +547,61 @@
     return res;
 }
 
+static bool isValidFilename(const std::string& name) {
+    if (name.empty() || (name == ".") || (name == "..")
+            || (name.find('/') != std::string::npos)) {
+        return false;
+    } else {
+        return true;
+    }
+}
+
 std::string BuildKeyPath(const std::string& partGuid) {
     return StringPrintf("%s/expand_%s.key", kKeyPath, partGuid.c_str());
 }
 
+std::string BuildDataSystemCePath(userid_t userId) {
+    // TODO: unify with installd path generation logic
+    std::string data(BuildDataPath(nullptr));
+    return StringPrintf("%s/system_ce/%u", data.c_str(), userId);
+}
+
+std::string BuildDataPath(const char* volumeUuid) {
+    // TODO: unify with installd path generation logic
+    if (volumeUuid == nullptr) {
+        return "/data";
+    } else {
+        CHECK(isValidFilename(volumeUuid));
+        return StringPrintf("/mnt/expand/%s", volumeUuid);
+    }
+}
+
+std::string BuildDataMediaPath(const char* volumeUuid, userid_t userId) {
+    // TODO: unify with installd path generation logic
+    std::string data(BuildDataPath(volumeUuid));
+    return StringPrintf("%s/media/%u", data.c_str(), userId);
+}
+
+std::string BuildDataUserPath(const char* volumeUuid, userid_t userId) {
+    // TODO: unify with installd path generation logic
+    std::string data(BuildDataPath(volumeUuid));
+    if (volumeUuid == nullptr) {
+        if (userId == 0) {
+            return StringPrintf("%s/data", data.c_str());
+        } else {
+            return StringPrintf("%s/user/%u", data.c_str(), userId);
+        }
+    } else {
+        return StringPrintf("%s/user/%u", data.c_str(), userId);
+    }
+}
+
+std::string BuildDataUserDePath(const char* volumeUuid, userid_t userId) {
+    // TODO: unify with installd path generation logic
+    std::string data(BuildDataPath(volumeUuid));
+    return StringPrintf("%s/user_de/%u", data.c_str(), userId);
+}
+
 dev_t GetDevice(const std::string& path) {
     struct stat sb;
     if (stat(path.c_str(), &sb)) {
diff --git a/Utils.h b/Utils.h
index 228727a..0a74af7 100644
--- a/Utils.h
+++ b/Utils.h
@@ -18,6 +18,7 @@
 #define ANDROID_VOLD_UTILS_H
 
 #include <utils/Errors.h>
+#include <cutils/multiuser.h>
 #include <selinux/selinux.h>
 
 #include <vector>
@@ -93,6 +94,13 @@
 
 std::string BuildKeyPath(const std::string& partGuid);
 
+std::string BuildDataSystemCePath(userid_t userid);
+
+std::string BuildDataPath(const char* volumeUuid);
+std::string BuildDataMediaPath(const char* volumeUuid, userid_t userid);
+std::string BuildDataUserPath(const char* volumeUuid, userid_t userid);
+std::string BuildDataUserDePath(const char* volumeUuid, userid_t userid);
+
 dev_t GetDevice(const std::string& path);
 
 std::string DefaultFstabPath();
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
old mode 100755
new mode 100644
diff --git a/cryptfs.c b/cryptfs.c
index 7ca05b0..c1020cd 100644
--- a/cryptfs.c
+++ b/cryptfs.c
@@ -83,6 +83,10 @@
 
 #define DEFAULT_PASSWORD "default_password"
 
+#define CRYPTO_BLOCK_DEVICE "userdata"
+
+#define BREADCRUMB_FILE "/data/misc/vold/convert_fde"
+
 #define EXT4_FS 1
 #define F2FS_FS 2
 
@@ -190,6 +194,11 @@
     keymaster0_device_t *keymaster0_dev = 0;
     keymaster1_device_t *keymaster1_dev = 0;
 
+    if (ftr->keymaster_blob_size) {
+        SLOGI("Already have key");
+        return 0;
+    }
+
     if (keymaster_init(&keymaster0_dev, &keymaster1_dev)) {
         SLOGE("Failed to init keymaster");
         return -1;
@@ -599,6 +608,16 @@
   return rc;
 }
 
+/* Set sha256 checksum in structure */
+static void set_ftr_sha(struct crypt_mnt_ftr *crypt_ftr)
+{
+    SHA256_CTX c;
+    SHA256_Init(&c);
+    memset(crypt_ftr->sha256, 0, sizeof(crypt_ftr->sha256));
+    SHA256_Update(&c, crypt_ftr, sizeof(*crypt_ftr));
+    SHA256_Final(crypt_ftr->sha256, &c);
+}
+
 /* key or salt can be NULL, in which case just skip writing that value.  Useful to
  * update the failed mount count but not change the key.
  */
@@ -614,6 +633,8 @@
   char *fname = NULL;
   struct stat statbuf;
 
+  set_ftr_sha(crypt_ftr);
+
   if (get_crypt_ftr_info(&fname, &starting_off)) {
     SLOGE("Unable to get crypt_ftr_info\n");
     return -1;
@@ -656,6 +677,14 @@
 
 }
 
+static bool check_ftr_sha(const struct crypt_mnt_ftr *crypt_ftr)
+{
+    struct crypt_mnt_ftr copy;
+    memcpy(&copy, crypt_ftr, sizeof(copy));
+    set_ftr_sha(&copy);
+    return memcmp(copy.sha256, crypt_ftr->sha256, sizeof(copy.sha256)) == 0;
+}
+
 static inline int unix_read(int  fd, void*  buff, int  len)
 {
     return TEMP_FAILURE_RETRY(read(fd, buff, len));
@@ -2052,13 +2081,41 @@
     int rc;
 
     rc = check_unmounted_and_get_ftr(&crypt_ftr);
-    if (rc)
+    if (rc) {
+        SLOGE("Could not get footer");
         return rc;
+    }
 
     rc = test_mount_encrypted_fs(&crypt_ftr, passwd,
-                                 DATA_MNT_POINT, "userdata");
+                                 DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
+    if (rc) {
+        SLOGE("Password did not match");
+        return rc;
+    }
 
-    if (rc == 0 && crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
+    if (crypt_ftr.flags & CRYPT_FORCE_COMPLETE) {
+        // Here we have a default actual password but a real password
+        // we must test against the scrypted value
+        // First, we must delete the crypto block device that
+        // test_mount_encrypted_fs leaves behind as a side effect
+        delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
+        rc = test_mount_encrypted_fs(&crypt_ftr, DEFAULT_PASSWORD,
+                                     DATA_MNT_POINT, CRYPTO_BLOCK_DEVICE);
+        if (rc) {
+            SLOGE("Default password did not match on reboot encryption");
+            return rc;
+        }
+
+        crypt_ftr.flags &= ~CRYPT_FORCE_COMPLETE;
+        put_crypt_ftr_and_key(&crypt_ftr);
+        rc = cryptfs_changepw(crypt_ftr.crypt_type, passwd);
+        if (rc) {
+            SLOGE("Could not change password on reboot encryption");
+            return rc;
+        }
+    }
+
+    if (crypt_ftr.crypt_type != CRYPT_TYPE_DEFAULT) {
         cryptfs_clear_password();
         password = strdup(passwd);
         struct timespec now;
@@ -2931,6 +2988,7 @@
     char key_loc[PROPERTY_VALUE_MAX];
     int num_vols;
     off64_t previously_encrypted_upto = 0;
+    bool rebootEncryption = false;
 
     if (!strcmp(howarg, "wipe")) {
       how = CRYPTO_ENABLE_WIPE;
@@ -2941,21 +2999,33 @@
       goto error_unencrypted;
     }
 
-    /* See if an encryption was underway and interrupted */
     if (how == CRYPTO_ENABLE_INPLACE
-          && get_crypt_ftr_and_key(&crypt_ftr) == 0
-          && (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS)) {
-        previously_encrypted_upto = crypt_ftr.encrypted_upto;
-        crypt_ftr.encrypted_upto = 0;
-        crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
+          && get_crypt_ftr_and_key(&crypt_ftr) == 0) {
+        if (crypt_ftr.flags & CRYPT_ENCRYPTION_IN_PROGRESS) {
+            /* An encryption was underway and was interrupted */
+            previously_encrypted_upto = crypt_ftr.encrypted_upto;
+            crypt_ftr.encrypted_upto = 0;
+            crypt_ftr.flags &= ~CRYPT_ENCRYPTION_IN_PROGRESS;
 
-        /* At this point, we are in an inconsistent state. Until we successfully
-           complete encryption, a reboot will leave us broken. So mark the
-           encryption failed in case that happens.
-           On successfully completing encryption, remove this flag */
-        crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
+            /* At this point, we are in an inconsistent state. Until we successfully
+               complete encryption, a reboot will leave us broken. So mark the
+               encryption failed in case that happens.
+               On successfully completing encryption, remove this flag */
+            crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
 
-        put_crypt_ftr_and_key(&crypt_ftr);
+            put_crypt_ftr_and_key(&crypt_ftr);
+        } else if (crypt_ftr.flags & CRYPT_FORCE_ENCRYPTION) {
+            if (!check_ftr_sha(&crypt_ftr)) {
+                memset(&crypt_ftr, 0, sizeof(crypt_ftr));
+                put_crypt_ftr_and_key(&crypt_ftr);
+                goto error_unencrypted;
+            }
+
+            /* Doing a reboot-encryption*/
+            crypt_ftr.flags &= ~CRYPT_FORCE_ENCRYPTION;
+            crypt_ftr.flags |= CRYPT_FORCE_COMPLETE;
+            rebootEncryption = true;
+        }
     }
 
     property_get("ro.crypto.state", encrypted_state, "");
@@ -3015,13 +3085,23 @@
         SLOGE("Failed to unmount all vold managed devices");
     }
 
-    /* Now unmount the /data partition. */
-    if (wait_and_unmount(DATA_MNT_POINT, false)) {
-        goto error_unencrypted;
+    /* no_ui means we are being called from init, not settings.
+       Now we always reboot from settings, so !no_ui means reboot
+     */
+    bool onlyCreateHeader = false;
+    if (!no_ui) {
+        /* Try fallback, which is to reboot and try there */
+        onlyCreateHeader = true;
+        FILE* breadcrumb = fopen(BREADCRUMB_FILE, "we");
+        if (breadcrumb == 0) {
+            SLOGE("Failed to create breadcrumb file");
+            goto error_shutting_down;
+        }
+        fclose(breadcrumb);
     }
 
     /* Do extra work for a better UX when doing the long inplace encryption */
-    if (how == CRYPTO_ENABLE_INPLACE) {
+    if (how == CRYPTO_ENABLE_INPLACE && !onlyCreateHeader) {
         /* Now that /data is unmounted, we need to mount a tmpfs
          * /data, set a property saying we're doing inplace encryption,
          * and restart the framework.
@@ -3048,7 +3128,7 @@
 
     /* Start the actual work of making an encrypted filesystem */
     /* Initialize a crypt_mnt_ftr for the partition */
-    if (previously_encrypted_upto == 0) {
+    if (previously_encrypted_upto == 0 && !rebootEncryption) {
         if (cryptfs_init_crypt_mnt_ftr(&crypt_ftr)) {
             goto error_shutting_down;
         }
@@ -3063,7 +3143,11 @@
            complete encryption, a reboot will leave us broken. So mark the
            encryption failed in case that happens.
            On successfully completing encryption, remove this flag */
-        crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
+        if (onlyCreateHeader) {
+            crypt_ftr.flags |= CRYPT_FORCE_ENCRYPTION;
+        } else {
+            crypt_ftr.flags |= CRYPT_INCONSISTENT_STATE;
+        }
         crypt_ftr.crypt_type = crypt_type;
 #ifndef CONFIG_HW_DISK_ENCRYPTION
         strlcpy((char *)crypt_ftr.crypto_type_name, "aes-cbc-essiv:sha256", MAX_CRYPTO_TYPE_NAME_LEN);
@@ -3084,11 +3168,21 @@
 #endif
 
         /* Make an encrypted master key */
-        if (create_encrypted_random_key(passwd, crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
+        if (create_encrypted_random_key(onlyCreateHeader ? DEFAULT_PASSWORD : passwd,
+                                        crypt_ftr.master_key, crypt_ftr.salt, &crypt_ftr)) {
             SLOGE("Cannot create encrypted master key\n");
             goto error_shutting_down;
         }
 
+        /* Replace scrypted intermediate key if we are preparing for a reboot */
+        if (onlyCreateHeader) {
+            unsigned char fake_master_key[KEY_LEN_BYTES];
+            unsigned char encrypted_fake_master_key[KEY_LEN_BYTES];
+            memset(fake_master_key, 0, sizeof(fake_master_key));
+            encrypt_master_key(passwd, crypt_ftr.salt, fake_master_key,
+                               encrypted_fake_master_key, &crypt_ftr);
+        }
+
         /* Write the key to the end of the partition */
         put_crypt_ftr_and_key(&crypt_ftr);
 
@@ -3107,7 +3201,12 @@
         }
     }
 
-    if (how == CRYPTO_ENABLE_INPLACE && !no_ui) {
+    if (onlyCreateHeader) {
+        sleep(2);
+        cryptfs_reboot(reboot);
+    }
+
+    if (how == CRYPTO_ENABLE_INPLACE && (!no_ui || rebootEncryption)) {
         /* startup service classes main and late_start */
         property_set("vold.decrypt", "trigger_restart_min_framework");
         SLOGD("Just triggered restart_min_framework\n");
@@ -3121,7 +3220,7 @@
 
     decrypt_master_key(passwd, decrypted_master_key, &crypt_ftr, 0, 0);
     create_crypto_blk_dev(&crypt_ftr, decrypted_master_key, real_blkdev, crypto_blkdev,
-                          "userdata");
+                          CRYPTO_BLOCK_DEVICE);
 
     /* If we are continuing, check checksums match */
     rc = 0;
@@ -3154,7 +3253,7 @@
     }
 
     /* Undo the dm-crypt mapping whether we succeed or not */
-    delete_crypto_blk_dev("userdata");
+    delete_crypto_blk_dev(CRYPTO_BLOCK_DEVICE);
 
     if (! rc) {
         /* Success */
@@ -3177,8 +3276,16 @@
             /* default encryption - continue first boot sequence */
             property_set("ro.crypto.state", "encrypted");
             release_wake_lock(lockid);
-            cryptfs_check_passwd(DEFAULT_PASSWORD);
-            cryptfs_restart_internal(1);
+            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");
+                sleep(2);
+                property_set("vold.encrypt_progress", "");
+                cryptfs_trigger_restart_min_framework();
+            } else {
+                cryptfs_check_passwd(DEFAULT_PASSWORD);
+                cryptfs_restart_internal(1);
+            }
             return 0;
           } else {
             sleep(2); /* Give the UI a chance to show 100% progress */
@@ -3729,6 +3836,12 @@
     return e4crypt_enable(DATA_MNT_POINT);
 }
 
+int cryptfs_isConvertibleToFBE()
+{
+    struct fstab_rec* rec = fs_mgr_get_entry_for_mount_point(fstab, DATA_MNT_POINT);
+    return fs_mgr_is_convertible_to_fbe(rec) ? 1 : 0;
+}
+
 int cryptfs_create_default_ftr(struct crypt_mnt_ftr* crypt_ftr, __attribute__((unused))int key_length)
 {
     if (cryptfs_init_crypt_mnt_ftr(crypt_ftr)) {
diff --git a/cryptfs.h b/cryptfs.h
index fd6f3da..033767f 100644
--- a/cryptfs.h
+++ b/cryptfs.h
@@ -52,6 +52,16 @@
                                         correctly marked partial encryption */
 #define CRYPT_DATA_CORRUPT 0x8 /* Set when encryption is fine, but the
                                   underlying volume is corrupt */
+#define CRYPT_FORCE_ENCRYPTION 0x10 /* Set when it is time to encrypt this
+                                       volume on boot. Everything in this
+                                       structure is set up correctly as
+                                       though device is encrypted except
+                                       that the master key is encrypted with the
+                                       default password. */
+#define CRYPT_FORCE_COMPLETE 0x20 /* Set when the above encryption cycle is
+                                     complete. On next cryptkeeper entry, match
+                                     the password. If it matches fix the master
+                                     key and remove this flag. */
 
 /* Allowed values for type in the structure below */
 #define CRYPT_TYPE_PASSWORD 0 /* master_key is encrypted with a password
@@ -94,7 +104,7 @@
   __le32 keysize;       /* in bytes */
   __le32 crypt_type;    /* how master_key is encrypted. Must be a
                          * CRYPT_TYPE_XXX value */
-  __le64 fs_size;	/* Size of the encrypted fs, in 512 byte sectors */
+  __le64 fs_size;       /* Size of the encrypted fs, in 512 byte sectors */
   __le32 failed_decrypt_count; /* count of # of failed attempts to decrypt and
                                   mount, set to 0 on successful mount */
   unsigned char crypto_type_name[MAX_CRYPTO_TYPE_NAME_LEN]; /* The type of encryption
@@ -145,6 +155,12 @@
      then we will be OK.
    */
   unsigned char scrypted_intermediate_key[SCRYPT_LEN];
+
+  /* sha of this structure with this element set to zero
+     Used when encrypting on reboot to validate structure before doing something
+     fatal
+   */
+  unsigned char sha256[SHA256_DIGEST_LENGTH];
 };
 
 /* Persistant data that should be available before decryption.
@@ -231,6 +247,7 @@
   int cryptfs_get_password_type(void);
   const char* cryptfs_get_password(void);
   void cryptfs_clear_password(void);
+  int cryptfs_isConvertibleToFBE(void);
 
   // Functions for file encryption to use to inherit our encryption logic
   int cryptfs_create_default_ftr(struct crypt_mnt_ftr* ftr, int key_length);
@@ -238,6 +255,7 @@
                              unsigned char* master_key);
   int cryptfs_set_password(struct crypt_mnt_ftr* ftr, const char* password,
                            const unsigned char* master_key);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/fs/F2fs.cpp b/fs/F2fs.cpp
index 84d27c4..0d12b07 100644
--- a/fs/F2fs.cpp
+++ b/fs/F2fs.cpp
@@ -43,7 +43,7 @@
 status_t Check(const std::string& source) {
     std::vector<std::string> cmd;
     cmd.push_back(kFsckPath);
-    cmd.push_back("-f");
+    cmd.push_back("-a");
     cmd.push_back(source);
 
     // f2fs devices are currently always trusted
diff --git a/main.cpp b/main.cpp
index 9cbcf88..6d94818 100644
--- a/main.cpp
+++ b/main.cpp
@@ -18,6 +18,7 @@
 #include "VolumeManager.h"
 #include "CommandListener.h"
 #include "CryptCommandListener.h"
+#include "Ext4Crypt.h"
 #include "NetlinkManager.h"
 #include "cryptfs.h"
 #include "sehandle.h"
diff --git a/secdiscard.cpp b/secdiscard.cpp
index 3f4ab2e..5c12cdd 100644
--- a/secdiscard.cpp
+++ b/secdiscard.cpp
@@ -14,7 +14,9 @@
  * limitations under the License.
  */
 
+#include <memory>
 #include <string>
+#include <vector>
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -24,213 +26,184 @@
 #include <fcntl.h>
 #include <linux/fs.h>
 #include <linux/fiemap.h>
+#include <mntent.h>
 
-#define LOG_TAG "secdiscard"
-#include "cutils/log.h"
+#include <android-base/logging.h>
 
-// Deliberately limit ourselves to wiping small files.
-#define MAX_WIPE_LENGTH 4096
-#define INIT_BUFFER_SIZE 2048
+#include <AutoCloseFD.h>
 
-static void usage(char *progname);
-static void destroy_key(const std::string &path);
-static int file_device_range(const std::string &path, uint64_t range[2]);
-static int open_block_device_for_path(const std::string &path);
-static int read_file_as_string_atomically(const std::string &path, std::string &contents);
-static int find_block_device_for_path(
-    const std::string &mounts,
-    const std::string &path,
-    std::string &block_device);
+namespace {
 
-int main(int argc, char **argv) {
-    if (argc != 2 || argv[1][0] != '/') {
+struct Options {
+    std::vector<std::string> targets;
+    bool unlink{true};
+};
+
+constexpr uint32_t max_extents = 32;
+
+bool read_command_line(int argc, const char * const argv[], Options &options);
+void usage(const char *progname);
+int secdiscard_path(const std::string &path);
+std::unique_ptr<struct fiemap> path_fiemap(const std::string &path, uint32_t extent_count);
+bool check_fiemap(const struct fiemap &fiemap, const std::string &path);
+std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count);
+std::string block_device_for_path(const std::string &path);
+
+}
+
+int main(int argc, const char * const argv[]) {
+    android::base::InitLogging(const_cast<char **>(argv));
+    Options options;
+    if (!read_command_line(argc, argv, options)) {
         usage(argv[0]);
         return -1;
     }
-    SLOGD("Running: %s %s", argv[0], argv[1]);
-    std::string target(argv[1]);
-    destroy_key(target);
-    if (unlink(argv[1]) != 0 && errno != ENOENT) {
-        SLOGE("Unable to delete %s: %s",
-            argv[1], strerror(errno));
-        return -1;
+    for (auto target: options.targets) {
+        LOG(DEBUG) << "Securely discarding '" << target << "' unlink=" << options.unlink;
+        secdiscard_path(target);
+        if (options.unlink) {
+            if (unlink(target.c_str()) != 0 && errno != ENOENT) {
+                PLOG(ERROR) << "Unable to unlink: " << target;
+            }
+        }
+        LOG(DEBUG) << "Discarded: " << target;
     }
     return 0;
 }
 
-static void usage(char *progname) {
-    fprintf(stderr, "Usage: %s <absolute path>\n", progname);
+namespace {
+
+bool read_command_line(int argc, const char * const argv[], Options &options) {
+    for (int i = 1; i < argc; i++) {
+        if (!strcmp("--no-unlink", argv[i])) {
+            options.unlink = false;
+        } else if (!strcmp("--", argv[i])) {
+            for (int j = i+1; j < argc; j++) {
+                if (argv[j][0] != '/') return false; // Must be absolute path
+                options.targets.emplace_back(argv[j]);
+            }
+            return options.targets.size() > 0;
+        } else {
+            return false; // Unknown option
+        }
+    }
+    return false; // "--" not found
+}
+
+void usage(const char *progname) {
+    fprintf(stderr, "Usage: %s [--no-unlink] -- <absolute path> ...\n", progname);
 }
 
 // BLKSECDISCARD all content in "path", if it's small enough.
-static void destroy_key(const std::string &path) {
-    uint64_t range[2];
-    if (file_device_range(path, range) < 0) {
-        return;
+int secdiscard_path(const std::string &path) {
+    auto fiemap = path_fiemap(path, max_extents);
+    if (!fiemap || !check_fiemap(*fiemap, path)) {
+        return -1;
     }
-    int fs_fd = open_block_device_for_path(path);
-    if (fs_fd < 0) {
-        return;
+    auto block_device = block_device_for_path(path);
+    if (block_device.empty()) {
+        return -1;
     }
-    if (ioctl(fs_fd, BLKSECDISCARD, range) != 0) {
-        SLOGE("Unable to BLKSECDISCARD %s: %s", path.c_str(), strerror(errno));
-        close(fs_fd);
-        return;
+    AutoCloseFD fs_fd(block_device, O_RDWR | O_LARGEFILE);
+    if (!fs_fd) {
+        PLOG(ERROR) << "Failed to open device " << block_device;
+        return -1;
     }
-    close(fs_fd);
-    SLOGD("Discarded %s", path.c_str());
-}
-
-// Find a short range that completely covers the file.
-// If there isn't one, return -1, otherwise 0.
-static int file_device_range(const std::string &path, uint64_t range[2])
-{
-    int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
-    if (fd < 0) {
-        if (errno == ENOENT) {
-            SLOGD("Unable to open %s: %s", path.c_str(), strerror(errno));
-        } else {
-            SLOGE("Unable to open %s: %s", path.c_str(), strerror(errno));
+    for (uint32_t i = 0; i < fiemap->fm_mapped_extents; i++) {
+        uint64_t range[2];
+        range[0] = fiemap->fm_extents[i].fe_physical;
+        range[1] = fiemap->fm_extents[i].fe_length;
+        if (ioctl(fs_fd.get(), BLKSECDISCARD, range) == -1) {
+            PLOG(ERROR) << "Unable to BLKSECDISCARD " << path;
+            return -1;
         }
-        return -1;
     }
-    alignas(struct fiemap) char fiemap_buffer[offsetof(struct fiemap, fm_extents[1])];
-    memset(fiemap_buffer, 0, sizeof(fiemap_buffer));
-    struct fiemap *fiemap = (struct fiemap *)fiemap_buffer;
-    fiemap->fm_start = 0;
-    fiemap->fm_length = UINT64_MAX;
-    fiemap->fm_flags = 0;
-    fiemap->fm_extent_count = 1;
-    fiemap->fm_mapped_extents = 0;
-    if (ioctl(fd, FS_IOC_FIEMAP, fiemap) != 0) {
-        SLOGE("Unable to FIEMAP %s: %s", path.c_str(), strerror(errno));
-        close(fd);
-        return -1;
-    }
-    close(fd);
-    if (fiemap->fm_mapped_extents != 1) {
-        SLOGE("Expecting one extent, got %d in %s", fiemap->fm_mapped_extents, path.c_str());
-        return -1;
-    }
-    struct fiemap_extent *extent = &fiemap->fm_extents[0];
-    if (!(extent->fe_flags & FIEMAP_EXTENT_LAST)) {
-        SLOGE("First extent was not the last in %s", path.c_str());
-        return -1;
-    }
-    if (extent->fe_flags &
-            (FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_NOT_ALIGNED)) {
-        SLOGE("Extent has unexpected flags %ulx: %s", extent->fe_flags, path.c_str());
-        return -1;
-    }
-    if (extent->fe_length > MAX_WIPE_LENGTH) {
-        SLOGE("Extent too big, %llu bytes in %s", extent->fe_length, path.c_str());
-        return -1;
-    }
-    range[0] = extent->fe_physical;
-    range[1] = extent->fe_length;
     return 0;
 }
 
-// Given a file path, look for the corresponding
-// block device in /proc/mounts and open it.
-static int open_block_device_for_path(const std::string &path)
+// Read the file's FIEMAP
+std::unique_ptr<struct fiemap> path_fiemap(const std::string &path, uint32_t extent_count)
 {
-    std::string mountsfile("/proc/mounts");
-    std::string mounts;
-    if (read_file_as_string_atomically(mountsfile, mounts) < 0) {
-        return -1;
+    AutoCloseFD fd(path);
+    if (!fd) {
+        if (errno == ENOENT) {
+            PLOG(DEBUG) << "Unable to open " << path;
+        } else {
+            PLOG(ERROR) << "Unable to open " << path;
+        }
+        return nullptr;
     }
-    std::string block_device;
-    if (find_block_device_for_path(mounts, path, block_device) < 0) {
-        return -1;
+    auto fiemap = alloc_fiemap(extent_count);
+    if (ioctl(fd.get(), FS_IOC_FIEMAP, fiemap.get()) != 0) {
+        PLOG(ERROR) << "Unable to FIEMAP " << path;
+        return nullptr;
     }
-    SLOGD("For path %s block device is %s", path.c_str(), block_device.c_str());
-    int res = open(block_device.c_str(), O_RDWR | O_LARGEFILE | O_CLOEXEC);
-    if (res < 0) {
-        SLOGE("Failed to open device %s: %s", block_device.c_str(), strerror(errno));
-        return -1;
+    auto mapped = fiemap->fm_mapped_extents;
+    if (mapped < 1 || mapped > extent_count) {
+        LOG(ERROR) << "Extent count not in bounds 1 <= " << mapped << " <= " << extent_count
+            << " in " << path;
+        return nullptr;
     }
+    return fiemap;
+}
+
+// Ensure that the FIEMAP covers the file and is OK to discard
+bool check_fiemap(const struct fiemap &fiemap, const std::string &path) {
+    auto mapped = fiemap.fm_mapped_extents;
+    if (!(fiemap.fm_extents[mapped - 1].fe_flags & FIEMAP_EXTENT_LAST)) {
+        LOG(ERROR) << "Extent " << mapped -1 << " was not the last in " << path;
+        return false;
+    }
+    for (uint32_t i = 0; i < mapped; i++) {
+        auto flags = fiemap.fm_extents[i].fe_flags;
+        if (flags & (FIEMAP_EXTENT_UNKNOWN | FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_NOT_ALIGNED)) {
+            LOG(ERROR) << "Extent " << i << " has unexpected flags " << flags << ": " << path;
+            return false;
+        }
+    }
+    return true;
+}
+
+std::unique_ptr<struct fiemap> alloc_fiemap(uint32_t extent_count)
+{
+    size_t allocsize = offsetof(struct fiemap, fm_extents[extent_count]);
+    std::unique_ptr<struct fiemap> res(new (::operator new (allocsize)) struct fiemap);
+    memset(res.get(), 0, allocsize);
+    res->fm_start = 0;
+    res->fm_length = UINT64_MAX;
+    res->fm_flags = 0;
+    res->fm_extent_count = extent_count;
+    res->fm_mapped_extents = 0;
     return res;
 }
 
-// Read a file into a buffer in a single gulp, for atomicity.
-// Null-terminate the buffer.
-// Retry until the buffer is big enough.
-static int read_file_as_string_atomically(const std::string &path, std::string &contents)
+// Given a file path, look for the corresponding block device in /proc/mount
+std::string block_device_for_path(const std::string &path)
 {
-    ssize_t buffer_size = INIT_BUFFER_SIZE;
-    while (true) {
-        int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC);
-        if (fd < 0) {
-            SLOGE("Failed to open %s: %s", path.c_str(), strerror(errno));
-            return -1;
-        }
-        contents.resize(buffer_size);
-        ssize_t read_size = read(fd, &contents[0], buffer_size);
-        if (read_size < 0) {
-            SLOGE("Failed to read from %s: %s", path.c_str(), strerror(errno));
-            close(fd);
-            return -1;
-        }
-        close(fd);
-        if (read_size < buffer_size) {
-            contents.resize(read_size);
-            return 0;
-        }
-        SLOGD("%s too big for buffer of size %zu", path.c_str(), buffer_size);
-        buffer_size <<= 1;
+    std::unique_ptr<FILE, int(*)(FILE*)> mnts(setmntent("/proc/mounts", "re"), endmntent);
+    if (!mnts) {
+        PLOG(ERROR) << "Unable to open /proc/mounts";
+        return "";
     }
+    std::string result;
+    size_t best_length = 0;
+    struct mntent *mnt; // getmntent returns a thread local, so it's safe.
+    while ((mnt = getmntent(mnts.get())) != nullptr) {
+        auto l = strlen(mnt->mnt_dir);
+        if (l > best_length &&
+            path.size() > l &&
+            path[l] == '/' &&
+            path.compare(0, l, mnt->mnt_dir) == 0) {
+                result = mnt->mnt_fsname;
+                best_length = l;
+        }
+    }
+    if (result.empty()) {
+        LOG(ERROR) <<"Didn't find a mountpoint to match path " << path;
+        return "";
+    }
+    LOG(DEBUG) << "For path " << path << " block device is " << result;
+    return result;
 }
 
-// Search a string representing the contents of /proc/mounts
-// for the mount point of a particular file by prefix matching
-// and return the corresponding block device.
-static int find_block_device_for_path(
-    const std::string &mounts,
-    const std::string &path,
-    std::string &block_device)
-{
-    auto line_begin = mounts.begin();
-    size_t best_prefix = 0;
-    std::string::const_iterator line_end;
-    while (line_begin != mounts.end()) {
-        line_end = std::find(line_begin, mounts.end(), '\n');
-        if (line_end == mounts.end()) {
-            break;
-        }
-        auto device_end = std::find(line_begin, line_end, ' ');
-        if (device_end == line_end) {
-            break;
-        }
-        auto mountpoint_begin = device_end + 1;
-        auto mountpoint_end = std::find(mountpoint_begin, line_end, ' ');
-        if (mountpoint_end == line_end) {
-            break;
-        }
-        if (std::find(line_begin, mountpoint_end, '\\') != mountpoint_end) {
-            // We don't correctly handle escape sequences, and we don't expect
-            // to encounter any, so fail if we do.
-            break;
-        }
-        size_t mountpoint_len = mountpoint_end - mountpoint_begin;
-        if (mountpoint_len > best_prefix &&
-                mountpoint_len < path.length() &&
-                path[mountpoint_len] == '/' &&
-                std::equal(mountpoint_begin, mountpoint_end, path.begin())) {
-            block_device = std::string(line_begin, device_end);
-            best_prefix = mountpoint_len;
-        }
-        line_begin = line_end + 1;
-    }
-    // All of the "break"s above are fatal parse errors.
-    if (line_begin != mounts.end()) {
-        auto bad_line = std::string(line_begin, line_end);
-        SLOGE("Unable to parse line in %s: %s", path.c_str(), bad_line.c_str());
-        return -1;
-    }
-    if (best_prefix == 0) {
-        SLOGE("No prefix found for path: %s", path.c_str());
-        return -1;
-    }
-    return 0;
 }