Add support for checkpointing

Checkpointing uses a combination of files on the meta partition
and the checkpoint= fs_mgr flag. Checkpointed partitions will
revert to their starting state on reboot unless checkpoint commit
is called.

Test: Run vdc commands, check file on metadata
Merged-In: Icba16578608a6cbf922472e9d4ae5b8cf5f016c6
Change-Id: Icba16578608a6cbf922472e9d4ae5b8cf5f016c6
diff --git a/Android.bp b/Android.bp
index 8fd29f0..1e45727 100644
--- a/Android.bp
+++ b/Android.bp
@@ -95,6 +95,7 @@
     srcs: [
         "Benchmark.cpp",
         "CheckEncryption.cpp",
+        "Checkpoint.cpp",
         "Devmapper.cpp",
         "EncryptInplace.cpp",
         "Ext4Crypt.cpp",
diff --git a/Checkpoint.cpp b/Checkpoint.cpp
new file mode 100644
index 0000000..476b0ae
--- /dev/null
+++ b/Checkpoint.cpp
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#define LOG_TAG "Checkpoint"
+#include "Checkpoint.h"
+
+#include <android-base/file.h>
+#include <android-base/parseint.h>
+#include <cutils/android_reboot.h>
+#include <fs_mgr.h>
+#include <mntent.h>
+#include <sys/mount.h>
+
+#include <list>
+#include <string>
+
+namespace android {
+namespace vold {
+
+static const std::string kMetadataCPFile = "/metadata/vold/checkpoint";
+
+bool cp_startCheckpoint(int retry) {
+    if (retry < -1) return false;
+    std::string content = std::to_string(retry);
+    return android::base::WriteStringToFile(content, kMetadataCPFile);
+}
+
+bool cp_commitChanges() {
+    struct fstab* fstab = fs_mgr_read_fstab_default();
+    if (!fstab) return false;
+
+    FILE* fp = setmntent("/proc/mounts", "r");
+    mntent* mentry;
+
+    if (fp == NULL) return false;
+
+    while ((mentry = getmntent(fp)) != NULL) {
+        auto test = std::string(mentry->mnt_dir) + "/";
+        for (int i = 0; i < fstab->num_entries; i++) {
+            if (!fs_mgr_is_checkpoint(&fstab->recs[i])) continue;
+
+            if (!strcmp(fstab->recs[i].mount_point, mentry->mnt_dir) &&
+                !strcmp(fstab->recs[i].fs_type, mentry->mnt_type)) {
+                if (!strcmp(fstab->recs[i].fs_type, "f2fs")) {
+                    mount(mentry->mnt_fsname, mentry->mnt_dir, "none",
+                          MS_REMOUNT | fstab->recs[i].flags, "checkpoint=enable");
+                }
+            }
+        }
+    }
+    endmntent(fp);
+
+    fs_mgr_free_fstab(fstab);
+    return android::base::RemoveFileIfExists(kMetadataCPFile);
+}
+
+void cp_abortChanges() {
+    android_reboot(ANDROID_RB_RESTART2, 0, nullptr);
+}
+
+bool cp_needRollback(const std::string& id) {
+    std::string content;
+    bool ret;
+
+    ret = android::base::ReadFileToString(kMetadataCPFile, &content);
+    if (ret) return content == "0";
+    return false;
+}
+
+bool cp_needsCheckpoint(void) {
+    bool ret;
+    std::string content;
+
+    ret = android::base::ReadFileToString(kMetadataCPFile, &content);
+    if (ret) return content != "0";
+    return false;
+}
+
+bool cp_prepareDriveForCheckpoint(const std::string& mountPoint) {
+    return false;
+}
+
+bool cp_markBootAttempt() {
+    std::string oldContent, newContent;
+    int retry = 0;
+    if (!android::base::ReadFileToString(kMetadataCPFile, &oldContent)) return false;
+
+    if (!android::base::ParseInt(oldContent, &retry)) return false;
+    if (retry > 0) retry--;
+
+    newContent = std::to_string(retry);
+    return android::base::WriteStringToFile(newContent, kMetadataCPFile);
+}
+
+}  // namespace vold
+}  // namespace android
diff --git a/Checkpoint.h b/Checkpoint.h
new file mode 100644
index 0000000..8449956
--- /dev/null
+++ b/Checkpoint.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2018 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 _CHECKPOINT_H
+#define _CHECKPOINT_H
+
+#include <string>
+
+namespace android {
+namespace vold {
+
+bool cp_startCheckpoint(int retry);
+
+bool cp_commitChanges();
+
+void cp_abortChanges();
+
+bool cp_needRollback(const std::string& id);
+
+bool cp_needsCheckpoint();
+
+bool cp_prepareDriveForCheckpoint(const std::string& mountPoint);
+
+bool cp_markBootAttempt();
+
+}  // namespace vold
+}  // namespace android
+
+#endif
diff --git a/MetadataCrypt.cpp b/MetadataCrypt.cpp
index 842b19b..6edc0a4 100644
--- a/MetadataCrypt.cpp
+++ b/MetadataCrypt.cpp
@@ -36,6 +36,7 @@
 #include <cutils/fs.h>
 #include <fs_mgr.h>
 
+#include "Checkpoint.h"
 #include "EncryptInplace.h"
 #include "KeyStorage.h"
 #include "KeyUtil.h"
@@ -59,7 +60,8 @@
         return false;
     }
     auto mount_rc = fs_mgr_do_mount(fstab_default, const_cast<char*>(mount_point),
-                                    const_cast<char*>(blk_device), nullptr);
+                                    const_cast<char*>(blk_device), nullptr,
+                                    android::vold::cp_needsCheckpoint());
     if (setexeccon(nullptr)) {
         PLOG(ERROR) << "Failed to clear setexeccon";
         return false;
diff --git a/VoldNativeService.cpp b/VoldNativeService.cpp
index 1a15304..582f84e 100644
--- a/VoldNativeService.cpp
+++ b/VoldNativeService.cpp
@@ -24,6 +24,7 @@
 #include "Process.h"
 #include "VolumeManager.h"
 
+#include "Checkpoint.h"
 #include "Ext4Crypt.h"
 #include "MetadataCrypt.h"
 #include "cryptfs.h"
@@ -764,5 +765,55 @@
     return translateBool(e4crypt_destroy_user_storage(uuid_, userId, flags));
 }
 
