blob: 3f636a22fba936ec088e6e4e416d841009b3cc21 [file] [log] [blame]
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -07001/*
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 Sharkey01a0e7f2017-10-17 16:06:32 -060017#include "MoveStorage.h"
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070018#include "Utils.h"
19#include "VolumeManager.h"
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070020
Elliott Hughes7e128fb2015-12-04 15:50:53 -080021#include <android-base/logging.h>
Jeff Sharkey8c24ae72018-01-04 16:46:34 -070022#include <android-base/properties.h>
23#include <android-base/stringprintf.h>
Jeff Sharkey8c24ae72018-01-04 16:46:34 -070024#include <private/android_filesystem_config.h>
Tri Vo15bbe222019-06-21 12:21:48 -070025#include <wakelock/wakelock.h>
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070026
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060027#include <thread>
28
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070029#include <dirent.h>
30#include <sys/wait.h>
31
Paul Crowley14c8c072018-09-18 13:30:21 -070032#define CONSTRAIN(amount, low, high) \
33 ((amount) < (low) ? (low) : ((amount) > (high) ? (high) : (amount)))
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070034
Jeff Sharkey8c24ae72018-01-04 16:46:34 -070035static const char* kPropBlockingExec = "persist.sys.blocking_exec";
Jeff Sharkeyfce701b2016-07-29 15:52:41 -060036
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070037using android::base::StringPrintf;
38
39namespace android {
40namespace vold {
41
42// TODO: keep in sync with PackageManager
43static const int kMoveSucceeded = -100;
44static const int kMoveFailedInternalError = -6;
45
46static const char* kCpPath = "/system/bin/cp";
47static const char* kRmPath = "/system/bin/rm";
48
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070049static const char* kWakeLock = "MoveTask";
50
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060051static void notifyProgress(int progress,
Paul Crowley14c8c072018-09-18 13:30:21 -070052 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060053 if (listener) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -060054 android::os::PersistableBundle extras;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060055 listener->onStatus(progress, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -060056 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070057}
58
Paul Crowley51209e92019-03-18 10:26:47 -070059static 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 Sharkey1d6fbcc2015-04-24 16:00:03 -070069 }
70 bool found = false;
71 struct dirent* ent;
Paul Crowley51209e92019-03-18 10:26:47 -070072 while ((ent = readdir(dirp.get())) != NULL) {
Eric Biggers7bcf4272020-11-02 15:31:56 -080073 if (IsDotOrDotDot(*ent)) continue;
Paul Crowley51209e92019-03-18 10:26:47 -070074 auto subdir = path + "/" + ent->d_name;
75 found |= pushBackContents(subdir, cmd, searchLevels - 1);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070076 }
Paul Crowley51209e92019-03-18 10:26:47 -070077 return found;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070078}
79
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060080static status_t execRm(const std::string& path, int startProgress, int stepProgress,
Paul Crowley14c8c072018-09-18 13:30:21 -070081 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060082 notifyProgress(startProgress, listener);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070083
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 Crowley51209e92019-03-18 10:26:47 -070091 if (!pushBackContents(path, cmd, 2)) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070092 LOG(WARNING) << "No contents in " << path;
93 return OK;
94 }
95
Jeff Sharkey8c24ae72018-01-04 16:46:34 -070096 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
97 return ForkExecvp(cmd);
98 }
99
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700100 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 Crowley14c8c072018-09-18 13:30:21 -0700116 notifyProgress(
117 startProgress +
118 CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
119 listener);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700120 }
121 return -1;
122}
123
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600124static status_t execCp(const std::string& fromPath, const std::string& toPath, int startProgress,
Paul Crowley14c8c072018-09-18 13:30:21 -0700125 int stepProgress,
126 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600127 notifyProgress(startProgress, listener);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700128
129 uint64_t expectedBytes = GetTreeBytes(fromPath);
130 uint64_t startFreeBytes = GetFreeBytes(toPath);
131
Jeff Sharkeya0220a52017-04-03 17:11:45 -0600132 if (expectedBytes > startFreeBytes) {
133 LOG(ERROR) << "Data size " << expectedBytes << " is too large to fit in free space "
Paul Crowley14c8c072018-09-18 13:30:21 -0700134 << startFreeBytes;
Jeff Sharkeya0220a52017-04-03 17:11:45 -0600135 return -1;
136 }
137
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700138 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 Crowley51209e92019-03-18 10:26:47 -0700144 if (!pushBackContents(fromPath, cmd, 1)) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700145 LOG(WARNING) << "No contents in " << fromPath;
146 return OK;
147 }
148 cmd.push_back(toPath.c_str());
149
Jeff Sharkey8c24ae72018-01-04 16:46:34 -0700150 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
151 return ForkExecvp(cmd);
152 }
153
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700154 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 Crowley14c8c072018-09-18 13:30:21 -0700170 notifyProgress(
171 startProgress +
172 CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
173 listener);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700174 }
175 return -1;
176}
177
178static 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
186static void bringOnline(const std::shared_ptr<VolumeBase>& vol) {
187 vol->destroy();
188 vol->setSilent(false);
189 vol->create();
190}
191
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600192static status_t moveStorageInternal(const std::shared_ptr<VolumeBase>& from,
Paul Crowley14c8c072018-09-18 13:30:21 -0700193 const std::shared_ptr<VolumeBase>& to,
194 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700195 std::string fromPath;
196 std::string toPath;
197
198 // TODO: add support for public volumes
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600199 if (from->getType() != VolumeBase::Type::kEmulated) goto fail;
200 if (to->getType() != VolumeBase::Type::kEmulated) goto fail;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700201
202 // Step 1: tear down volumes and mount silently without making
203 // visible to userspace apps
Henrik Baard7f52bca2015-11-26 12:05:13 +0100204 {
205 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600206 bringOffline(from);
207 bringOffline(to);
Henrik Baard7f52bca2015-11-26 12:05:13 +0100208 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700209
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600210 fromPath = from->getInternalPath();
211 toPath = to->getInternalPath();
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700212
213 // Step 2: clean up any stale data
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600214 if (execRm(toPath, 10, 10, listener) != OK) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700215 goto fail;
216 }
217
218 // Step 3: perform actual copy
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600219 if (execCp(fromPath, toPath, 20, 60, listener) != OK) {
Henrik Baard77f156d2015-12-17 13:58:42 +0100220 goto copy_fail;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700221 }
222
223 // NOTE: MountService watches for this magic value to know
224 // that move was successful
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600225 notifyProgress(82, listener);
Henrik Baard7f52bca2015-11-26 12:05:13 +0100226 {
227 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600228 bringOnline(from);
229 bringOnline(to);
Henrik Baard7f52bca2015-11-26 12:05:13 +0100230 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700231
232 // Step 4: clean up old data
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600233 if (execRm(fromPath, 85, 15, listener) != OK) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700234 goto fail;
235 }
236
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600237 notifyProgress(kMoveSucceeded, listener);
238 return OK;
Henrik Baard77f156d2015-12-17 13:58:42 +0100239
240copy_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 Sharkey01a0e7f2017-10-17 16:06:32 -0600244 execRm(toPath, 80, 1, listener);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700245fail:
Paul Crowley14c8c072018-09-18 13:30:21 -0700246 // clang-format off
Henrik Baard7f52bca2015-11-26 12:05:13 +0100247 {
248 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600249 bringOnline(from);
250 bringOnline(to);
Henrik Baard7f52bca2015-11-26 12:05:13 +0100251 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700252 // clang-format on
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600253 notifyProgress(kMoveFailedInternalError, listener);
254 return -1;
255}
256
257void MoveStorage(const std::shared_ptr<VolumeBase>& from, const std::shared_ptr<VolumeBase>& to,
Paul Crowley14c8c072018-09-18 13:30:21 -0700258 const android::sp<android::os::IVoldTaskListener>& listener) {
Tri Vo15bbe222019-06-21 12:21:48 -0700259 android::wakelock::WakeLock wl{kWakeLock};
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600260
261 android::os::PersistableBundle extras;
262 status_t res = moveStorageInternal(from, to, listener);
263 if (listener) {
264 listener->onFinished(res, extras);
265 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700266}
267
268} // namespace vold
269} // namespace android