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 ((!strcmp(ent->d_name, ".")) || (!strcmp(ent->d_name, ".."))) {
74 continue;
75 }
76 auto subdir = path + "/" + ent->d_name;
77 found |= pushBackContents(subdir, cmd, searchLevels - 1);
78 }
79 return found;
80 }
81
execRm(const std::string & path,int startProgress,int stepProgress,const android::sp<android::os::IVoldTaskListener> & listener)82 static status_t execRm(const std::string& path, int startProgress, int stepProgress,
83 const android::sp<android::os::IVoldTaskListener>& listener) {
84 notifyProgress(startProgress, listener);
85
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 */
93 if (!pushBackContents(path, cmd, 2)) {
94 LOG(WARNING) << "No contents in " << path;
95 return OK;
96 }
97
98 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
99 return ForkExecvp(cmd);
100 }
101
102 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;
118 notifyProgress(
119 startProgress +
120 CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
121 listener);
122 }
123 return -1;
124 }
125
execCp(const std::string & fromPath,const std::string & toPath,int startProgress,int stepProgress,const android::sp<android::os::IVoldTaskListener> & listener)126 static status_t execCp(const std::string& fromPath, const std::string& toPath, int startProgress,
127 int stepProgress,
128 const android::sp<android::os::IVoldTaskListener>& listener) {
129 notifyProgress(startProgress, listener);
130
131 uint64_t expectedBytes = GetTreeBytes(fromPath);
132 uint64_t startFreeBytes = GetFreeBytes(toPath);
133
134 if (expectedBytes > startFreeBytes) {
135 LOG(ERROR) << "Data size " << expectedBytes << " is too large to fit in free space "
136 << startFreeBytes;
137 return -1;
138 }
139
140 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 */
146 if (!pushBackContents(fromPath, cmd, 1)) {
147 LOG(WARNING) << "No contents in " << fromPath;
148 return OK;
149 }
150 cmd.push_back(toPath.c_str());
151
152 if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
153 return ForkExecvp(cmd);
154 }
155
156 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);
172 notifyProgress(
173 startProgress +
174 CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
175 listener);
176 }
177 return -1;
178 }
179
bringOffline(const std::shared_ptr<VolumeBase> & vol)180 static 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
bringOnline(const std::shared_ptr<VolumeBase> & vol)188 static void bringOnline(const std::shared_ptr<VolumeBase>& vol) {
189 vol->destroy();
190 vol->setSilent(false);
191 vol->create();
192 }
193
moveStorageInternal(const std::shared_ptr<VolumeBase> & from,const std::shared_ptr<VolumeBase> & to,const android::sp<android::os::IVoldTaskListener> & listener)194 static status_t moveStorageInternal(const std::shared_ptr<VolumeBase>& from,
195 const std::shared_ptr<VolumeBase>& to,
196 const android::sp<android::os::IVoldTaskListener>& listener) {
197 std::string fromPath;
198 std::string toPath;
199
200 // TODO: add support for public volumes
201 if (from->getType() != VolumeBase::Type::kEmulated) goto fail;
202 if (to->getType() != VolumeBase::Type::kEmulated) goto fail;
203
204 // Step 1: tear down volumes and mount silently without making
205 // visible to userspace apps
206 {
207 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
208 bringOffline(from);
209 bringOffline(to);
210 }
211
212 fromPath = from->getInternalPath();
213 toPath = to->getInternalPath();
214
215 // Step 2: clean up any stale data
216 if (execRm(toPath, 10, 10, listener) != OK) {
217 goto fail;
218 }
219
220 // Step 3: perform actual copy
221 if (execCp(fromPath, toPath, 20, 60, listener) != OK) {
222 goto copy_fail;
223 }
224
225 // NOTE: MountService watches for this magic value to know
226 // that move was successful
227 notifyProgress(82, listener);
228 {
229 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
230 bringOnline(from);
231 bringOnline(to);
232 }
233
234 // Step 4: clean up old data
235 if (execRm(fromPath, 85, 15, listener) != OK) {
236 goto fail;
237 }
238
239 notifyProgress(kMoveSucceeded, listener);
240 return OK;
241
242 copy_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.
246 execRm(toPath, 80, 1, listener);
247 fail:
248 // clang-format off
249 {
250 std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
251 bringOnline(from);
252 bringOnline(to);
253 }
254 // clang-format on
255 notifyProgress(kMoveFailedInternalError, listener);
256 return -1;
257 }
258
MoveStorage(const std::shared_ptr<VolumeBase> & from,const std::shared_ptr<VolumeBase> & to,const android::sp<android::os::IVoldTaskListener> & listener)259 void MoveStorage(const std::shared_ptr<VolumeBase>& from, const std::shared_ptr<VolumeBase>& to,
260 const android::sp<android::os::IVoldTaskListener>& listener) {
261 android::wakelock::WakeLock wl{kWakeLock};
262
263 android::os::PersistableBundle extras;
264 status_t res = moveStorageInternal(from, to, listener);
265 if (listener) {
266 listener->onFinished(res, extras);
267 }
268 }
269
270 } // namespace vold
271 } // namespace android
272