+binder::Status VoldNativeService::startCheckpoint(int32_t retry, bool* _aidl_return) {
+    ENFORCE_UID(AID_SYSTEM);
+    ACQUIRE_LOCK;
+
+    *_aidl_return = cp_startCheckpoint(retry);
+    return ok();
+}
+
+binder::Status VoldNativeService::needsCheckpoint(bool* _aidl_return) {
+    ENFORCE_UID(AID_SYSTEM);
+    ACQUIRE_LOCK;
+
+    *_aidl_return = cp_needsCheckpoint();
+    return ok();
+}
+
+binder::Status VoldNativeService::commitChanges(bool* _aidl_return) {
+    ENFORCE_UID(AID_SYSTEM);
+    ACQUIRE_LOCK;
+
+    *_aidl_return = cp_commitChanges();
+    return ok();
+}
+
+binder::Status VoldNativeService::prepareDriveForCheckpoint(const std::string& mountPoint,
+                                                            bool* _aidl_return) {
+    ENFORCE_UID(AID_SYSTEM);
+    CHECK_ARGUMENT_PATH(mountPoint);
+    ACQUIRE_LOCK;
+
+    *_aidl_return = cp_prepareDriveForCheckpoint(mountPoint);
+    return ok();
+}
+
+binder::Status VoldNativeService::markBootAttempt(bool* _aidl_return) {
+    ENFORCE_UID(AID_SYSTEM);
+    ACQUIRE_LOCK;
+
+    *_aidl_return = cp_markBootAttempt();
+    return ok();
+}
+
+binder::Status VoldNativeService::abortChanges() {
+    ENFORCE_UID(AID_SYSTEM);
+    ACQUIRE_LOCK;
+
+    cp_abortChanges();
+    return ok();
+}
+
 }  // namespace vold
 }  // namespace android
diff --git a/VoldNativeService.h b/VoldNativeService.h
index 0a080e1..2282141 100644
--- a/VoldNativeService.h
+++ b/VoldNativeService.h
@@ -110,6 +110,16 @@
                                       int32_t userSerial, int32_t flags);
     binder::Status destroyUserStorage(const std::unique_ptr<std::string>& uuid, int32_t userId,
                                       int32_t flags);
+
+    binder::Status mountExternalStorageForApp(const std::string& packageName, int32_t appId,
+                                              const std::string& sandboxId, int32_t userId);
+
+    binder::Status startCheckpoint(int32_t retry, bool* _aidl_return);
+    binder::Status needsCheckpoint(bool* _aidl_return);
+    binder::Status commitChanges(bool* _aidl_return);
+    binder::Status prepareDriveForCheckpoint(const std::string& mountPoint, bool* _aidl_return);
+    binder::Status markBootAttempt(bool* __aidl_return);
+    binder::Status abortChanges();
 };
 
 }  // namespace vold
