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