2015-04-25 01:00:03 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
#include "MoveStorage.h"
|
2015-04-25 01:00:03 +02:00
|
|
|
#include "Utils.h"
|
|
|
|
#include "VolumeManager.h"
|
|
|
|
|
2015-12-05 00:50:53 +01:00
|
|
|
#include <android-base/logging.h>
|
2018-01-05 00:46:34 +01:00
|
|
|
#include <android-base/properties.h>
|
|
|
|
#include <android-base/stringprintf.h>
|
2015-06-26 23:02:09 +02:00
|
|
|
#include <hardware_legacy/power.h>
|
2018-01-05 00:46:34 +01:00
|
|
|
#include <private/android_filesystem_config.h>
|
2015-04-25 01:00:03 +02:00
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
#include <thread>
|
|
|
|
|
2015-04-25 01:00:03 +02:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2018-09-18 22:30:21 +02:00
|
|
|
#define CONSTRAIN(amount, low, high) \
|
|
|
|
((amount) < (low) ? (low) : ((amount) > (high) ? (high) : (amount)))
|
2015-04-25 01:00:03 +02:00
|
|
|
|
2018-01-05 00:46:34 +01:00
|
|
|
static const char* kPropBlockingExec = "persist.sys.blocking_exec";
|
2016-07-29 23:52:41 +02:00
|
|
|
|
2015-04-25 01:00:03 +02:00
|
|
|
using android::base::StringPrintf;
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace vold {
|
|
|
|
|
|
|
|
// TODO: keep in sync with PackageManager
|
|
|
|
static const int kMoveSucceeded = -100;
|
|
|
|
static const int kMoveFailedInternalError = -6;
|
|
|
|
|
|
|
|
static const char* kCpPath = "/system/bin/cp";
|
|
|
|
static const char* kRmPath = "/system/bin/rm";
|
|
|
|
|
2015-06-26 23:02:09 +02:00
|
|
|
static const char* kWakeLock = "MoveTask";
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
static void notifyProgress(int progress,
|
2018-09-18 22:30:21 +02:00
|
|
|
const android::sp<android::os::IVoldTaskListener>& listener) {
|
2017-10-18 00:06:32 +02:00
|
|
|
if (listener) {
|
2017-09-15 20:57:44 +02:00
|
|
|
android::os::PersistableBundle extras;
|
2017-10-18 00:06:32 +02:00
|
|
|
listener->onStatus(progress, extras);
|
2017-09-15 20:57:44 +02:00
|
|
|
}
|
2015-04-25 01:00:03 +02:00
|
|
|
}
|
|
|
|
|
Progress towards FBE and adoptable storage.
Offer to adopt storage devices on FBE devices, but keep it guarded
behind a system property for now, since we still need to work out key
storage details.
When migrating shared storage, leave user-specific /data/media
directories in place, since they already have the needed crypto
policies defined.
Enable journaling, quotas, and encrypt options when formatting
newly adopted devices. installd already gracefully handles older
partitions without quota enabled.
Test: cts-tradefed run commandAndExit cts-dev --abi armeabi-v7a -m CtsAppSecurityHostTestCases -t android.appsecurity.cts.AdoptableHostTest
Bug: 62290006, 36757864, 29117062, 37395736
Bug: 29923055, 25861755, 30230655, 37436961
Change-Id: Ibbeb6ec9db2394a279bbac221a2b20711d65494e
2017-06-21 21:52:23 +02:00
|
|
|
static status_t pushBackContents(const std::string& path, std::vector<std::string>& cmd,
|
2018-09-18 22:30:21 +02:00
|
|
|
bool addWildcard) {
|
2015-04-25 01:00:03 +02:00
|
|
|
DIR* dir = opendir(path.c_str());
|
|
|
|
if (dir == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
bool found = false;
|
|
|
|
struct dirent* ent;
|
|
|
|
while ((ent = readdir(dir)) != NULL) {
|
|
|
|
if ((!strcmp(ent->d_name, ".")) || (!strcmp(ent->d_name, ".."))) {
|
|
|
|
continue;
|
|
|
|
}
|
Progress towards FBE and adoptable storage.
Offer to adopt storage devices on FBE devices, but keep it guarded
behind a system property for now, since we still need to work out key
storage details.
When migrating shared storage, leave user-specific /data/media
directories in place, since they already have the needed crypto
policies defined.
Enable journaling, quotas, and encrypt options when formatting
newly adopted devices. installd already gracefully handles older
partitions without quota enabled.
Test: cts-tradefed run commandAndExit cts-dev --abi armeabi-v7a -m CtsAppSecurityHostTestCases -t android.appsecurity.cts.AdoptableHostTest
Bug: 62290006, 36757864, 29117062, 37395736
Bug: 29923055, 25861755, 30230655, 37436961
Change-Id: Ibbeb6ec9db2394a279bbac221a2b20711d65494e
2017-06-21 21:52:23 +02:00
|
|
|
if (addWildcard) {
|
|
|
|
cmd.push_back(StringPrintf("%s/%s/*", path.c_str(), ent->d_name));
|
|
|
|
} else {
|
|
|
|
cmd.push_back(StringPrintf("%s/%s", path.c_str(), ent->d_name));
|
|
|
|
}
|
2015-04-25 01:00:03 +02:00
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
closedir(dir);
|
|
|
|
return found ? OK : -1;
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
static status_t execRm(const std::string& path, int startProgress, int stepProgress,
|
2018-09-18 22:30:21 +02:00
|
|
|
const android::sp<android::os::IVoldTaskListener>& listener) {
|
2017-10-18 00:06:32 +02:00
|
|
|
notifyProgress(startProgress, listener);
|
2015-04-25 01:00:03 +02:00
|
|
|
|
|
|
|
uint64_t expectedBytes = GetTreeBytes(path);
|
|
|
|
uint64_t startFreeBytes = GetFreeBytes(path);
|
|
|
|
|
|
|
|
std::vector<std::string> cmd;
|
|
|
|
cmd.push_back(kRmPath);
|
|
|
|
cmd.push_back("-f"); /* force: remove without confirmation, no error if it doesn't exist */
|
|
|
|
cmd.push_back("-R"); /* recursive: remove directory contents */
|
Progress towards FBE and adoptable storage.
Offer to adopt storage devices on FBE devices, but keep it guarded
behind a system property for now, since we still need to work out key
storage details.
When migrating shared storage, leave user-specific /data/media
directories in place, since they already have the needed crypto
policies defined.
Enable journaling, quotas, and encrypt options when formatting
newly adopted devices. installd already gracefully handles older
partitions without quota enabled.
Test: cts-tradefed run commandAndExit cts-dev --abi armeabi-v7a -m CtsAppSecurityHostTestCases -t android.appsecurity.cts.AdoptableHostTest
Bug: 62290006, 36757864, 29117062, 37395736
Bug: 29923055, 25861755, 30230655, 37436961
Change-Id: Ibbeb6ec9db2394a279bbac221a2b20711d65494e
2017-06-21 21:52:23 +02:00
|
|
|
if (pushBackContents(path, cmd, true) != OK) {
|
2015-04-25 01:00:03 +02:00
|
|
|
LOG(WARNING) << "No contents in " << path;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2018-01-05 00:46:34 +01:00
|
|
|
if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
|
|
|
|
return ForkExecvp(cmd);
|
|
|
|
}
|
|
|
|
|
2015-04-25 01:00:03 +02:00
|
|
|
pid_t pid = ForkExecvpAsync(cmd);
|
|
|
|
if (pid == -1) return -1;
|
|
|
|
|
|
|
|
int status;
|
|
|
|
while (true) {
|
|
|
|
if (waitpid(pid, &status, WNOHANG) == pid) {
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
LOG(DEBUG) << "Finished rm with status " << WEXITSTATUS(status);
|
|
|
|
return (WEXITSTATUS(status) == 0) ? OK : -1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
uint64_t deltaFreeBytes = GetFreeBytes(path) - startFreeBytes;
|
2018-09-18 22:30:21 +02:00
|
|
|
notifyProgress(
|
|
|
|
startProgress +
|
|
|
|
CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
|
|
|
|
listener);
|
2015-04-25 01:00:03 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
static status_t execCp(const std::string& fromPath, const std::string& toPath, int startProgress,
|
2018-09-18 22:30:21 +02:00
|
|
|
int stepProgress,
|
|
|
|
const android::sp<android::os::IVoldTaskListener>& listener) {
|
2017-10-18 00:06:32 +02:00
|
|
|
notifyProgress(startProgress, listener);
|
2015-04-25 01:00:03 +02:00
|
|
|
|
|
|
|
uint64_t expectedBytes = GetTreeBytes(fromPath);
|
|
|
|
uint64_t startFreeBytes = GetFreeBytes(toPath);
|
|
|
|
|
2017-04-04 01:11:45 +02:00
|
|
|
if (expectedBytes > startFreeBytes) {
|
|
|
|
LOG(ERROR) << "Data size " << expectedBytes << " is too large to fit in free space "
|
2018-09-18 22:30:21 +02:00
|
|
|
<< startFreeBytes;
|
2017-04-04 01:11:45 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-04-25 01:00:03 +02:00
|
|
|
std::vector<std::string> cmd;
|
|
|
|
cmd.push_back(kCpPath);
|
|
|
|
cmd.push_back("-p"); /* preserve timestamps, ownership, and permissions */
|
|
|
|
cmd.push_back("-R"); /* recurse into subdirectories (DEST must be a directory) */
|
|
|
|
cmd.push_back("-P"); /* Do not follow symlinks [default] */
|
|
|
|
cmd.push_back("-d"); /* don't dereference symlinks */
|
Progress towards FBE and adoptable storage.
Offer to adopt storage devices on FBE devices, but keep it guarded
behind a system property for now, since we still need to work out key
storage details.
When migrating shared storage, leave user-specific /data/media
directories in place, since they already have the needed crypto
policies defined.
Enable journaling, quotas, and encrypt options when formatting
newly adopted devices. installd already gracefully handles older
partitions without quota enabled.
Test: cts-tradefed run commandAndExit cts-dev --abi armeabi-v7a -m CtsAppSecurityHostTestCases -t android.appsecurity.cts.AdoptableHostTest
Bug: 62290006, 36757864, 29117062, 37395736
Bug: 29923055, 25861755, 30230655, 37436961
Change-Id: Ibbeb6ec9db2394a279bbac221a2b20711d65494e
2017-06-21 21:52:23 +02:00
|
|
|
if (pushBackContents(fromPath, cmd, false) != OK) {
|
2015-04-25 01:00:03 +02:00
|
|
|
LOG(WARNING) << "No contents in " << fromPath;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
cmd.push_back(toPath.c_str());
|
|
|
|
|
2018-01-05 00:46:34 +01:00
|
|
|
if (android::base::GetBoolProperty(kPropBlockingExec, false)) {
|
|
|
|
return ForkExecvp(cmd);
|
|
|
|
}
|
|
|
|
|
2015-04-25 01:00:03 +02:00
|
|
|
pid_t pid = ForkExecvpAsync(cmd);
|
|
|
|
if (pid == -1) return -1;
|
|
|
|
|
|
|
|
int status;
|
|
|
|
while (true) {
|
|
|
|
if (waitpid(pid, &status, WNOHANG) == pid) {
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
LOG(DEBUG) << "Finished cp with status " << WEXITSTATUS(status);
|
|
|
|
return (WEXITSTATUS(status) == 0) ? OK : -1;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
uint64_t deltaFreeBytes = startFreeBytes - GetFreeBytes(toPath);
|
2018-09-18 22:30:21 +02:00
|
|
|
notifyProgress(
|
|
|
|
startProgress +
|
|
|
|
CONSTRAIN((int)((deltaFreeBytes * stepProgress) / expectedBytes), 0, stepProgress),
|
|
|
|
listener);
|
2015-04-25 01:00:03 +02:00
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bringOffline(const std::shared_ptr<VolumeBase>& vol) {
|
|
|
|
vol->destroy();
|
|
|
|
vol->setSilent(true);
|
|
|
|
vol->create();
|
|
|
|
vol->setMountFlags(0);
|
|
|
|
vol->mount();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bringOnline(const std::shared_ptr<VolumeBase>& vol) {
|
|
|
|
vol->destroy();
|
|
|
|
vol->setSilent(false);
|
|
|
|
vol->create();
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
static status_t moveStorageInternal(const std::shared_ptr<VolumeBase>& from,
|
2018-09-18 22:30:21 +02:00
|
|
|
const std::shared_ptr<VolumeBase>& to,
|
|
|
|
const android::sp<android::os::IVoldTaskListener>& listener) {
|
2015-04-25 01:00:03 +02:00
|
|
|
std::string fromPath;
|
|
|
|
std::string toPath;
|
|
|
|
|
|
|
|
// TODO: add support for public volumes
|
2017-10-18 00:06:32 +02:00
|
|
|
if (from->getType() != VolumeBase::Type::kEmulated) goto fail;
|
|
|
|
if (to->getType() != VolumeBase::Type::kEmulated) goto fail;
|
2015-04-25 01:00:03 +02:00
|
|
|
|
|
|
|
// Step 1: tear down volumes and mount silently without making
|
|
|
|
// visible to userspace apps
|
2015-11-26 12:05:13 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
|
2017-10-18 00:06:32 +02:00
|
|
|
bringOffline(from);
|
|
|
|
bringOffline(to);
|
2015-11-26 12:05:13 +01:00
|
|
|
}
|
2015-04-25 01:00:03 +02:00
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
fromPath = from->getInternalPath();
|
|
|
|
toPath = to->getInternalPath();
|
2015-04-25 01:00:03 +02:00
|
|
|
|
|
|
|
// Step 2: clean up any stale data
|
2017-10-18 00:06:32 +02:00
|
|
|
if (execRm(toPath, 10, 10, listener) != OK) {
|
2015-04-25 01:00:03 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 3: perform actual copy
|
2017-10-18 00:06:32 +02:00
|
|
|
if (execCp(fromPath, toPath, 20, 60, listener) != OK) {
|
2015-12-17 13:58:42 +01:00
|
|
|
goto copy_fail;
|
2015-04-25 01:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: MountService watches for this magic value to know
|
|
|
|
// that move was successful
|
2017-10-18 00:06:32 +02:00
|
|
|
notifyProgress(82, listener);
|
2015-11-26 12:05:13 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
|
2017-10-18 00:06:32 +02:00
|
|
|
bringOnline(from);
|
|
|
|
bringOnline(to);
|
2015-11-26 12:05:13 +01:00
|
|
|
}
|
2015-04-25 01:00:03 +02:00
|
|
|
|
|
|
|
// Step 4: clean up old data
|
2017-10-18 00:06:32 +02:00
|
|
|
if (execRm(fromPath, 85, 15, listener) != OK) {
|
2015-04-25 01:00:03 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2017-10-18 00:06:32 +02:00
|
|
|
notifyProgress(kMoveSucceeded, listener);
|
|
|
|
return OK;
|
2015-12-17 13:58:42 +01:00
|
|
|
|
|
|
|
copy_fail:
|
|
|
|
// if we failed to copy the data we should not leave it laying around
|
|
|
|
// in target location. Do not check return value, we can not do any
|
|
|
|
// useful anyway.
|
2017-10-18 00:06:32 +02:00
|
|
|
execRm(toPath, 80, 1, listener);
|
2015-04-25 01:00:03 +02:00
|
|
|
fail:
|
2018-09-18 22:30:21 +02:00
|
|
|
// clang-format off
|
2015-11-26 12:05:13 +01:00
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(VolumeManager::Instance()->getLock());
|
2017-10-18 00:06:32 +02:00
|
|
|
bringOnline(from);
|
|
|
|
bringOnline(to);
|
|
|
|
}
|
2018-09-18 22:30:21 +02:00
|
|
|
// clang-format on
|
2017-10-18 00:06:32 +02:00
|
|
|
notifyProgress(kMoveFailedInternalError, listener);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MoveStorage(const std::shared_ptr<VolumeBase>& from, const std::shared_ptr<VolumeBase>& to,
|
2018-09-18 22:30:21 +02:00
|
|
|
const android::sp<android::os::IVoldTaskListener>& listener) {
|
2017-10-18 00:06:32 +02:00
|
|
|
acquire_wake_lock(PARTIAL_WAKE_LOCK, kWakeLock);
|
|
|
|
|
|
|
|
android::os::PersistableBundle extras;
|
|
|
|
status_t res = moveStorageInternal(from, to, listener);
|
|
|
|
if (listener) {
|
|
|
|
listener->onFinished(res, extras);
|
2015-11-26 12:05:13 +01:00
|
|
|
}
|
2017-10-18 00:06:32 +02:00
|
|
|
|
2015-06-26 23:02:09 +02:00
|
|
|
release_wake_lock(kWakeLock);
|
2015-04-25 01:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace vold
|
|
|
|
} // namespace android
|