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"
|
Support bind mounting volumes into other volume's mountpoint.
With the way the FUSE mount point are currently setup for emulated
volumes, there can be multiple paths that serve the same files on the
lower filesystem; eg
* /mnt/user/0/emulated/0/Android
* /mnt/user/10/emulated/0/Android
both refer to the same file on the lower filesystem:
* /data/media/0/Android
this is normally not a problem, because cross-user file access is not
allowed, and so the FUSE daemon won't serve files for other users.
With clone profiles this is no longer true however, as their volumes
are accessible by each other.
So, it can happen that an app running in clone profile 10 accesses
"/mnt/user/10/emulated/0/Android", which would be served by the FUSE
daemon for the user 10 filesystem.
At the same time, an app running in the owner profile 0 accesses
"mnt/user/0/emulated/0/Android", which would be served by the FUSE
daemon for the user 0 filesystem.
This can cause page cache inconsistencies, because multiple FUSE daemons
can be running on top of the same entries in the lower filesystem.
To prevent this, use bind mounts to make sure that cross-profile
accesses actually end up in the FUSE daemon to which the volume
belongs: "/mnt/user/10/emulated/0" is bind-mounted to
"/mnt/user/0/emulated/0", and vice-versa.
Bug: 228271997
Test: manual
Change-Id: Iefcbc813670628b329a1a5d408b6126b84991e09
2022-07-12 10:11:02 +02:00
|
|
|
#include "VolumeBase.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
|
|
|
|
Support bind mounting volumes into other volume's mountpoint.
With the way the FUSE mount point are currently setup for emulated
volumes, there can be multiple paths that serve the same files on the
lower filesystem; eg
* /mnt/user/0/emulated/0/Android
* /mnt/user/10/emulated/0/Android
both refer to the same file on the lower filesystem:
* /data/media/0/Android
this is normally not a problem, because cross-user file access is not
allowed, and so the FUSE daemon won't serve files for other users.
With clone profiles this is no longer true however, as their volumes
are accessible by each other.
So, it can happen that an app running in clone profile 10 accesses
"/mnt/user/10/emulated/0/Android", which would be served by the FUSE
daemon for the user 10 filesystem.
At the same time, an app running in the owner profile 0 accesses
"mnt/user/0/emulated/0/Android", which would be served by the FUSE
daemon for the user 0 filesystem.
This can cause page cache inconsistencies, because multiple FUSE daemons
can be running on top of the same entries in the lower filesystem.
To prevent this, use bind mounts to make sure that cross-profile
accesses actually end up in the FUSE daemon to which the volume
belongs: "/mnt/user/10/emulated/0" is bind-mounted to
"/mnt/user/0/emulated/0", and vice-versa.
Bug: 228271997
Test: manual
Change-Id: Iefcbc813670628b329a1a5d408b6126b84991e09
2022-07-12 10:11:02 +02:00
|
|
|
std::string EmulatedVolume::getLabel() const {
|
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;
|
|
|
|
}
|
|
|
|
|
Support bind mounting volumes into other volume's mountpoint.
With the way the FUSE mount point are currently setup for emulated
volumes, there can be multiple paths that serve the same files on the
lower filesystem; eg
* /mnt/user/0/emulated/0/Android
* /mnt/user/10/emulated/0/Android
both refer to the same file on the lower filesystem:
* /data/media/0/Android
this is normally not a problem, because cross-user file access is not
allowed, and so the FUSE daemon won't serve files for other users.
With clone profiles this is no longer true however, as their volumes
are accessible by each other.
So, it can happen that an app running in clone profile 10 accesses
"/mnt/user/10/emulated/0/Android", which would be served by the FUSE
daemon for the user 10 filesystem.
At the same time, an app running in the owner profile 0 accesses
"mnt/user/0/emulated/0/Android", which would be served by the FUSE
daemon for the user 0 filesystem.
This can cause page cache inconsistencies, because multiple FUSE daemons
can be running on top of the same entries in the lower filesystem.
To prevent this, use bind mounts to make sure that cross-profile
accesses actually end up in the FUSE daemon to which the volume
belongs: "/mnt/user/10/emulated/0" is bind-mounted to
"/mnt/user/0/emulated/0", and vice-versa.
Bug: 228271997
Test: manual
Change-Id: Iefcbc813670628b329a1a5d408b6126b84991e09
2022-07-12 10:11:02 +02:00
|
|
|
// Bind mounts the volume 'volume' onto this volume.
|
|
|
|
status_t EmulatedVolume::bindMountVolume(const EmulatedVolume& volume,
|
|
|
|
std::list<std::string>& pathsToUnmount) {
|
|
|
|
int myUserId = getMountUserId();
|
|
|
|
int volumeUserId = volume.getMountUserId();
|
|
|
|
std::string label = volume.getLabel();
|
|
|
|
|
|
|
|
// eg /mnt/user/10/emulated/10
|
|
|
|
std::string srcUserPath = GetFuseMountPathForUser(volumeUserId, label);
|
|
|
|
std::string srcPath = StringPrintf("%s/%d", srcUserPath.c_str(), volumeUserId);
|
|
|
|
// eg /mnt/user/0/emulated/10
|
|
|
|
std::string dstUserPath = GetFuseMountPathForUser(myUserId, label);
|
|
|
|
std::string dstPath = StringPrintf("%s/%d", dstUserPath.c_str(), volumeUserId);
|
|
|
|
|
|
|
|
auto status = doFuseBindMount(srcPath, dstPath, pathsToUnmount);
|
|
|
|
if (status == OK) {
|
|
|
|
// Store the mount path, so we can unmount it when this volume goes away
|
|
|
|
mSharedStorageMountPath = dstPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
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;
|
2021-03-19 16:35:49 +01:00
|
|
|
// Zygote will unmount these dirs if app data isolation is enabled, so apps
|
|
|
|
// cannot access these dirs directly.
|
|
|
|
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
|
|
|
|
2021-03-19 16:35:49 +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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Support bind mounting volumes into other volume's mountpoint.
With the way the FUSE mount point are currently setup for emulated
volumes, there can be multiple paths that serve the same files on the
lower filesystem; eg
* /mnt/user/0/emulated/0/Android
* /mnt/user/10/emulated/0/Android
both refer to the same file on the lower filesystem:
* /data/media/0/Android
this is normally not a problem, because cross-user file access is not
allowed, and so the FUSE daemon won't serve files for other users.
With clone profiles this is no longer true however, as their volumes
are accessible by each other.
So, it can happen that an app running in clone profile 10 accesses
"/mnt/user/10/emulated/0/Android", which would be served by the FUSE
daemon for the user 10 filesystem.
At the same time, an app running in the owner profile 0 accesses
"mnt/user/0/emulated/0/Android", which would be served by the FUSE
daemon for the user 0 filesystem.
This can cause page cache inconsistencies, because multiple FUSE daemons
can be running on top of the same entries in the lower filesystem.
To prevent this, use bind mounts to make sure that cross-profile
accesses actually end up in the FUSE daemon to which the volume
belongs: "/mnt/user/10/emulated/0" is bind-mounted to
"/mnt/user/0/emulated/0", and vice-versa.
Bug: 228271997
Test: manual
Change-Id: Iefcbc813670628b329a1a5d408b6126b84991e09
2022-07-12 10:11:02 +02:00
|
|
|
// For users that share their volume with another user (eg a clone
|
|
|
|
// profile), the current mount setup can cause page cache inconsistency
|
|
|
|
// issues. Let's say this is user 10, and the user it shares storage with
|
|
|
|
// is user 0.
|
|
|
|
// Then:
|
|
|
|
// * The FUSE daemon for user 0 serves /mnt/user/0
|
|
|
|
// * The FUSE daemon for user 10 serves /mnt/user/10
|
|
|
|
// The emulated volume for user 10 would be located at two paths:
|
|
|
|
// /mnt/user/0/emulated/10
|
|
|
|
// /mnt/user/10/emulated/10
|
|
|
|
// Since these paths refer to the same files but are served by different FUSE
|
|
|
|
// daemons, this can result in page cache inconsistency issues. To prevent this,
|
|
|
|
// bind mount the relevant paths for the involved users:
|
|
|
|
// 1. /mnt/user/10/emulated/10 =B=> /mnt/user/0/emulated/10
|
|
|
|
// 2. /mnt/user/0/emulated/0 =B=> /mnt/user/10/emulated/0
|
|
|
|
//
|
|
|
|
// This will ensure that any access to the volume for a specific user always
|
|
|
|
// goes through a single FUSE daemon.
|
|
|
|
userid_t sharedStorageUserId = VolumeManager::Instance()->getSharedStorageUser(userId);
|
|
|
|
if (sharedStorageUserId != USER_UNKNOWN) {
|
|
|
|
auto filter_fn = [&](const VolumeBase& vol) {
|
|
|
|
if (vol.getState() != VolumeBase::State::kMounted) {
|
|
|
|
// The volume must be mounted
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (vol.getType() != VolumeBase::Type::kEmulated) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (vol.getMountUserId() != sharedStorageUserId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((vol.getMountFlags() & MountFlags::kPrimary) == 0) {
|
|
|
|
// We only care about the primary emulated volume, so not a private
|
|
|
|
// volume with an emulated volume stacked on top.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
auto vol = VolumeManager::Instance()->findVolumeWithFilter(filter_fn);
|
|
|
|
if (vol != nullptr) {
|
|
|
|
auto sharedVol = static_cast<EmulatedVolume*>(vol.get());
|
|
|
|
// Bind mount this volume in the other user's primary volume
|
|
|
|
status = sharedVol->bindMountVolume(*this, pathsToUnmount);
|
|
|
|
if (status != OK) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
// And vice-versa
|
|
|
|
status = bindMountVolume(*sharedVol, pathsToUnmount);
|
|
|
|
if (status != OK) {
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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();
|
|
|
|
|
Support bind mounting volumes into other volume's mountpoint.
With the way the FUSE mount point are currently setup for emulated
volumes, there can be multiple paths that serve the same files on the
lower filesystem; eg
* /mnt/user/0/emulated/0/Android
* /mnt/user/10/emulated/0/Android
both refer to the same file on the lower filesystem:
* /data/media/0/Android
this is normally not a problem, because cross-user file access is not
allowed, and so the FUSE daemon won't serve files for other users.
With clone profiles this is no longer true however, as their volumes
are accessible by each other.
So, it can happen that an app running in clone profile 10 accesses
"/mnt/user/10/emulated/0/Android", which would be served by the FUSE
daemon for the user 10 filesystem.
At the same time, an app running in the owner profile 0 accesses
"mnt/user/0/emulated/0/Android", which would be served by the FUSE
daemon for the user 0 filesystem.
This can cause page cache inconsistencies, because multiple FUSE daemons
can be running on top of the same entries in the lower filesystem.
To prevent this, use bind mounts to make sure that cross-profile
accesses actually end up in the FUSE daemon to which the volume
belongs: "/mnt/user/10/emulated/0" is bind-mounted to
"/mnt/user/0/emulated/0", and vice-versa.
Bug: 228271997
Test: manual
Change-Id: Iefcbc813670628b329a1a5d408b6126b84991e09
2022-07-12 10:11:02 +02:00
|
|
|
if (!mSharedStorageMountPath.empty()) {
|
|
|
|
LOG(INFO) << "Unmounting " << mSharedStorageMountPath;
|
|
|
|
auto status = UnmountTree(mSharedStorageMountPath);
|
|
|
|
if (status != OK) {
|
|
|
|
LOG(ERROR) << "Failed to unmount " << mSharedStorageMountPath;
|
|
|
|
}
|
|
|
|
mSharedStorageMountPath = "";
|
|
|
|
}
|
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));
|
2021-04-30 10:53:07 +02:00
|
|
|
// Here we assume obb/data dirs is mounted as tmpfs, then it must be caused by
|
|
|
|
// app data isolation.
|
|
|
|
KillProcessesWithTmpfsMountPrefix(appObbDir);
|
2021-05-05 16:43:45 +02:00
|
|
|
}
|
2020-03-16 14:37:33 +01:00
|
|
|
|
2021-05-05 16:43:45 +02:00
|
|
|
// Always unmount data and obb dirs as they are mounted to lowerfs for speeding up access.
|
|
|
|
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;
|
2020-03-16 14:37:33 +01:00
|
|
|
|
2021-05-05 16:43:45 +02:00
|
|
|
std::string androidObbTarget(
|
|
|
|
StringPrintf("/mnt/user/%d/%s/%d/Android/obb", userId, label.c_str(), userId));
|
2020-01-11 19:38:37 +01:00
|
|
|
|
2021-05-05 16:43:45 +02:00
|
|
|
LOG(INFO) << "Unmounting " << androidObbTarget;
|
|
|
|
status = UnmountTree(androidObbTarget);
|
|
|
|
if (status != OK) {
|
|
|
|
return status;
|
2019-11-28 11:56:13 +01:00
|
|
|
}
|
2021-05-05 16:43:45 +02: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();
|
2021-11-11 03:23:01 +01:00
|
|
|
bool isVisible = isVisibleForWrite();
|
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-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
|
|
|
|
2021-01-14 16:51:54 +01:00
|
|
|
if (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
|
|
|
|
2022-12-14 23:35:05 +01:00
|
|
|
if (!IsFuseBpfEnabled()) {
|
2021-10-13 13:03:18 +02:00
|
|
|
// Only do the bind-mounts when we know for sure the FUSE daemon can resolve the path.
|
|
|
|
res = mountFuseBindMounts();
|
|
|
|
if (res != OK) {
|
|
|
|
return res;
|
|
|
|
}
|
2020-01-15 16:00:07 +01:00
|
|
|
}
|
2020-03-16 14:37:33 +01:00
|
|
|
|
2020-06-12 13:59:45 +02:00
|
|
|
ConfigureReadAheadForFuse(GetFuseMountPathForUser(user_id, label), 256u);
|
|
|
|
|
2020-06-29 11:53:34 +02:00
|
|
|
// By default, FUSE has a max_dirty ratio of 1%. This means that out of
|
|
|
|
// all dirty pages in the system, only 1% is allowed to belong to any
|
|
|
|
// FUSE filesystem. The reason this is in place is that FUSE
|
|
|
|
// filesystems shouldn't be trusted by default; a FUSE filesystem could
|
|
|
|
// take up say 100% of dirty pages, and subsequently refuse to write
|
|
|
|
// them back to storage. The kernel will then apply rate-limiting, and
|
|
|
|
// block other tasks from writing. For this particular FUSE filesystem
|
|
|
|
// however, we trust the implementation, because it is a part of the
|
|
|
|
// Android platform. So use the default ratio of 100%.
|
|
|
|
//
|
|
|
|
// The reason we're setting this is that there's a suspicion that the
|
|
|
|
// kernel starts rate-limiting the FUSE filesystem under extreme
|
|
|
|
// memory pressure scenarios. While the kernel will only rate limit if
|
|
|
|
// the writeback can't keep up with the write rate, under extreme
|
|
|
|
// memory pressure the write rate may dip as well, in which case FUSE
|
|
|
|
// writes to a 1% max_ratio filesystem are throttled to an extreme amount.
|
|
|
|
//
|
|
|
|
// To prevent this, just give FUSE 40% max_ratio, meaning it can take
|
|
|
|
// up to 40% of all dirty pages in the system.
|
|
|
|
ConfigureMaxDirtyRatioForFuse(GetFuseMountPathForUser(user_id, label), 40u);
|
|
|
|
|
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
|
|
|
|
2022-12-14 23:35:05 +01:00
|
|
|
if (!IsFuseBpfEnabled()) {
|
2021-10-13 13:03:18 +02:00
|
|
|
// Ignoring unmount return status because we do want to try to
|
|
|
|
// unmount the rest cleanly.
|
|
|
|
unmountFuseBindMounts();
|
|
|
|
}
|
2020-03-16 14:37:33 +01:00
|
|
|
|
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
|