diff --git a/binder/android/os/IVold.aidl b/binder/android/os/IVold.aidl
index 567385c..fd97077 100644
--- a/binder/android/os/IVold.aidl
+++ b/binder/android/os/IVold.aidl
@@ -96,6 +96,13 @@
                             int storageFlags);
     void destroyUserStorage(@nullable @utf8InCpp String uuid, int userId, int storageFlags);
 
+    boolean startCheckpoint(int retry);
+    boolean needsCheckpoint();
+    void abortChanges();
+    boolean commitChanges();
+    boolean prepareDriveForCheckpoint(@utf8InCpp String mountPoint);
+    boolean markBootAttempt();
+
     const int ENCRYPTION_FLAG_NO_UI = 4;
 
     const int ENCRYPTION_STATE_NONE = 1;
diff --git a/cryptfs.cpp b/cryptfs.cpp
index 00e5519..383bbe8 100644
--- a/cryptfs.cpp
+++ b/cryptfs.cpp
@@ -24,6 +24,7 @@
 
 #include "cryptfs.h"
 
+#include "Checkpoint.h"
 #include "EncryptInplace.h"
 #include "Ext4Crypt.h"
 #include "Keymaster.h"
@@ -1562,7 +1563,9 @@
             SLOGE("Failed to setexeccon");
             return -1;
         }
-        while ((mount_rc = fs_mgr_do_mount(fstab_default, DATA_MNT_POINT, crypto_blkdev, 0)) != 0) {
+        bool needs_cp = android::vold::cp_needsCheckpoint();
+        while ((mount_rc = fs_mgr_do_mount(fstab_default, DATA_MNT_POINT, crypto_blkdev, 0,
+                                           needs_cp)) != 0) {
             if (mount_rc == FS_MGR_DOMNT_BUSY) {
                 /* TODO: invoke something similar to
                    Process::killProcessWithOpenFiles(DATA_MNT_POINT,
diff --git a/vdc.cpp b/vdc.cpp
index f49d6b8..ff5073b 100644
--- a/vdc.cpp
+++ b/vdc.cpp
@@ -31,6 +31,7 @@
 #include "android/os/IVold.h"
 
 #include <android-base/logging.h>
+#include <android-base/parseint.h>
 #include <android-base/stringprintf.h>
 #include <binder/IServiceManager.h>
 #include <binder/Status.h>
@@ -104,6 +105,31 @@
         checkStatus(vold->mountFstab(args[2]));
     } else if (args[0] == "cryptfs" && args[1] == "encryptFstab" && args.size() == 3) {
         checkStatus(vold->encryptFstab(args[2]));
+    } else if (args[0] == "checkpoint" && args[1] == "startCheckpoint" && args.size() == 3) {
+        int retry;
+        bool success = false;
+        if (!android::base::ParseInt(args[2], &retry)) exit(EINVAL);
+        checkStatus(vold->startCheckpoint(retry, &success));
+        return success ? 1 : 0;
+    } else if (args[0] == "checkpoint" && args[1] == "needsCheckpoint" && args.size() == 2) {
+        bool enabled = false;
+        checkStatus(vold->needsCheckpoint(&enabled));
+        return enabled ? 1 : 0;
+    } else if (args[0] == "checkpoint" && args[1] == "commitChanges" && args.size() == 2) {
+        bool success = false;
+        checkStatus(vold->commitChanges(&success));
+        return success ? 1 : 0;
+    } else if (args[0] == "checkpoint" && args[1] == "prepareDriveForCheckpoint" &&
+               args.size() == 3) {
+        bool success = false;
+        checkStatus(vold->prepareDriveForCheckpoint(args[2], &success));
+        return success ? 1 : 0;
+    } else if (args[0] == "checkpoint" && args[1] == "markBootAttempt" && args.size() == 2) {
+        bool success = false;
+        checkStatus(vold->markBootAttempt(&success));
+        return success ? 1 : 0;
+    } else if (args[0] == "checkpoint" && args[1] == "abortChanges" && args.size() == 2) {
+        checkStatus(vold->abortChanges());
     } else {
         LOG(ERROR) << "Raw commands are no longer supported";
         exit(EINVAL);