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