blob: 2447cce45049b8ca6fa18fa2f4113e23065a9483 [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) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070073 if ((!strcmp(ent->d_name, ".")) || (!strcmp(ent->d_name, ".."))) {
74 continue;
75 }
Paul Crowley51209e92019-03-18 10:26:47 -070076 auto subdir = path + "/" + ent->d_name;
77 found |= pushBackContents(subdir, cmd, searchLevels - 1);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070078 }
Paul Crowley51209e92019-03-18 10:26:47 -070079 return found;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070080}
81
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060082static status_t execRm(const std::string& path, int startProgress, int stepProgress,
Paul Crowley14c8c072018-09-18 13:30:21 -070083 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060084 notifyProgress(startProgress, listener);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070085
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 Crowley51209e92019-03-18 10:26:47 -070093 if (!pushBackContents(path, cmd, 2)) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070094 LOG(WARNING) << "No contents in " << path;
95 return OK;
96 }
97
Jeff Sharkey8c24ae72018-01-04 16:46:34 -070098 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
99 return ForkExecvp(cmd);
100 }
101
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700102 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 Crowley14c8c072018-09-18 13:30:21 -0700118 notifyProgress(
119 startProgress +
120 CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
121 listener);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700122 }
123 return -1;
124}
125
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600126static status_t execCp(const std::string& fromPath, const std::string& toPath, int startProgress,
Paul Crowley14c8c072018-09-18 13:30:21 -0700127 int stepProgress,
128 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600129 notifyProgress(startProgress, listener);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700130
131 uint64_t expectedBytes = GetTreeBytes(fromPath);
132 uint64_t startFreeBytes = GetFreeBytes(toPath);
133
Jeff Sharkeya0220a52017-04-03 17:11:45 -0600134 if (expectedBytes > startFreeBytes) {
135 LOG(ERROR) << "Data size " << expectedBytes << " is too large to fit in free space "
Paul Crowley14c8c072018-09-18 13:30:21 -0700136 << startFreeBytes;
Jeff Sharkeya0220a52017-04-03 17:11:45 -0600137 return -1;
138 }
139
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700140 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 Crowley51209e92019-03-18 10:26:47 -0700146 if (!pushBackContents(fromPath, cmd, 1)) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700147 LOG(WARNING) << "No contents in " << fromPath;
148 return OK;
149 }
150 cmd.push_back(toPath.c_str());
151
Jeff Sharkey8c24ae72018-01-04 16:46:34 -0700152 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
153 return ForkExecvp(cmd);
154 }
155
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700156 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 Crowley14c8c072018-09-18 13:30:21 -0700172 notifyProgress(
173 startProgress +
174 CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
175 listener);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700176 }
177 return -1;
178}
179
180static 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
188static void bringOnline(const std::shared_ptr<VolumeBase>& vol) {
189 vol->destroy();
190 vol->setSilent(false);
191 vol->create();
192}
193
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600194static status_t moveStorageInternal(const std::shared_ptr<VolumeBase>& from,
Paul Crowley14c8c072018-09-18 13:30:21 -0700195 const std::shared_ptr<VolumeBase>& to,
196 const android::sp<android::os::IVoldTaskListener>& listener) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700197 std::string fromPath;
198 std::string toPath;
199
200 // TODO: add support for public volumes
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600201 if (from->getType() != VolumeBase::Type::kEmulated) goto fail;
202 if (to->getType() != VolumeBase::Type::kEmulated) goto fail;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700203
204 // Step 1: tear down volumes and mount silently without making
205 // visible to userspace apps
Henrik Baard7f52bca2015-11-26 12:05:13 +0100206 {
207 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600208 bringOffline(from);
209 bringOffline(to);
Henrik Baard7f52bca2015-11-26 12:05:13 +0100210 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700211
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600212 fromPath = from->getInternalPath();
213 toPath = to->getInternalPath();
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700214
215 // Step 2: clean up any stale data
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600216 if (execRm(toPath, 10, 10, listener) != OK) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700217 goto fail;
218 }
219
220 // Step 3: perform actual copy
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600221 if (execCp(fromPath, toPath, 20, 60, listener) != OK) {
Henrik Baard77f156d2015-12-17 13:58:42 +0100222 goto copy_fail;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700223 }
224
225 // NOTE: MountService watches for this magic value to know
226 // that move was successful
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600227 notifyProgress(82, listener);
Henrik Baard7f52bca2015-11-26 12:05:13 +0100228 {
229 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600230 bringOnline(from);
231 bringOnline(to);
Henrik Baard7f52bca2015-11-26 12:05:13 +0100232 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700233
234 // Step 4: clean up old data
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600235 if (execRm(fromPath, 85, 15, listener) != OK) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700236 goto fail;
237 }
238
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600239 notifyProgress(kMoveSucceeded, listener);
240 return OK;
Henrik Baard77f156d2015-12-17 13:58:42 +0100241
242copy_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 Sharkey01a0e7f2017-10-17 16:06:32 -0600246 execRm(toPath, 80, 1, listener);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700247fail:
Paul Crowley14c8c072018-09-18 13:30:21 -0700248 // clang-format off
Henrik Baard7f52bca2015-11-26 12:05:13 +0100249 {
250 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600251 bringOnline(from);
252 bringOnline(to);
Henrik Baard7f52bca2015-11-26 12:05:13 +0100253 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700254 // clang-format on
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600255 notifyProgress(kMoveFailedInternalError, listener);
256 return -1;
257}
258
259void MoveStorage(const std::shared_ptr<VolumeBase>& from, const std::shared_ptr<VolumeBase>& to,
Paul Crowley14c8c072018-09-18 13:30:21 -0700260 const android::sp<android::os::IVoldTaskListener>& listener) {
Tri Vo15bbe222019-06-21 12:21:48 -0700261 android::wakelock::WakeLock wl{kWakeLock};
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600262
263 android::os::PersistableBundle extras;
264 status_t res = moveStorageInternal(from, to, listener);
265 if (listener) {
266 listener->onFinished(res, extras);
267 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700268}
269
270} // namespace vold
271} // namespace android