blob: 46240260cae144eea57fac4f40a7cb92af33484c [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 Sharkeyc86ab6f2015-06-26 14:02:09 -070024#include <hardware_legacy/power.h>
Jeff Sharkey8c24ae72018-01-04 16:46:34 -070025#include <private/android_filesystem_config.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
Chih-Hung Hsiehcc5d5802016-05-11 15:05:05 -070032#define CONSTRAIN(amount, low, high) ((amount) < (low) ? (low) : ((amount) > (high) ? (high) : (amount)))
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070033
Jeff Sharkey8c24ae72018-01-04 16:46:34 -070034static const char* kPropBlockingExec = "persist.sys.blocking_exec";
Jeff Sharkeyfce701b2016-07-29 15:52:41 -060035
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070036using android::base::StringPrintf;
37
38namespace android {
39namespace vold {
40
41// TODO: keep in sync with PackageManager
42static const int kMoveSucceeded = -100;
43static const int kMoveFailedInternalError = -6;
44
45static const char* kCpPath = "/system/bin/cp";
46static const char* kRmPath = "/system/bin/rm";
47
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -070048static const char* kWakeLock = "MoveTask";
49
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060050static void notifyProgress(int progress,
51 const android::sp<android::os::IVoldTaskListener>& listener) {
52 if (listener) {
Jeff Sharkey52f7a912017-09-15 12:57:44 -060053 android::os::PersistableBundle extras;
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060054 listener->onStatus(progress, extras);
Jeff Sharkey52f7a912017-09-15 12:57:44 -060055 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070056}
57
Jeff Sharkey46bb69f2017-06-21 13:52:23 -060058static status_t pushBackContents(const std::string& path, std::vector<std::string>& cmd,
59 bool addWildcard) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070060 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 Sharkey46bb69f2017-06-21 13:52:23 -060070 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 Sharkey1d6fbcc2015-04-24 16:00:03 -070075 found = true;
76 }
77 closedir(dir);
78 return found ? OK : -1;
79}
80
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -060081static status_t execRm(const std::string& path, int startProgress, int stepProgress,
82 const android::sp<android::os::IVoldTaskListener>& listener) {
83 notifyProgress(startProgress, listener);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070084
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 Sharkey46bb69f2017-06-21 13:52:23 -060092 if (pushBackContents(path, cmd, true) != OK) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -070093 LOG(WARNING) << "No contents in " << path;
94 return OK;
95 }
96
Jeff Sharkey8c24ae72018-01-04 16:46:34 -070097 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
98 return ForkExecvp(cmd);
99 }
100
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700101 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 Sharkey01a0e7f2017-10-17 16:06:32 -0600118 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress), listener);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700119 }
120 return -1;
121}
122
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600123static 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 Sharkey1d6fbcc2015-04-24 16:00:03 -0700126
127 uint64_t expectedBytes = GetTreeBytes(fromPath);
128 uint64_t startFreeBytes = GetFreeBytes(toPath);
129
Jeff Sharkeya0220a52017-04-03 17:11:45 -0600130 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 Sharkey1d6fbcc2015-04-24 16:00:03 -0700136 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 Sharkey46bb69f2017-06-21 13:52:23 -0600142 if (pushBackContents(fromPath, cmd, false) != OK) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700143 LOG(WARNING) << "No contents in " << fromPath;
144 return OK;
145 }
146 cmd.push_back(toPath.c_str());
147
Jeff Sharkey8c24ae72018-01-04 16:46:34 -0700148 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
149 return ForkExecvp(cmd);
150 }
151
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700152 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 Sharkey01a0e7f2017-10-17 16:06:32 -0600169 ((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress), listener);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700170 }
171 return -1;
172}
173
174static 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
182static void bringOnline(const std::shared_ptr<VolumeBase>& vol) {
183 vol->destroy();
184 vol->setSilent(false);
185 vol->create();
186}
187
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600188static 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 Sharkey1d6fbcc2015-04-24 16:00:03 -0700191 std::string fromPath;
192 std::string toPath;
193
194 // TODO: add support for public volumes
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600195 if (from->getType() != VolumeBase::Type::kEmulated) goto fail;
196 if (to->getType() != VolumeBase::Type::kEmulated) goto fail;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700197
198 // Step 1: tear down volumes and mount silently without making
199 // visible to userspace apps
Henrik Baard7f52bca2015-11-26 12:05:13 +0100200 {
201 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600202 bringOffline(from);
203 bringOffline(to);
Henrik Baard7f52bca2015-11-26 12:05:13 +0100204 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700205
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600206 fromPath = from->getInternalPath();
207 toPath = to->getInternalPath();
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700208
209 // Step 2: clean up any stale data
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600210 if (execRm(toPath, 10, 10, listener) != OK) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700211 goto fail;
212 }
213
214 // Step 3: perform actual copy
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600215 if (execCp(fromPath, toPath, 20, 60, listener) != OK) {
Henrik Baard77f156d2015-12-17 13:58:42 +0100216 goto copy_fail;
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700217 }
218
219 // NOTE: MountService watches for this magic value to know
220 // that move was successful
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600221 notifyProgress(82, listener);
Henrik Baard7f52bca2015-11-26 12:05:13 +0100222 {
223 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600224 bringOnline(from);
225 bringOnline(to);
Henrik Baard7f52bca2015-11-26 12:05:13 +0100226 }
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700227
228 // Step 4: clean up old data
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600229 if (execRm(fromPath, 85, 15, listener) != OK) {
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700230 goto fail;
231 }
232
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600233 notifyProgress(kMoveSucceeded, listener);
234 return OK;
Henrik Baard77f156d2015-12-17 13:58:42 +0100235
236copy_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 Sharkey01a0e7f2017-10-17 16:06:32 -0600240 execRm(toPath, 80, 1, listener);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700241fail:
Henrik Baard7f52bca2015-11-26 12:05:13 +0100242 {
243 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600244 bringOnline(from);
245 bringOnline(to);
Henrik Baard7f52bca2015-11-26 12:05:13 +0100246 }
Jeff Sharkey01a0e7f2017-10-17 16:06:32 -0600247 notifyProgress(kMoveFailedInternalError, listener);
248 return -1;
249}
250
251void 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 Sharkeyc86ab6f2015-06-26 14:02:09 -0700261 release_wake_lock(kWakeLock);
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700262}
263
264} // namespace vold
265} // namespace android