Daniel Rosenberg | 65f99c9 | 2018-08-28 01:58:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "Checkpoint" |
| 18 | #include "Checkpoint.h" |
| 19 | |
| 20 | #include <android-base/file.h> |
| 21 | #include <android-base/parseint.h> |
| 22 | #include <cutils/android_reboot.h> |
| 23 | #include <fs_mgr.h> |
| 24 | #include <mntent.h> |
| 25 | #include <sys/mount.h> |
| 26 | |
| 27 | #include <list> |
| 28 | #include <string> |
| 29 | |
| 30 | namespace android { |
| 31 | namespace vold { |
| 32 | |
| 33 | static const std::string kMetadataCPFile = "/metadata/vold/checkpoint"; |
| 34 | |
| 35 | bool cp_startCheckpoint(int retry) { |
| 36 | if (retry < -1) return false; |
| 37 | std::string content = std::to_string(retry); |
| 38 | return android::base::WriteStringToFile(content, kMetadataCPFile); |
| 39 | } |
| 40 | |
| 41 | bool cp_commitChanges() { |
| 42 | struct fstab* fstab = fs_mgr_read_fstab_default(); |
| 43 | if (!fstab) return false; |
| 44 | |
| 45 | FILE* fp = setmntent("/proc/mounts", "r"); |
| 46 | mntent* mentry; |
| 47 | |
| 48 | if (fp == NULL) return false; |
| 49 | |
| 50 | while ((mentry = getmntent(fp)) != NULL) { |
| 51 | auto test = std::string(mentry->mnt_dir) + "/"; |
| 52 | for (int i = 0; i < fstab->num_entries; i++) { |
| 53 | if (!fs_mgr_is_checkpoint(&fstab->recs[i])) continue; |
| 54 | |
| 55 | if (!strcmp(fstab->recs[i].mount_point, mentry->mnt_dir) && |
| 56 | !strcmp(fstab->recs[i].fs_type, mentry->mnt_type)) { |
| 57 | if (!strcmp(fstab->recs[i].fs_type, "f2fs")) { |
| 58 | mount(mentry->mnt_fsname, mentry->mnt_dir, "none", |
| 59 | MS_REMOUNT | fstab->recs[i].flags, "checkpoint=enable"); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | endmntent(fp); |
| 65 | |
| 66 | fs_mgr_free_fstab(fstab); |
| 67 | return android::base::RemoveFileIfExists(kMetadataCPFile); |
| 68 | } |
| 69 | |
| 70 | void cp_abortChanges() { |
| 71 | android_reboot(ANDROID_RB_RESTART2, 0, nullptr); |
| 72 | } |
| 73 | |
| 74 | bool cp_needRollback(const std::string& id) { |
| 75 | std::string content; |
| 76 | bool ret; |
| 77 | |
| 78 | ret = android::base::ReadFileToString(kMetadataCPFile, &content); |
| 79 | if (ret) return content == "0"; |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | bool cp_needsCheckpoint(void) { |
| 84 | bool ret; |
| 85 | std::string content; |
| 86 | |
| 87 | ret = android::base::ReadFileToString(kMetadataCPFile, &content); |
| 88 | if (ret) return content != "0"; |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | bool cp_prepareDriveForCheckpoint(const std::string& mountPoint) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | bool cp_markBootAttempt() { |
| 97 | std::string oldContent, newContent; |
| 98 | int retry = 0; |
| 99 | if (!android::base::ReadFileToString(kMetadataCPFile, &oldContent)) return false; |
| 100 | |
| 101 | if (!android::base::ParseInt(oldContent, &retry)) return false; |
| 102 | if (retry > 0) retry--; |
| 103 | |
| 104 | newContent = std::to_string(retry); |
| 105 | return android::base::WriteStringToFile(newContent, kMetadataCPFile); |
| 106 | } |
| 107 | |
| 108 | } // namespace vold |
| 109 | } // namespace android |