Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 17 | #include "MoveStorage.h" |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 18 | #include "Utils.h" |
| 19 | #include "VolumeManager.h" |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 20 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Jeff Sharkey | 8c24ae7 | 2018-01-04 16:46:34 -0700 | [diff] [blame] | 22 | #include <android-base/properties.h> |
| 23 | #include <android-base/stringprintf.h> |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 24 | #include <hardware_legacy/power.h> |
Jeff Sharkey | 8c24ae7 | 2018-01-04 16:46:34 -0700 | [diff] [blame] | 25 | #include <private/android_filesystem_config.h> |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 26 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 27 | #include <thread> |
| 28 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 29 | #include <dirent.h> |
| 30 | #include <sys/wait.h> |
| 31 | |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 32 | #define CONSTRAIN(amount, low, high) \ |
| 33 | ((amount) < (low) ? (low) : ((amount) > (high) ? (high) : (amount))) |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 34 | |
Jeff Sharkey | 8c24ae7 | 2018-01-04 16:46:34 -0700 | [diff] [blame] | 35 | static const char* kPropBlockingExec = "persist.sys.blocking_exec"; |
Jeff Sharkey | fce701b | 2016-07-29 15:52:41 -0600 | [diff] [blame] | 36 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 37 | using android::base::StringPrintf; |
| 38 | |
| 39 | namespace android { |
| 40 | namespace vold { |
| 41 | |
| 42 | // TODO: keep in sync with PackageManager |
| 43 | static const int kMoveSucceeded = -100; |
| 44 | static const int kMoveFailedInternalError = -6; |
| 45 | |
| 46 | static const char* kCpPath = "/system/bin/cp"; |
| 47 | static const char* kRmPath = "/system/bin/rm"; |
| 48 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 49 | static const char* kWakeLock = "MoveTask"; |
| 50 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 51 | static void notifyProgress(int progress, |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 52 | const android::sp<android::os::IVoldTaskListener>& listener) { |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 53 | if (listener) { |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 54 | android::os::PersistableBundle extras; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 55 | listener->onStatus(progress, extras); |
Jeff Sharkey | 52f7a91 | 2017-09-15 12:57:44 -0600 | [diff] [blame] | 56 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Paul Crowley | 51209e9 | 2019-03-18 10:26:47 -0700 | [diff] [blame] | 59 | static bool pushBackContents(const std::string& path, std::vector<std::string>& cmd, |
| 60 | int searchLevels) { |
| 61 | if (searchLevels == 0) { |
| 62 | cmd.emplace_back(path); |
| 63 | return true; |
| 64 | } |
| 65 | auto dirp = std::unique_ptr<DIR, int (*)(DIR*)>(opendir(path.c_str()), closedir); |
| 66 | if (!dirp) { |
| 67 | PLOG(ERROR) << "Unable to open directory: " << path; |
| 68 | return false; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 69 | } |
| 70 | bool found = false; |
| 71 | struct dirent* ent; |
Paul Crowley | 51209e9 | 2019-03-18 10:26:47 -0700 | [diff] [blame] | 72 | while ((ent = readdir(dirp.get())) != NULL) { |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 73 | if ((!strcmp(ent->d_name, ".")) || (!strcmp(ent->d_name, ".."))) { |
| 74 | continue; |
| 75 | } |
Paul Crowley | 51209e9 | 2019-03-18 10:26:47 -0700 | [diff] [blame] | 76 | auto subdir = path + "/" + ent->d_name; |
| 77 | found |= pushBackContents(subdir, cmd, searchLevels - 1); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 78 | } |
Paul Crowley | 51209e9 | 2019-03-18 10:26:47 -0700 | [diff] [blame] | 79 | return found; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 80 | } |
| 81 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 82 | static status_t execRm(const std::string& path, int startProgress, int stepProgress, |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 83 | const android::sp<android::os::IVoldTaskListener>& listener) { |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 84 | notifyProgress(startProgress, listener); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 85 | |
| 86 | uint64_t expectedBytes = GetTreeBytes(path); |
| 87 | uint64_t startFreeBytes = GetFreeBytes(path); |
| 88 | |
| 89 | std::vector<std::string> cmd; |
| 90 | cmd.push_back(kRmPath); |
| 91 | cmd.push_back("-f"); /* force: remove without confirmation, no error if it doesn't exist */ |
| 92 | cmd.push_back("-R"); /* recursive: remove directory contents */ |
Paul Crowley | 51209e9 | 2019-03-18 10:26:47 -0700 | [diff] [blame] | 93 | if (!pushBackContents(path, cmd, 2)) { |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 94 | LOG(WARNING) << "No contents in " << path; |
| 95 | return OK; |
| 96 | } |
| 97 | |
Jeff Sharkey | 8c24ae7 | 2018-01-04 16:46:34 -0700 | [diff] [blame] | 98 | if (android::base::GetBoolProperty(kPropBlockingExec, false)) { |
| 99 | return ForkExecvp(cmd); |
| 100 | } |
| 101 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 102 | pid_t pid = ForkExecvpAsync(cmd); |
| 103 | if (pid == -1) return -1; |
| 104 | |
| 105 | int status; |
| 106 | while (true) { |
| 107 | if (waitpid(pid, &status, WNOHANG) == pid) { |
| 108 | if (WIFEXITED(status)) { |
| 109 | LOG(DEBUG) << "Finished rm with status " << WEXITSTATUS(status); |
| 110 | return (WEXITSTATUS(status) == 0) ? OK : -1; |
| 111 | } else { |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | sleep(1); |
| 117 | uint64_t deltaFreeBytes = GetFreeBytes(path) - startFreeBytes; |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 118 | notifyProgress( |
| 119 | startProgress + |
| 120 | CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress), |
| 121 | listener); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 122 | } |
| 123 | return -1; |
| 124 | } |
| 125 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 126 | static status_t execCp(const std::string& fromPath, const std::string& toPath, int startProgress, |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 127 | int stepProgress, |
| 128 | const android::sp<android::os::IVoldTaskListener>& listener) { |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 129 | notifyProgress(startProgress, listener); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 130 | |
| 131 | uint64_t expectedBytes = GetTreeBytes(fromPath); |
| 132 | uint64_t startFreeBytes = GetFreeBytes(toPath); |
| 133 | |
Jeff Sharkey | a0220a5 | 2017-04-03 17:11:45 -0600 | [diff] [blame] | 134 | if (expectedBytes > startFreeBytes) { |
| 135 | LOG(ERROR) << "Data size " << expectedBytes << " is too large to fit in free space " |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 136 | << startFreeBytes; |
Jeff Sharkey | a0220a5 | 2017-04-03 17:11:45 -0600 | [diff] [blame] | 137 | return -1; |
| 138 | } |
| 139 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 140 | std::vector<std::string> cmd; |
| 141 | cmd.push_back(kCpPath); |
| 142 | cmd.push_back("-p"); /* preserve timestamps, ownership, and permissions */ |
| 143 | cmd.push_back("-R"); /* recurse into subdirectories (DEST must be a directory) */ |
| 144 | cmd.push_back("-P"); /* Do not follow symlinks [default] */ |
| 145 | cmd.push_back("-d"); /* don't dereference symlinks */ |
Paul Crowley | 51209e9 | 2019-03-18 10:26:47 -0700 | [diff] [blame] | 146 | if (!pushBackContents(fromPath, cmd, 1)) { |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 147 | LOG(WARNING) << "No contents in " << fromPath; |
| 148 | return OK; |
| 149 | } |
| 150 | cmd.push_back(toPath.c_str()); |
| 151 | |
Jeff Sharkey | 8c24ae7 | 2018-01-04 16:46:34 -0700 | [diff] [blame] | 152 | if (android::base::GetBoolProperty(kPropBlockingExec, false)) { |
| 153 | return ForkExecvp(cmd); |
| 154 | } |
| 155 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 156 | pid_t pid = ForkExecvpAsync(cmd); |
| 157 | if (pid == -1) return -1; |
| 158 | |
| 159 | int status; |
| 160 | while (true) { |
| 161 | if (waitpid(pid, &status, WNOHANG) == pid) { |
| 162 | if (WIFEXITED(status)) { |
| 163 | LOG(DEBUG) << "Finished cp with status " << WEXITSTATUS(status); |
| 164 | return (WEXITSTATUS(status) == 0) ? OK : -1; |
| 165 | } else { |
| 166 | break; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | sleep(1); |
| 171 | uint64_t deltaFreeBytes = startFreeBytes - GetFreeBytes(toPath); |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 172 | notifyProgress( |
| 173 | startProgress + |
| 174 | CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress), |
| 175 | listener); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 176 | } |
| 177 | return -1; |
| 178 | } |
| 179 | |
| 180 | static void bringOffline(const std::shared_ptr<VolumeBase>& vol) { |
| 181 | vol->destroy(); |
| 182 | vol->setSilent(true); |
| 183 | vol->create(); |
| 184 | vol->setMountFlags(0); |
| 185 | vol->mount(); |
| 186 | } |
| 187 | |
| 188 | static void bringOnline(const std::shared_ptr<VolumeBase>& vol) { |
| 189 | vol->destroy(); |
| 190 | vol->setSilent(false); |
| 191 | vol->create(); |
| 192 | } |
| 193 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 194 | static status_t moveStorageInternal(const std::shared_ptr<VolumeBase>& from, |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 195 | const std::shared_ptr<VolumeBase>& to, |
| 196 | const android::sp<android::os::IVoldTaskListener>& listener) { |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 197 | std::string fromPath; |
| 198 | std::string toPath; |
| 199 | |
| 200 | // TODO: add support for public volumes |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 201 | if (from->getType() != VolumeBase::Type::kEmulated) goto fail; |
| 202 | if (to->getType() != VolumeBase::Type::kEmulated) goto fail; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 203 | |
| 204 | // Step 1: tear down volumes and mount silently without making |
| 205 | // visible to userspace apps |
Henrik Baard | 7f52bca | 2015-11-26 12:05:13 +0100 | [diff] [blame] | 206 | { |
| 207 | std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 208 | bringOffline(from); |
| 209 | bringOffline(to); |
Henrik Baard | 7f52bca | 2015-11-26 12:05:13 +0100 | [diff] [blame] | 210 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 211 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 212 | fromPath = from->getInternalPath(); |
| 213 | toPath = to->getInternalPath(); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 214 | |
| 215 | // Step 2: clean up any stale data |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 216 | if (execRm(toPath, 10, 10, listener) != OK) { |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 217 | goto fail; |
| 218 | } |
| 219 | |
| 220 | // Step 3: perform actual copy |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 221 | if (execCp(fromPath, toPath, 20, 60, listener) != OK) { |
Henrik Baard | 77f156d | 2015-12-17 13:58:42 +0100 | [diff] [blame] | 222 | goto copy_fail; |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // NOTE: MountService watches for this magic value to know |
| 226 | // that move was successful |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 227 | notifyProgress(82, listener); |
Henrik Baard | 7f52bca | 2015-11-26 12:05:13 +0100 | [diff] [blame] | 228 | { |
| 229 | std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 230 | bringOnline(from); |
| 231 | bringOnline(to); |
Henrik Baard | 7f52bca | 2015-11-26 12:05:13 +0100 | [diff] [blame] | 232 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 233 | |
| 234 | // Step 4: clean up old data |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 235 | if (execRm(fromPath, 85, 15, listener) != OK) { |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 236 | goto fail; |
| 237 | } |
| 238 | |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 239 | notifyProgress(kMoveSucceeded, listener); |
| 240 | return OK; |
Henrik Baard | 77f156d | 2015-12-17 13:58:42 +0100 | [diff] [blame] | 241 | |
| 242 | copy_fail: |
| 243 | // if we failed to copy the data we should not leave it laying around |
| 244 | // in target location. Do not check return value, we can not do any |
| 245 | // useful anyway. |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 246 | execRm(toPath, 80, 1, listener); |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 247 | fail: |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 248 | // clang-format off |
Henrik Baard | 7f52bca | 2015-11-26 12:05:13 +0100 | [diff] [blame] | 249 | { |
| 250 | std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock()); |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 251 | bringOnline(from); |
| 252 | bringOnline(to); |
Henrik Baard | 7f52bca | 2015-11-26 12:05:13 +0100 | [diff] [blame] | 253 | } |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 254 | // clang-format on |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 255 | notifyProgress(kMoveFailedInternalError, listener); |
| 256 | return -1; |
| 257 | } |
| 258 | |
| 259 | void MoveStorage(const std::shared_ptr<VolumeBase>& from, const std::shared_ptr<VolumeBase>& to, |
Paul Crowley | 14c8c07 | 2018-09-18 13:30:21 -0700 | [diff] [blame] | 260 | const android::sp<android::os::IVoldTaskListener>& listener) { |
Tri Vo | 242130f | 2019-06-18 15:33:01 -0700 | [diff] [blame] | 261 | android::power::WakeLock wl{kWakeLock}; |
Jeff Sharkey | 01a0e7f | 2017-10-17 16:06:32 -0600 | [diff] [blame] | 262 | |
| 263 | android::os::PersistableBundle extras; |
| 264 | status_t res = moveStorageInternal(from, to, listener); |
| 265 | if (listener) { |
| 266 | listener->onFinished(res, extras); |
| 267 | } |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | } // namespace vold |
| 271 | } // namespace android |