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
17 #include "MoveStorage.h"
18 #include "Utils.h"
19 #include "VolumeManager.h"
20
21 #include <android-base/logging.h>
22 #include <android-base/properties.h>
23 #include <android-base/stringprintf.h>
24 #include <private/android_filesystem_config.h>
25 #include <wakelock/wakelock.h>
26
27 #include <thread>
28
29 #include <dirent.h>
30 #include <sys/wait.h>
31
32 #define CONSTRAIN(amount, low, high) \
33 ((amount) < (low) ? (low) : ((amount) > (high) ? (high) : (amount)))
34
35 static const char* kPropBlockingExec = "persist.sys.blocking_exec";
36
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
49 static const char* kWakeLock = "MoveTask";
50
notifyProgress(int progress,const android::sp<android::os::IVoldTaskListener> & listener)51 static void notifyProgress(int progress,
52 const android::sp<android::os::IVoldTaskListener>& listener) {
53 if (listener) {
54 android::os::PersistableBundle extras;
55 listener->onStatus(progress, extras);
56 }
57 }
58
pushBackContents(const std::string & path,std::vector<std::string> & cmd,int searchLevels)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;
69 }
70 bool found = false;
71 struct dirent* ent;
72 while ((ent = readdir(dirp.get())) != NULL) {
73 if (IsDotOrDotDot(*ent)) continue;
74 auto subdir = path + "/" + ent->d_name;
75 found |= pushBackContents(subdir, cmd, searchLevels - 1);
76 }
77 return found;
78 }
79
execRm(const std::string & path,int startProgress,int stepProgress,const android::sp<android::os::IVoldTaskListener> & listener)80 static status_t execRm(const std::string& path, int startProgress, int stepProgress,
81 const android::sp<android::os::IVoldTaskListener>& listener) {
82 notifyProgress(startProgress, listener);
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 */
91 if (!pushBackContents(path, cmd, 2)) {
92 LOG(WARNING) << "No contents in " << path;
93 return OK;
94 }
95
96 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
97 return ForkExecvp(cmd);
98 }
99
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;
116 notifyProgress(
117 startProgress +
118 CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
119 listener);
120 }
121 return -1;
122 }
123
execCp(const std::string & fromPath,const std::string & toPath,int startProgress,int stepProgress,const android::sp<android::os::IVoldTaskListener> & listener)124 static status_t execCp(const std::string& fromPath, const std::string& toPath, int startProgress,
125 int stepProgress,
126 const android::sp<android::os::IVoldTaskListener>& listener) {
127 notifyProgress(startProgress, listener);
128
129 uint64_t expectedBytes = GetTreeBytes(fromPath);
130 uint64_t startFreeBytes = GetFreeBytes(toPath);
131
132 if (expectedBytes > startFreeBytes) {
133 LOG(ERROR) << "Data size " << expectedBytes << " is too large to fit in free space "
134 << startFreeBytes;
135 return -1;
136 }
137
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 */
144 if (!pushBackContents(fromPath, cmd, 1)) {
145 LOG(WARNING) << "No contents in " << fromPath;
146 return OK;
147 }
148 cmd.push_back(toPath.c_str());
149
150 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
151 return ForkExecvp(cmd);
152 }
153
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);
170 notifyProgress(
171 startProgress +
172 CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
173 listener);
174 }
175 return -1;
176 }
177
bringOffline(const std::shared_ptr<VolumeBase> & vol)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
bringOnline(const std::shared_ptr<VolumeBase> & vol)186 static void bringOnline(const std::shared_ptr<VolumeBase>& vol) {
187 vol->destroy();
188 vol->setSilent(false);
189 vol->create();
190 }
191
moveStorageInternal(const std::shared_ptr<VolumeBase> & from,const std::shared_ptr<VolumeBase> & to,const android::sp<android::os::IVoldTaskListener> & listener)192 static status_t moveStorageInternal(const std::shared_ptr<VolumeBase>& from,
193 const std::shared_ptr<VolumeBase>& to,
194 const android::sp<android::os::IVoldTaskListener>& listener) {
195 std::string fromPath;
196 std::string toPath;
197
198 // TODO: add support for public volumes
199 if (from->getType() != VolumeBase::Type::kEmulated) goto fail;
200 if (to->getType() != VolumeBase::Type::kEmulated) goto fail;
201
202 // Step 1: tear down volumes and mount silently without making
203 // visible to userspace apps
204 {
205 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
206 bringOffline(from);
207 bringOffline(to);
208 }
209
210 fromPath = from->getInternalPath();
211 toPath = to->getInternalPath();
212
213 // Step 2: clean up any stale data
214 if (execRm(toPath, 10, 10, listener) != OK) {
215 goto fail;
216 }
217
218 // Step 3: perform actual copy
219 if (execCp(fromPath, toPath, 20, 60, listener) != OK) {
220 goto copy_fail;
221 }
222
223 // NOTE: MountService watches for this magic value to know
224 // that move was successful
225 notifyProgress(82, listener);
226 {
227 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
228 bringOnline(from);
229 bringOnline(to);
230 }
231
232 // Step 4: clean up old data
233 if (execRm(fromPath, 85, 15, listener) != OK) {
234 goto fail;
235 }
236
237 notifyProgress(kMoveSucceeded, listener);
238 return OK;
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.
244 execRm(toPath, 80, 1, listener);
245 fail:
246 // clang-format off
247 {
248 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
249 bringOnline(from);
250 bringOnline(to);
251 }
252 // clang-format on
253 notifyProgress(kMoveFailedInternalError, listener);
254 return -1;
255 }
256
MoveStorage(const std::shared_ptr<VolumeBase> & from,const std::shared_ptr<VolumeBase> & to,const android::sp<android::os::IVoldTaskListener> & listener)257 void MoveStorage(const std::shared_ptr<VolumeBase>& from, const std::shared_ptr<VolumeBase>& to,
258 const android::sp<android::os::IVoldTaskListener>& listener) {
259 auto wl = android::wakelock::WakeLock::tryGet(kWakeLock);
260 if (!wl.has_value()) {
261 return;
262 }
263
264 android::os::PersistableBundle extras;
265 status_t res = moveStorageInternal(from, to, listener);
266 if (listener) {
267 listener->onFinished(res, extras);
268 }
269 }
270
271 } // namespace vold
272 } // namespace android
273