2015-03-03 06:01:40 +01: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "EmulatedVolume.h"
|
2019-07-19 17:46:53 +02:00
|
|
|
|
|
|
|
#include "AppFuseUtil.h"
|
2015-03-03 06:01:40 +01:00
|
|
|
#include "Utils.h"
|
2018-09-18 22:07:45 +02:00
|
|
|
#include "VolumeManager.h"
|
2015-03-03 06:01:40 +01:00
|
|
|
|
2015-12-05 00:50:53 +01:00
|
|
|
#include <android-base/logging.h>
|
2019-07-19 17:46:53 +02:00
|
|
|
#include <android-base/properties.h>
|
2020-03-16 14:37:33 +01:00
|
|
|
#include <android-base/scopeguard.h>
|
2018-08-01 19:24:13 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2015-03-03 06:01:40 +01:00
|
|
|
#include <cutils/fs.h>
|
|
|
|
#include <private/android_filesystem_config.h>
|
2017-09-18 22:47:10 +02:00
|
|
|
#include <utils/Timers.h>
|
2015-03-03 06:01:40 +01:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
2017-05-18 18:08:24 +02:00
|
|
|
#include <sys/sysmacros.h>
|
2018-09-19 00:14:18 +02:00
|
|
|
#include <sys/types.h>
|
2015-03-03 06:01:40 +01:00
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2015-03-16 18:35:17 +01:00
|
|
|
using android::base::StringPrintf;
|
|
|
|
|
2015-03-03 06:01:40 +01:00
|
|
|
namespace android {
|
|
|
|
namespace vold {
|
|
|
|
|
2019-12-09 14:18:01 +01:00
|
|
|
static const char* kSdcardFsPath = "/system/bin/sdcard";
|
2015-03-03 06:01:40 +01:00
|
|
|
|
2019-09-25 15:37:38 +02:00
|
|
|
EmulatedVolume::EmulatedVolume(const std::string& rawPath, int userId)
|
2019-12-09 14:18:01 +01:00
|
|
|
: VolumeBase(Type::kEmulated) {
|
2019-09-25 15:37:38 +02:00
|
|
|
setId(StringPrintf("emulated;%u", userId));
|
2015-04-13 01:03:33 +02:00
|
|
|
mRawPath = rawPath;
|
2015-06-24 20:49:24 +02:00
|
|
|
mLabel = "emulated";
|
2019-12-11 14:57:59 +01:00
|
|
|
mFuseMounted = false;
|
2020-05-12 07:58:42 +02:00
|
|
|
mUseSdcardFs = IsSdcardfsUsed();
|
2020-02-11 15:31:24 +01:00
|
|
|
mAppDataIsolationEnabled = base::GetBoolProperty(kVoldAppDataIsolationEnabled, false);
|
2015-04-13 01:03:33 +02:00
|
|
|
}
|
2015-03-03 06:01:40 +01:00
|
|
|
|
2019-09-25 15:37:38 +02:00
|
|
|
EmulatedVolume::EmulatedVolume(const std::string& rawPath, dev_t device, const std::string& fsUuid,
|
|
|
|
int userId)
|
2019-12-09 14:18:01 +01:00
|
|
|
: VolumeBase(Type::kEmulated) {
|
2019-09-25 15:37:38 +02:00
|
|
|
setId(StringPrintf("emulated:%u,%u;%u", major(device), minor(device), userId));
|
2015-03-03 06:01:40 +01:00
|
|
|
mRawPath = rawPath;
|
2015-06-24 20:49:24 +02:00
|
|
|
mLabel = fsUuid;
|
2019-12-12 14:41:46 +01:00
|
|
|
mFuseMounted = false;
|
2020-05-12 07:58:42 +02:00
|
|
|
mUseSdcardFs = IsSdcardfsUsed();
|
2020-02-11 15:31:24 +01:00
|
|
|
mAppDataIsolationEnabled = base::GetBoolProperty(kVoldAppDataIsolationEnabled, false);
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
2018-09-19 00:14:18 +02:00
|
|
|
EmulatedVolume::~EmulatedVolume() {}
|
2015-03-03 06:01:40 +01:00
|
|
|
|
2019-11-28 11:53:53 +01:00
|
|
|
std::string EmulatedVolume::getLabel() {
|
2015-07-07 23:37:03 +02:00
|
|
|
// We could have migrated storage to an adopted private volume, so always
|
|
|
|
// call primary storage "emulated" to avoid media rescans.
|
|
|
|
if (getMountFlags() & MountFlags::kPrimary) {
|
2019-11-28 11:53:53 +01:00
|
|
|
return "emulated";
|
|
|
|
} else {
|
|
|
|
return mLabel;
|
2015-07-07 23:37:03 +02:00
|
|
|
}
|
2019-11-28 11:53:53 +01:00
|
|
|
}
|
|
|
|
|
2020-01-31 15:23:09 +01:00
|
|
|
// Creates a bind mount from source to target
|
2020-03-16 14:37:33 +01:00
|
|
|
static status_t doFuseBindMount(const std::string& source, const std::string& target,
|
|
|
|
std::list<std::string>& pathsToUnmount) {
|
2020-01-11 19:38:37 +01:00
|
|
|
LOG(INFO) << "Bind mounting " << source << " on " << target;
|
|
|
|
auto status = BindMount(source, target);
|
|
|
|
if (status != OK) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Bind mounted " << source << " on " << target;
|
2020-03-16 14:37:33 +01:00
|
|
|
pathsToUnmount.push_front(target);
|
2020-01-11 19:38:37 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-01-06 09:48:14 +01:00
|
|
|
status_t EmulatedVolume::mountFuseBindMounts() {
|
|
|
|
std::string androidSource;
|
|
|
|
std::string label = getLabel();
|
|
|
|
int userId = getMountUserId();
|
2020-03-16 14:37:33 +01:00
|
|
|
std::list<std::string> pathsToUnmount;
|
|
|
|
|
|
|
|
auto unmounter = [&]() {
|
|
|
|
LOG(INFO) << "mountFuseBindMounts() unmount scope_guard running";
|
|
|
|
for (const auto& path : pathsToUnmount) {
|
|
|
|
LOG(INFO) << "Unmounting " << path;
|
|
|
|
auto status = UnmountTree(path);
|
|
|
|
if (status != OK) {
|
|
|
|
LOG(INFO) << "Failed to unmount " << path;
|
|
|
|
} else {
|
|
|
|
LOG(INFO) << "Unmounted " << path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
auto unmount_guard = android::base::make_scope_guard(unmounter);
|
2020-01-06 09:48:14 +01:00
|
|
|
|
|
|
|
if (mUseSdcardFs) {
|
|
|
|
androidSource = StringPrintf("/mnt/runtime/default/%s/%d/Android", label.c_str(), userId);
|
|
|
|
} else {
|
|
|
|
androidSource = StringPrintf("/%s/%d/Android", mRawPath.c_str(), userId);
|
|
|
|
}
|
2020-02-11 15:31:24 +01:00
|
|
|
|
|
|
|
status_t status = OK;
|
|
|
|
// When app data isolation is enabled, obb/ will be mounted per app, otherwise we should
|
|
|
|
// bind mount the whole Android/ to speed up reading.
|
|
|
|
if (!mAppDataIsolationEnabled) {
|
2020-03-16 14:37:33 +01:00
|
|
|
std::string androidDataSource = StringPrintf("%s/data", androidSource.c_str());
|
|
|
|
std::string androidDataTarget(
|
|
|
|
StringPrintf("/mnt/user/%d/%s/%d/Android/data", userId, label.c_str(), userId));
|
|
|
|
status = doFuseBindMount(androidDataSource, androidDataTarget, pathsToUnmount);
|
|
|
|
if (status != OK) {
|
|
|
|
return status;
|
|
|
|
}
|
2019-11-28 11:56:13 +01:00
|
|
|
|
2020-03-16 14:37:33 +01:00
|
|
|
std::string androidObbSource = StringPrintf("%s/obb", androidSource.c_str());
|
|
|
|
std::string androidObbTarget(
|
|
|
|
StringPrintf("/mnt/user/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
|
|
|
|
status = doFuseBindMount(androidObbSource, androidObbTarget, pathsToUnmount);
|
|
|
|
if (status != OK) {
|
|
|
|
return status;
|
|
|
|
}
|
2019-11-28 11:56:13 +01:00
|
|
|
}
|
2020-03-17 16:15:42 +01:00
|
|
|
|
2020-01-11 19:38:37 +01:00
|
|
|
// Installers get the same view as all other apps, with the sole exception that the
|
|
|
|
// OBB dirs (Android/obb) are writable to them. On sdcardfs devices, this requires
|
|
|
|
// a special bind mount, since app-private and OBB dirs share the same GID, but we
|
|
|
|
// only want to give access to the latter.
|
2020-03-16 14:37:33 +01:00
|
|
|
if (mUseSdcardFs) {
|
2020-04-07 14:43:20 +02:00
|
|
|
std::string obbSource(StringPrintf("/mnt/runtime/write/%s/%d/Android/obb",
|
|
|
|
label.c_str(), userId));
|
|
|
|
std::string obbInstallerTarget(StringPrintf("/mnt/installer/%d/%s/%d/Android/obb",
|
|
|
|
userId, label.c_str(), userId));
|
|
|
|
|
|
|
|
status = doFuseBindMount(obbSource, obbInstallerTarget, pathsToUnmount);
|
|
|
|
if (status != OK) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
} else if (mAppDataIsolationEnabled) {
|
|
|
|
std::string obbSource(StringPrintf("%s/obb", androidSource.c_str()));
|
|
|
|
std::string obbInstallerTarget(StringPrintf("/mnt/installer/%d/%s/%d/Android/obb",
|
|
|
|
userId, label.c_str(), userId));
|
2020-01-11 19:38:37 +01:00
|
|
|
|
2020-04-07 14:43:20 +02:00
|
|
|
status = doFuseBindMount(obbSource, obbInstallerTarget, pathsToUnmount);
|
|
|
|
if (status != OK) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// /mnt/androidwriteable is similar to /mnt/installer, but it's for
|
|
|
|
// MOUNT_EXTERNAL_ANDROID_WRITABLE apps and it can also access DATA (Android/data) dirs.
|
|
|
|
if (mAppDataIsolationEnabled) {
|
|
|
|
std::string obbSource = mUseSdcardFs ?
|
|
|
|
StringPrintf("/mnt/runtime/write/%s/%d/Android/obb", label.c_str(), userId)
|
|
|
|
: StringPrintf("%s/obb", androidSource.c_str());
|
|
|
|
|
|
|
|
std::string obbAndroidWritableTarget(
|
|
|
|
StringPrintf("/mnt/androidwritable/%d/%s/%d/Android/obb",
|
|
|
|
userId, label.c_str(), userId));
|
|
|
|
|
|
|
|
status = doFuseBindMount(obbSource, obbAndroidWritableTarget, pathsToUnmount);
|
|
|
|
if (status != OK) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string dataSource = mUseSdcardFs ?
|
|
|
|
StringPrintf("/mnt/runtime/write/%s/%d/Android/data", label.c_str(), userId)
|
|
|
|
: StringPrintf("%s/data", androidSource.c_str());
|
|
|
|
std::string dataTarget(StringPrintf("/mnt/androidwritable/%d/%s/%d/Android/data",
|
|
|
|
userId, label.c_str(), userId));
|
|
|
|
|
|
|
|
status = doFuseBindMount(dataSource, dataTarget, pathsToUnmount);
|
2020-03-16 14:37:33 +01:00
|
|
|
if (status != OK) {
|
|
|
|
return status;
|
|
|
|
}
|
2020-01-11 19:38:37 +01:00
|
|
|
}
|
2020-03-16 14:37:33 +01:00
|
|
|
unmount_guard.Disable();
|
2019-11-28 11:56:13 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-01-06 09:48:14 +01:00
|
|
|
status_t EmulatedVolume::unmountFuseBindMounts() {
|
|
|
|
std::string label = getLabel();
|
|
|
|
int userId = getMountUserId();
|
|
|
|
|
2020-04-07 14:43:20 +02:00
|
|
|
if (mUseSdcardFs || mAppDataIsolationEnabled) {
|
2020-01-11 19:38:37 +01:00
|
|
|
std::string installerTarget(
|
|
|
|
StringPrintf("/mnt/installer/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
|
|
|
|
LOG(INFO) << "Unmounting " << installerTarget;
|
|
|
|
auto status = UnmountTree(installerTarget);
|
|
|
|
if (status != OK) {
|
|
|
|
LOG(ERROR) << "Failed to unmount " << installerTarget;
|
|
|
|
// Intentional continue to try to unmount the other bind mount
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 14:43:20 +02:00
|
|
|
if (mAppDataIsolationEnabled) {
|
|
|
|
std::string obbTarget( StringPrintf("/mnt/androidwritable/%d/%s/%d/Android/obb",
|
|
|
|
userId, label.c_str(), userId));
|
|
|
|
LOG(INFO) << "Unmounting " << obbTarget;
|
|
|
|
auto status = UnmountTree(obbTarget);
|
|
|
|
if (status != OK) {
|
|
|
|
LOG(ERROR) << "Failed to unmount " << obbTarget;
|
|
|
|
// Intentional continue to try to unmount the other bind mount
|
|
|
|
}
|
|
|
|
std::string dataTarget(StringPrintf("/mnt/androidwritable/%d/%s/%d/Android/data",
|
|
|
|
userId, label.c_str(), userId));
|
|
|
|
LOG(INFO) << "Unmounting " << dataTarget;
|
|
|
|
status = UnmountTree(dataTarget);
|
|
|
|
if (status != OK) {
|
|
|
|
LOG(ERROR) << "Failed to unmount " << dataTarget;
|
|
|
|
// Intentional continue to try to unmount the other bind mount
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 15:31:24 +01:00
|
|
|
// When app data isolation is enabled, kill all apps that obb/ is mounted, otherwise we should
|
|
|
|
// umount the whole Android/ dir.
|
|
|
|
if (mAppDataIsolationEnabled) {
|
|
|
|
std::string appObbDir(StringPrintf("%s/%d/Android/obb", getPath().c_str(), userId));
|
|
|
|
KillProcessesWithMountPrefix(appObbDir);
|
|
|
|
} else {
|
2020-03-16 14:37:33 +01:00
|
|
|
std::string androidDataTarget(
|
|
|
|
StringPrintf("/mnt/user/%d/%s/%d/Android/data", userId, label.c_str(), userId));
|
|
|
|
|
|
|
|
LOG(INFO) << "Unmounting " << androidDataTarget;
|
|
|
|
auto status = UnmountTree(androidDataTarget);
|
|
|
|
if (status != OK) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Unmounted " << androidDataTarget;
|
|
|
|
|
|
|
|
std::string androidObbTarget(
|
|
|
|
StringPrintf("/mnt/user/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
|
2020-01-11 19:38:37 +01:00
|
|
|
|
2020-03-16 14:37:33 +01:00
|
|
|
LOG(INFO) << "Unmounting " << androidObbTarget;
|
|
|
|
status = UnmountTree(androidObbTarget);
|
2020-02-11 15:31:24 +01:00
|
|
|
if (status != OK) {
|
|
|
|
return status;
|
|
|
|
}
|
2020-03-16 14:37:33 +01:00
|
|
|
LOG(INFO) << "Unmounted " << androidObbTarget;
|
2019-11-28 11:56:13 +01:00
|
|
|
}
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-03-16 14:37:33 +01:00
|
|
|
status_t EmulatedVolume::unmountSdcardFs() {
|
|
|
|
if (!mUseSdcardFs || getMountUserId() != 0) {
|
|
|
|
// For sdcardfs, only unmount for user 0, since user 0 will always be running
|
|
|
|
// and the paths don't change for different users.
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ForceUnmount(mSdcardFsDefault);
|
|
|
|
ForceUnmount(mSdcardFsRead);
|
|
|
|
ForceUnmount(mSdcardFsWrite);
|
|
|
|
ForceUnmount(mSdcardFsFull);
|
|
|
|
|
|
|
|
rmdir(mSdcardFsDefault.c_str());
|
|
|
|
rmdir(mSdcardFsRead.c_str());
|
|
|
|
rmdir(mSdcardFsWrite.c_str());
|
|
|
|
rmdir(mSdcardFsFull.c_str());
|
|
|
|
|
|
|
|
mSdcardFsDefault.clear();
|
|
|
|
mSdcardFsRead.clear();
|
|
|
|
mSdcardFsWrite.clear();
|
|
|
|
mSdcardFsFull.clear();
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:53:53 +01:00
|
|
|
status_t EmulatedVolume::doMount() {
|
|
|
|
std::string label = getLabel();
|
|
|
|
bool isVisible = getMountFlags() & MountFlags::kVisible;
|
2015-07-07 23:37:03 +02:00
|
|
|
|
2019-12-09 14:18:01 +01:00
|
|
|
mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", label.c_str());
|
|
|
|
mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", label.c_str());
|
|
|
|
mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", label.c_str());
|
|
|
|
mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", label.c_str());
|
2015-06-24 20:49:24 +02:00
|
|
|
|
|
|
|
setInternalPath(mRawPath);
|
2015-07-07 23:37:03 +02:00
|
|
|
setPath(StringPrintf("/storage/%s", label.c_str()));
|
2015-06-24 20:49:24 +02:00
|
|
|
|
2019-12-09 14:18:01 +01:00
|
|
|
if (fs_prepare_dir(mSdcardFsDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
|
|
|
|
fs_prepare_dir(mSdcardFsRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
|
|
|
|
fs_prepare_dir(mSdcardFsWrite.c_str(), 0700, AID_ROOT, AID_ROOT) ||
|
|
|
|
fs_prepare_dir(mSdcardFsFull.c_str(), 0700, AID_ROOT, AID_ROOT)) {
|
2015-06-24 20:49:24 +02:00
|
|
|
PLOG(ERROR) << getId() << " failed to create mount points";
|
2015-03-03 06:01:40 +01:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2019-12-09 14:18:01 +01:00
|
|
|
dev_t before = GetDevice(mSdcardFsFull);
|
2015-03-14 00:09:20 +01:00
|
|
|
|
2019-12-13 11:26:32 +01:00
|
|
|
bool isFuse = base::GetBoolProperty(kPropFuse, false);
|
2019-07-19 17:46:53 +02:00
|
|
|
|
2019-11-28 11:53:53 +01:00
|
|
|
// Mount sdcardfs regardless of FUSE, since we need it to bind-mount on top of the
|
|
|
|
// FUSE volume for various reasons.
|
2020-01-06 09:48:14 +01:00
|
|
|
if (mUseSdcardFs && getMountUserId() == 0) {
|
2019-11-28 11:53:53 +01:00
|
|
|
LOG(INFO) << "Executing sdcardfs";
|
|
|
|
int sdcardFsPid;
|
|
|
|
if (!(sdcardFsPid = fork())) {
|
|
|
|
// clang-format off
|
|
|
|
if (execl(kSdcardFsPath, kSdcardFsPath,
|
|
|
|
"-u", "1023", // AID_MEDIA_RW
|
|
|
|
"-g", "1023", // AID_MEDIA_RW
|
|
|
|
"-m",
|
|
|
|
"-w",
|
|
|
|
"-G",
|
|
|
|
"-i",
|
|
|
|
"-o",
|
|
|
|
mRawPath.c_str(),
|
|
|
|
label.c_str(),
|
|
|
|
NULL)) {
|
|
|
|
// clang-format on
|
|
|
|
PLOG(ERROR) << "Failed to exec";
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(ERROR) << "sdcardfs exiting";
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sdcardFsPid == -1) {
|
|
|
|
PLOG(ERROR) << getId() << " failed to fork";
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
|
|
|
|
while (before == GetDevice(mSdcardFsFull)) {
|
|
|
|
LOG(DEBUG) << "Waiting for sdcardfs to spin up...";
|
|
|
|
usleep(50000); // 50ms
|
|
|
|
|
|
|
|
nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
|
|
|
|
if (nanoseconds_to_milliseconds(now - start) > 5000) {
|
|
|
|
LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up";
|
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* sdcardfs will have exited already. The filesystem will still be running */
|
|
|
|
TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0));
|
|
|
|
sdcardFsPid = 0;
|
|
|
|
}
|
2020-03-16 14:37:33 +01:00
|
|
|
|
2019-11-28 11:53:53 +01:00
|
|
|
if (isFuse && isVisible) {
|
2020-03-16 14:37:33 +01:00
|
|
|
// Make sure we unmount sdcardfs if we bail out with an error below
|
|
|
|
auto sdcardfs_unmounter = [&]() {
|
|
|
|
LOG(INFO) << "sdcardfs_unmounter scope_guard running";
|
|
|
|
unmountSdcardFs();
|
|
|
|
};
|
|
|
|
auto sdcardfs_guard = android::base::make_scope_guard(sdcardfs_unmounter);
|
|
|
|
|
2019-07-19 17:46:53 +02:00
|
|
|
LOG(INFO) << "Mounting emulated fuse volume";
|
2019-08-29 16:22:42 +02:00
|
|
|
android::base::unique_fd fd;
|
2019-09-09 11:24:44 +02:00
|
|
|
int user_id = getMountUserId();
|
2020-01-31 15:23:09 +01:00
|
|
|
auto volumeRoot = getRootPath();
|
2019-09-09 11:24:44 +02:00
|
|
|
|
2020-01-31 15:23:09 +01:00
|
|
|
// Make sure Android/ dirs exist for bind mounting
|
|
|
|
status_t res = PrepareAndroidDirs(volumeRoot);
|
|
|
|
if (res != OK) {
|
|
|
|
LOG(ERROR) << "Failed to prepare Android/ directories";
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = MountUserFuse(user_id, getInternalPath(), label, &fd);
|
|
|
|
if (res != 0) {
|
2019-07-19 17:46:53 +02:00
|
|
|
PLOG(ERROR) << "Failed to mount emulated fuse volume";
|
2020-01-31 15:23:09 +01:00
|
|
|
return res;
|
2019-07-19 17:46:53 +02:00
|
|
|
}
|
2019-11-19 10:16:03 +01:00
|
|
|
|
2019-11-28 11:53:53 +01:00
|
|
|
mFuseMounted = true;
|
2020-03-16 14:37:33 +01:00
|
|
|
auto fuse_unmounter = [&]() {
|
|
|
|
LOG(INFO) << "fuse_unmounter scope_guard running";
|
|
|
|
fd.reset();
|
|
|
|
if (UnmountUserFuse(user_id, getInternalPath(), label) != OK) {
|
|
|
|
PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume";
|
|
|
|
}
|
|
|
|
mFuseMounted = false;
|
|
|
|
};
|
|
|
|
auto fuse_guard = android::base::make_scope_guard(fuse_unmounter);
|
|
|
|
|
2019-11-19 10:16:03 +01:00
|
|
|
auto callback = getMountCallback();
|
|
|
|
if (callback) {
|
|
|
|
bool is_ready = false;
|
|
|
|
callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready);
|
|
|
|
if (!is_ready) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
2019-11-28 11:56:13 +01:00
|
|
|
|
|
|
|
// Only do the bind-mounts when we know for sure the FUSE daemon can resolve the path.
|
2020-01-31 15:23:09 +01:00
|
|
|
res = mountFuseBindMounts();
|
2020-01-15 16:00:07 +01:00
|
|
|
if (res != OK) {
|
2020-03-16 14:37:33 +01:00
|
|
|
return res;
|
2020-01-15 16:00:07 +01:00
|
|
|
}
|
2020-03-16 14:37:33 +01:00
|
|
|
|
|
|
|
// All mounts where successful, disable scope guards
|
|
|
|
sdcardfs_guard.Disable();
|
|
|
|
fuse_guard.Disable();
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t EmulatedVolume::doUnmount() {
|
2019-11-29 15:38:55 +01:00
|
|
|
int userId = getMountUserId();
|
|
|
|
|
|
|
|
// Kill all processes using the filesystem before we unmount it. If we
|
|
|
|
// unmount the filesystem first, most file system operations will return
|
2016-01-21 13:26:05 +01:00
|
|
|
// ENOTCONN until the unmount completes. This is an exotic and unusual
|
|
|
|
// error code and might cause broken behaviour in applications.
|
2019-11-29 15:38:55 +01:00
|
|
|
if (mFuseMounted) {
|
|
|
|
// For FUSE specifically, we have an emulated volume per user, so only kill
|
|
|
|
// processes using files from this particular user.
|
|
|
|
std::string user_path(StringPrintf("%s/%d", getPath().c_str(), getMountUserId()));
|
|
|
|
LOG(INFO) << "Killing all processes referencing " << user_path;
|
|
|
|
KillProcessesUsingPath(user_path);
|
|
|
|
} else {
|
|
|
|
KillProcessesUsingPath(getPath());
|
|
|
|
}
|
2019-07-19 17:46:53 +02:00
|
|
|
|
2019-11-28 11:53:53 +01:00
|
|
|
if (mFuseMounted) {
|
|
|
|
std::string label = getLabel();
|
2020-02-11 15:31:24 +01:00
|
|
|
|
2019-11-28 11:56:13 +01:00
|
|
|
// Ignoring unmount return status because we do want to try to unmount
|
|
|
|
// the rest cleanly.
|
2020-03-16 14:37:33 +01:00
|
|
|
unmountFuseBindMounts();
|
|
|
|
|
2019-11-28 11:56:13 +01:00
|
|
|
if (UnmountUserFuse(userId, getInternalPath(), label) != OK) {
|
2019-09-25 15:37:38 +02:00
|
|
|
PLOG(INFO) << "UnmountUserFuse failed on emulated fuse volume";
|
|
|
|
return -errno;
|
2019-07-19 17:46:53 +02:00
|
|
|
}
|
|
|
|
|
2019-11-28 11:53:53 +01:00
|
|
|
mFuseMounted = false;
|
|
|
|
}
|
2015-03-03 06:01:40 +01:00
|
|
|
|
2020-03-16 14:37:33 +01:00
|
|
|
return unmountSdcardFs();
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
2020-01-31 15:23:09 +01:00
|
|
|
std::string EmulatedVolume::getRootPath() const {
|
|
|
|
int user_id = getMountUserId();
|
|
|
|
std::string volumeRoot = StringPrintf("%s/%d", getInternalPath().c_str(), user_id);
|
|
|
|
|
|
|
|
return volumeRoot;
|
|
|
|
}
|
|
|
|
|
2015-03-03 06:01:40 +01:00
|
|
|
} // namespace vold
|
|
|
|
} // namespace android
|