Merge RP1A.200106.001

Change-Id: I252ed1d3edd08c92d4791f75235d5896a5466cb2
diff --git a/EncryptInplace.cpp b/EncryptInplace.cpp
index 20d306c..14277a4 100644
--- a/EncryptInplace.cpp
+++ b/EncryptInplace.cpp
@@ -398,6 +398,8 @@
     struct encryptGroupsData data;
     struct f2fs_info* f2fs_info = NULL;
     int rc = ENABLE_INPLACE_ERR_OTHER;
+    struct timespec time_started = {0};
+
     if (previously_encrypted_upto > *size_already_done) {
         LOG(DEBUG) << "Not fast encrypting since resuming part way through";
         return ENABLE_INPLACE_ERR_OTHER;
@@ -435,9 +437,14 @@
 
     data.one_pct = data.tot_used_blocks / 100;
     data.cur_pct = 0;
-    data.time_started = time(NULL);
+    if (clock_gettime(CLOCK_MONOTONIC, &time_started)) {
+        LOG(WARNING) << "Error getting time at start";
+        // Note - continue anyway - we'll run with 0
+    }
+    data.time_started = time_started.tv_sec;
     data.remaining_time = -1;
 
+
     data.buffer = (char*)malloc(f2fs_info->block_size);
     if (!data.buffer) {
         LOG(ERROR) << "Failed to allocate crypto buffer";
diff --git a/KeyUtil.cpp b/KeyUtil.cpp
index 3058d72..09d6ea3 100644
--- a/KeyUtil.cpp
+++ b/KeyUtil.cpp
@@ -21,7 +21,7 @@
 #include <string>
 
 #include <fcntl.h>
-#include <linux/fs.h>
+#include <linux/fscrypt.h>
 #include <openssl/sha.h>
 #include <sys/ioctl.h>
 
@@ -32,7 +32,6 @@
 #include "FsCrypt.h"
 #include "KeyStorage.h"
 #include "Utils.h"
-#include "fscrypt_uapi.h"
 
 #define MAX_USER_ID 0xFFFFFFFF
 
@@ -41,10 +40,8 @@
 namespace android {
 namespace vold {
 
-constexpr int FS_AES_256_XTS_KEY_SIZE = 64;
-
 bool randomKey(KeyBuffer* key) {
-    *key = KeyBuffer(FS_AES_256_XTS_KEY_SIZE);
+    *key = KeyBuffer(FSCRYPT_MAX_KEY_SIZE);
     if (ReadRandomBytes(key->size(), key->data()) != 0) {
         // TODO status_t plays badly with PLOG, fix it.
         LOG(ERROR) << "Random read failed";
@@ -103,20 +100,20 @@
     unsigned char key_ref2[SHA512_DIGEST_LENGTH];
     SHA512_Final(key_ref2, &c);
 
-    static_assert(FS_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH, "Hash too short for descriptor");
-    return std::string((char*)key_ref2, FS_KEY_DESCRIPTOR_SIZE);
+    static_assert(FSCRYPT_KEY_DESCRIPTOR_SIZE <= SHA512_DIGEST_LENGTH,
+                  "Hash too short for descriptor");
+    return std::string((char*)key_ref2, FSCRYPT_KEY_DESCRIPTOR_SIZE);
 }
 
 static bool fillKey(const KeyBuffer& key, fscrypt_key* fs_key) {
-    if (key.size() != FS_AES_256_XTS_KEY_SIZE) {
+    if (key.size() != FSCRYPT_MAX_KEY_SIZE) {
         LOG(ERROR) << "Wrong size key " << key.size();
         return false;
     }
-    static_assert(FS_AES_256_XTS_KEY_SIZE <= sizeof(fs_key->raw), "Key too long!");
-    fs_key->mode = FS_ENCRYPTION_MODE_AES_256_XTS;
-    fs_key->size = key.size();
-    memset(fs_key->raw, 0, sizeof(fs_key->raw));
+    static_assert(FSCRYPT_MAX_KEY_SIZE == sizeof(fs_key->raw), "Mismatch of max key sizes");
+    fs_key->mode = 0;  // unused by kernel
     memcpy(fs_key->raw, key.data(), key.size());
+    fs_key->size = key.size();
     return true;
 }
 
diff --git a/Utils.h b/Utils.h
index 5bb2855..4c0114a 100644
--- a/Utils.h
+++ b/Utils.h
@@ -34,7 +34,7 @@
 namespace android {
 namespace vold {
 
-static const char* kPropFuseSnapshot = "sys.fuse_snapshot";
+static const char* kPropFuse = "persist.sys.fuse";
 
 /* SELinux contexts used depending on the block device type */
 extern security_context_t sBlkidContext;
diff --git a/VolumeManager.cpp b/VolumeManager.cpp
index caf311d..adf2d84 100644
--- a/VolumeManager.cpp
+++ b/VolumeManager.cpp
@@ -377,7 +377,7 @@
 }
 
 int VolumeManager::linkPrimary(userid_t userId) {
-    if (!GetBoolProperty(android::vold::kPropFuseSnapshot, false)) {
+    if (!GetBoolProperty(android::vold::kPropFuse, false)) {
         std::string source(mPrimary->getPath());
         if (mPrimary->isEmulated()) {
             source = StringPrintf("%s/%d", source.c_str(), userId);
@@ -471,7 +471,7 @@
         createEmulatedVolumesForUser(userId);
     }
 
-    if (!GetBoolProperty(android::vold::kPropFuseSnapshot, false)) {
+    if (!GetBoolProperty(android::vold::kPropFuse, false)) {
         // Note that sometimes the system will spin up processes from Zygote
         // before actually starting the user, so we're okay if Zygote
         // already created this directory.
@@ -566,7 +566,7 @@
 }
 
 int VolumeManager::remountUid(uid_t uid, int32_t mountMode) {
-    if (GetBoolProperty(android::vold::kPropFuseSnapshot, false)) {
+    if (GetBoolProperty(android::vold::kPropFuse, false)) {
         // TODO(135341433): Implement fuse specific logic.
         return 0;
     }
@@ -812,8 +812,9 @@
 int VolumeManager::mkdirs(const std::string& path) {
     // Only offer to create directories for paths managed by vold
     if (StartsWith(path, "/storage/")) {
+        std::string lower_path = "/mnt/runtime/default/" + path.substr(9);
         // fs_mkdirs() does symlink checking and relative path enforcement
-        return fs_mkdirs(path.c_str(), 0700);
+        return fs_mkdirs(lower_path.c_str(), 0700);
     } else {
         LOG(ERROR) << "Failed to find mounted volume for " << path;
         return -EINVAL;
diff --git a/fscrypt_uapi.h b/fscrypt_uapi.h
deleted file mode 100644
index 3999036..0000000
--- a/fscrypt_uapi.h
+++ /dev/null
@@ -1,48 +0,0 @@
-#ifndef _UAPI_LINUX_FSCRYPT_H
-#define _UAPI_LINUX_FSCRYPT_H
-
-// Definitions for FS_IOC_ADD_ENCRYPTION_KEY and FS_IOC_REMOVE_ENCRYPTION_KEY
-
-// TODO: switch to <linux/fscrypt.h> once it's in Bionic
-
-#ifndef FS_IOC_ADD_ENCRYPTION_KEY
-
-#include <linux/types.h>
-
-#define FSCRYPT_KEY_DESCRIPTOR_SIZE 8
-#define FSCRYPT_KEY_IDENTIFIER_SIZE 16
-
-#define FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR 1
-#define FSCRYPT_KEY_SPEC_TYPE_IDENTIFIER 2
-
-struct fscrypt_key_specifier {
-    __u32 type;
-    __u32 __reserved;
-    union {
-        __u8 __reserved[32];
-        __u8 descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
-        __u8 identifier[FSCRYPT_KEY_IDENTIFIER_SIZE];
-    } u;
-};
-
-struct fscrypt_add_key_arg {
-    struct fscrypt_key_specifier key_spec;
-    __u32 raw_size;
-    __u32 __reserved[9];
-    __u8 raw[];
-};
-
-struct fscrypt_remove_key_arg {
-    struct fscrypt_key_specifier key_spec;
-#define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_FILES_BUSY 0x00000001
-#define FSCRYPT_KEY_REMOVAL_STATUS_FLAG_OTHER_USERS 0x00000002
-    __u32 removal_status_flags;
-    __u32 __reserved[5];
-};
-
-#define FS_IOC_ADD_ENCRYPTION_KEY _IOWR('f', 23, struct fscrypt_add_key_arg)
-#define FS_IOC_REMOVE_ENCRYPTION_KEY _IOWR('f', 24, struct fscrypt_remove_key_arg)
-
-#endif /* FS_IOC_ADD_ENCRYPTION_KEY */
-
-#endif /* _UAPI_LINUX_FSCRYPT_H */
diff --git a/model/EmulatedVolume.cpp b/model/EmulatedVolume.cpp
index b42bd49..aef7b77 100644
--- a/model/EmulatedVolume.cpp
+++ b/model/EmulatedVolume.cpp
@@ -56,6 +56,7 @@
     setId(StringPrintf("emulated:%u,%u;%u", major(device), minor(device), userId));
     mRawPath = rawPath;
     mLabel = fsUuid;
+    mFuseMounted = false;
 }
 
 EmulatedVolume::~EmulatedVolume() {}
@@ -74,7 +75,7 @@
     // TODO(b/134706060) we don't actually want to mount the "write" view by
     // default, since it gives write access to all OBB dirs.
     std::string androidSource(
-            StringPrintf("/mnt/runtime/write/%s/%d/Android", label.c_str(), userId));
+            StringPrintf("/mnt/runtime/default/%s/%d/Android", label.c_str(), userId));
     std::string androidTarget(
             StringPrintf("/mnt/user/%d/%s/%d/Android", userId, label.c_str(), userId));
 
@@ -132,7 +133,7 @@
 
     dev_t before = GetDevice(mSdcardFsFull);
 
-    bool isFuse = base::GetBoolProperty(kPropFuseSnapshot, false);
+    bool isFuse = base::GetBoolProperty(kPropFuse, false);
 
     // Mount sdcardfs regardless of FUSE, since we need it to bind-mount on top of the
     // FUSE volume for various reasons.
diff --git a/model/PublicVolume.cpp b/model/PublicVolume.cpp
index b9164f5..78f150d 100644
--- a/model/PublicVolume.cpp
+++ b/model/PublicVolume.cpp
@@ -223,7 +223,7 @@
     /* sdcardfs will have exited already. The filesystem will still be running */
     TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0));
 
-    bool isFuse = base::GetBoolProperty(kPropFuseSnapshot, false);
+    bool isFuse = base::GetBoolProperty(kPropFuse, false);
     if (isFuse) {
         // We need to mount FUSE *after* sdcardfs, since the FUSE daemon may depend
         // on sdcardfs being up.