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 "PublicVolume.h"
|
2019-07-19 17:46:53 +02:00
|
|
|
|
|
|
|
#include "AppFuseUtil.h"
|
2015-03-03 06:01:40 +01:00
|
|
|
#include "Utils.h"
|
2015-03-14 00:09:20 +01:00
|
|
|
#include "VolumeManager.h"
|
2018-01-19 02:55:18 +01:00
|
|
|
#include "fs/Exfat.h"
|
|
|
|
#include "fs/Vfat.h"
|
2015-03-03 06:01:40 +01:00
|
|
|
|
2015-12-05 00:50:53 +01:00
|
|
|
#include <android-base/logging.h>
|
2018-09-18 22:07:45 +02:00
|
|
|
#include <android-base/properties.h>
|
2018-09-19 00:14:18 +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>
|
|
|
|
|
2018-09-18 22:07:45 +02:00
|
|
|
using android::base::GetBoolProperty;
|
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
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
static const char* kAsecPath = "/mnt/secure/asec";
|
2015-03-03 06:01:40 +01:00
|
|
|
|
2019-12-09 14:18:01 +01:00
|
|
|
PublicVolume::PublicVolume(dev_t device) : VolumeBase(Type::kPublic), mDevice(device) {
|
2015-03-14 00:09:20 +01:00
|
|
|
setId(StringPrintf("public:%u,%u", major(device), minor(device)));
|
|
|
|
mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
|
2019-12-11 14:57:59 +01:00
|
|
|
mFuseMounted = false;
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
2018-09-19 00:14:18 +02:00
|
|
|
PublicVolume::~PublicVolume() {}
|
2015-03-03 06:01:40 +01:00
|
|
|
|
|
|
|
status_t PublicVolume::readMetadata() {
|
2017-10-07 02:02:53 +02:00
|
|
|
status_t res = ReadMetadataUntrusted(mDevPath, &mFsType, &mFsUuid, &mFsLabel);
|
2017-09-16 00:50:28 +02:00
|
|
|
|
2017-09-13 19:49:44 +02:00
|
|
|
auto listener = getListener();
|
|
|
|
if (listener) listener->onVolumeMetadataChanged(getId(), mFsType, mFsUuid, mFsLabel);
|
2017-09-16 00:50:28 +02:00
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
return res;
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t PublicVolume::initAsecStage() {
|
|
|
|
std::string legacyPath(mRawPath + "/android_secure");
|
|
|
|
std::string securePath(mRawPath + "/.android_secure");
|
|
|
|
|
|
|
|
// Recover legacy secure path
|
2018-09-19 00:14:18 +02:00
|
|
|
if (!access(legacyPath.c_str(), R_OK | X_OK) && access(securePath.c_str(), R_OK | X_OK)) {
|
2015-03-03 06:01:40 +01:00
|
|
|
if (rename(legacyPath.c_str(), securePath.c_str())) {
|
2015-03-31 19:35:33 +02:00
|
|
|
PLOG(WARNING) << getId() << " failed to rename legacy ASEC dir";
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
if (TEMP_FAILURE_RETRY(mkdir(securePath.c_str(), 0700))) {
|
|
|
|
if (errno != EEXIST) {
|
2015-03-31 19:35:33 +02:00
|
|
|
PLOG(WARNING) << getId() << " creating ASEC stage failed";
|
2015-03-14 00:09:20 +01:00
|
|
|
return -errno;
|
|
|
|
}
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
BindMount(securePath, kAsecPath);
|
|
|
|
|
2015-03-03 06:01:40 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-03-31 19:35:33 +02:00
|
|
|
status_t PublicVolume::doCreate() {
|
|
|
|
return CreateDeviceNode(mDevPath, mDevice);
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t PublicVolume::doDestroy() {
|
|
|
|
return DestroyDeviceNode(mDevPath);
|
|
|
|
}
|
|
|
|
|
2015-03-03 06:01:40 +01:00
|
|
|
status_t PublicVolume::doMount() {
|
2019-11-28 11:53:53 +01:00
|
|
|
bool isVisible = getMountFlags() & MountFlags::kVisible;
|
2015-03-31 19:35:33 +02:00
|
|
|
readMetadata();
|
|
|
|
|
2018-01-19 02:55:18 +01:00
|
|
|
if (mFsType == "vfat" && vfat::IsSupported()) {
|
|
|
|
if (vfat::Check(mDevPath)) {
|
|
|
|
LOG(ERROR) << getId() << " failed filesystem check";
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
} else if (mFsType == "exfat" && exfat::IsSupported()) {
|
|
|
|
if (exfat::Check(mDevPath)) {
|
|
|
|
LOG(ERROR) << getId() << " failed filesystem check";
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
} else {
|
2015-06-24 22:30:45 +02:00
|
|
|
LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
// Use UUID as stable name, if available
|
|
|
|
std::string stableName = getId();
|
|
|
|
if (!mFsUuid.empty()) {
|
2015-04-06 23:08:45 +02:00
|
|
|
stableName = mFsUuid;
|
2015-03-14 00:09:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
mRawPath = StringPrintf("/mnt/media_rw/%s", stableName.c_str());
|
2015-06-24 20:49:24 +02:00
|
|
|
|
2019-12-09 14:18:01 +01:00
|
|
|
mSdcardFsDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
|
|
|
|
mSdcardFsRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
|
|
|
|
mSdcardFsWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
|
|
|
|
mSdcardFsFull = StringPrintf("/mnt/runtime/full/%s", stableName.c_str());
|
2015-06-24 20:49:24 +02:00
|
|
|
|
2015-05-13 21:36:48 +02:00
|
|
|
setInternalPath(mRawPath);
|
2019-11-28 11:53:53 +01:00
|
|
|
if (isVisible) {
|
2015-07-31 01:54:23 +02:00
|
|
|
setPath(StringPrintf("/storage/%s", stableName.c_str()));
|
|
|
|
} else {
|
|
|
|
setPath(mRawPath);
|
|
|
|
}
|
2015-03-14 00:09:20 +01:00
|
|
|
|
2015-12-15 08:20:41 +01:00
|
|
|
if (fs_prepare_dir(mRawPath.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;
|
|
|
|
}
|
|
|
|
|
2018-01-19 02:55:18 +01:00
|
|
|
if (mFsType == "vfat") {
|
|
|
|
if (vfat::Mount(mDevPath, mRawPath, false, false, false, AID_MEDIA_RW, AID_MEDIA_RW, 0007,
|
|
|
|
true)) {
|
|
|
|
PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
} else if (mFsType == "exfat") {
|
|
|
|
if (exfat::Mount(mDevPath, mRawPath, AID_MEDIA_RW, AID_MEDIA_RW, 0007)) {
|
|
|
|
PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
|
|
|
|
return -EIO;
|
|
|
|
}
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
2015-04-18 02:35:20 +02:00
|
|
|
if (getMountFlags() & MountFlags::kPrimary) {
|
2015-03-14 00:09:20 +01:00
|
|
|
initAsecStage();
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:53:53 +01:00
|
|
|
if (!isVisible) {
|
2019-12-09 14:18:01 +01:00
|
|
|
// Not visible to apps, so no need to spin up sdcardfs or FUSE
|
2015-07-01 00:54:17 +02:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
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)) {
|
|
|
|
PLOG(ERROR) << getId() << " failed to create sdcardfs mount points";
|
2015-12-15 08:20:41 +01:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2019-12-09 14:18:01 +01:00
|
|
|
dev_t before = GetDevice(mSdcardFsFull);
|
2015-06-24 20:49:24 +02:00
|
|
|
|
2019-12-09 14:18:01 +01:00
|
|
|
int sdcardFsPid;
|
|
|
|
if (!(sdcardFsPid = fork())) {
|
2015-07-01 00:54:17 +02:00
|
|
|
if (getMountFlags() & MountFlags::kPrimary) {
|
2018-09-19 00:14:18 +02:00
|
|
|
// clang-format off
|
2019-12-09 14:18:01 +01:00
|
|
|
if (execl(kSdcardFsPath, kSdcardFsPath,
|
2015-03-03 06:01:40 +01:00
|
|
|
"-u", "1023", // AID_MEDIA_RW
|
|
|
|
"-g", "1023", // AID_MEDIA_RW
|
2015-07-28 19:57:29 +02:00
|
|
|
"-U", std::to_string(getMountUserId()).c_str(),
|
2015-06-24 20:49:24 +02:00
|
|
|
"-w",
|
2015-03-03 06:01:40 +01:00
|
|
|
mRawPath.c_str(),
|
2015-06-24 20:49:24 +02:00
|
|
|
stableName.c_str(),
|
2015-03-14 00:09:20 +01:00
|
|
|
NULL)) {
|
2018-09-19 00:14:18 +02:00
|
|
|
// clang-format on
|
2015-03-14 00:09:20 +01:00
|
|
|
PLOG(ERROR) << "Failed to exec";
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-09-19 00:14:18 +02:00
|
|
|
// clang-format off
|
2019-12-09 14:18:01 +01:00
|
|
|
if (execl(kSdcardFsPath, kSdcardFsPath,
|
2019-01-09 21:15:15 +01:00
|
|
|
"-u", "1023", // AID_MEDIA_RW
|
|
|
|
"-g", "1023", // AID_MEDIA_RW
|
|
|
|
"-U", std::to_string(getMountUserId()).c_str(),
|
|
|
|
mRawPath.c_str(),
|
|
|
|
stableName.c_str(),
|
|
|
|
NULL)) {
|
|
|
|
// clang-format on
|
|
|
|
PLOG(ERROR) << "Failed to exec";
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 14:18:01 +01:00
|
|
|
LOG(ERROR) << "sdcardfs exiting";
|
2015-03-03 06:01:40 +01:00
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
2019-12-09 14:18:01 +01:00
|
|
|
if (sdcardFsPid == -1) {
|
2015-03-31 19:35:33 +02:00
|
|
|
PLOG(ERROR) << getId() << " failed to fork";
|
2015-03-03 06:01:40 +01:00
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2017-09-18 22:47:10 +02:00
|
|
|
nsecs_t start = systemTime(SYSTEM_TIME_BOOTTIME);
|
2019-12-09 14:18:01 +01:00
|
|
|
while (before == GetDevice(mSdcardFsFull)) {
|
|
|
|
LOG(DEBUG) << "Waiting for sdcardfs to spin up...";
|
2018-09-19 00:14:18 +02:00
|
|
|
usleep(50000); // 50ms
|
2017-09-18 22:47:10 +02:00
|
|
|
|
|
|
|
nsecs_t now = systemTime(SYSTEM_TIME_BOOTTIME);
|
|
|
|
if (nanoseconds_to_milliseconds(now - start) > 5000) {
|
2019-12-09 14:18:01 +01:00
|
|
|
LOG(WARNING) << "Timed out while waiting for sdcardfs to spin up";
|
2017-09-18 22:47:10 +02:00
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
2015-06-24 20:49:24 +02:00
|
|
|
}
|
2019-12-09 14:18:01 +01:00
|
|
|
/* sdcardfs will have exited already. The filesystem will still be running */
|
|
|
|
TEMP_FAILURE_RETRY(waitpid(sdcardFsPid, nullptr, 0));
|
2015-06-24 20:49:24 +02:00
|
|
|
|
2019-11-28 11:53:53 +01:00
|
|
|
bool isFuse = base::GetBoolProperty(kPropFuseSnapshot, false);
|
|
|
|
if (isFuse) {
|
|
|
|
// We need to mount FUSE *after* sdcardfs, since the FUSE daemon may depend
|
|
|
|
// on sdcardfs being up.
|
|
|
|
LOG(INFO) << "Mounting public fuse volume";
|
|
|
|
android::base::unique_fd fd;
|
|
|
|
int user_id = getMountUserId();
|
|
|
|
int result = MountUserFuse(user_id, getInternalPath(), stableName, &fd);
|
|
|
|
|
|
|
|
if (result != 0) {
|
|
|
|
LOG(ERROR) << "Failed to mount public fuse volume";
|
|
|
|
return -result;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFuseMounted = true;
|
|
|
|
auto callback = getMountCallback();
|
|
|
|
if (callback) {
|
|
|
|
bool is_ready = false;
|
|
|
|
callback->onVolumeChecking(std::move(fd), getPath(), getInternalPath(), &is_ready);
|
|
|
|
if (!is_ready) {
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-03 06:01:40 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t PublicVolume::doUnmount() {
|
2016-04-18 23:23:29 +02:00
|
|
|
// Unmount the storage before we kill the FUSE process. If we kill
|
|
|
|
// the FUSE process first, most file system operations will return
|
|
|
|
// ENOTCONN until the unmount completes. This is an exotic and unusual
|
|
|
|
// error code and might cause broken behaviour in applications.
|
2016-03-31 04:37:28 +02:00
|
|
|
KillProcessesUsingPath(getPath());
|
|
|
|
|
2019-11-28 11:53:53 +01:00
|
|
|
if (mFuseMounted) {
|
2019-07-19 17:46:53 +02:00
|
|
|
// Use UUID as stable name, if available
|
|
|
|
std::string stableName = getId();
|
|
|
|
if (!mFsUuid.empty()) {
|
|
|
|
stableName = mFsUuid;
|
|
|
|
}
|
|
|
|
|
2019-11-28 11:53:53 +01:00
|
|
|
if (UnmountUserFuse(getMountUserId(), getInternalPath(), stableName) != OK) {
|
2019-09-25 15:37:38 +02:00
|
|
|
PLOG(INFO) << "UnmountUserFuse failed on public fuse volume";
|
|
|
|
return -errno;
|
2019-07-19 17:46:53 +02:00
|
|
|
}
|
2019-09-25 15:37:38 +02:00
|
|
|
|
2019-11-28 11:53:53 +01:00
|
|
|
mFuseMounted = false;
|
2019-07-19 17:46:53 +02:00
|
|
|
}
|
|
|
|
|
2015-04-13 06:50:32 +02:00
|
|
|
ForceUnmount(kAsecPath);
|
2015-06-24 20:49:24 +02:00
|
|
|
|
2019-12-09 14:18:01 +01:00
|
|
|
ForceUnmount(mSdcardFsDefault);
|
|
|
|
ForceUnmount(mSdcardFsRead);
|
|
|
|
ForceUnmount(mSdcardFsWrite);
|
|
|
|
ForceUnmount(mSdcardFsFull);
|
2015-03-03 06:01:40 +01:00
|
|
|
ForceUnmount(mRawPath);
|
|
|
|
|
2019-12-09 14:18:01 +01:00
|
|
|
rmdir(mSdcardFsDefault.c_str());
|
|
|
|
rmdir(mSdcardFsRead.c_str());
|
|
|
|
rmdir(mSdcardFsWrite.c_str());
|
|
|
|
rmdir(mSdcardFsFull.c_str());
|
2015-06-24 20:49:24 +02:00
|
|
|
rmdir(mRawPath.c_str());
|
2015-03-14 00:09:20 +01:00
|
|
|
|
2019-12-09 14:18:01 +01:00
|
|
|
mSdcardFsDefault.clear();
|
|
|
|
mSdcardFsRead.clear();
|
|
|
|
mSdcardFsWrite.clear();
|
|
|
|
mSdcardFsFull.clear();
|
2015-03-14 00:09:20 +01:00
|
|
|
mRawPath.clear();
|
2015-03-03 06:01:40 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-05-22 07:35:42 +02:00
|
|
|
status_t PublicVolume::doFormat(const std::string& fsType) {
|
2018-05-23 18:59:48 +02:00
|
|
|
bool useVfat = vfat::IsSupported();
|
|
|
|
bool useExfat = exfat::IsSupported();
|
|
|
|
status_t res = OK;
|
|
|
|
|
|
|
|
// Resolve the target filesystem type
|
|
|
|
if (fsType == "auto" && useVfat && useExfat) {
|
|
|
|
uint64_t size = 0;
|
|
|
|
|
|
|
|
res = GetBlockDevSize(mDevPath, &size);
|
|
|
|
if (res != OK) {
|
|
|
|
LOG(ERROR) << "Couldn't get device size " << mDevPath;
|
|
|
|
return res;
|
2015-05-22 07:35:42 +02:00
|
|
|
}
|
2018-05-23 18:59:48 +02:00
|
|
|
|
|
|
|
// If both vfat & exfat are supported use exfat for SDXC (>~32GiB) cards
|
|
|
|
if (size > 32896LL * 1024 * 1024) {
|
|
|
|
useVfat = false;
|
|
|
|
} else {
|
|
|
|
useExfat = false;
|
2018-01-19 02:55:18 +01:00
|
|
|
}
|
2018-05-23 18:59:48 +02:00
|
|
|
} else if (fsType == "vfat") {
|
|
|
|
useExfat = false;
|
|
|
|
} else if (fsType == "exfat") {
|
|
|
|
useVfat = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!useVfat && !useExfat) {
|
2015-05-22 07:35:42 +02:00
|
|
|
LOG(ERROR) << "Unsupported filesystem " << fsType;
|
|
|
|
return -EINVAL;
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
2015-05-22 07:35:42 +02:00
|
|
|
|
2018-05-23 18:59:48 +02:00
|
|
|
if (WipeBlockDevice(mDevPath) != OK) {
|
|
|
|
LOG(WARNING) << getId() << " failed to wipe";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (useVfat) {
|
|
|
|
res = vfat::Format(mDevPath, 0);
|
|
|
|
} else if (useExfat) {
|
|
|
|
res = exfat::Format(mDevPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res != OK) {
|
|
|
|
LOG(ERROR) << getId() << " failed to format";
|
|
|
|
res = -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace vold
|
|
|
|
} // namespace android
|