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.
|
|
|
|
*/
|
|
|
|
|
2015-05-22 07:35:42 +02:00
|
|
|
#include "fs/Vfat.h"
|
2015-03-03 06:01:40 +01:00
|
|
|
#include "PublicVolume.h"
|
|
|
|
#include "Utils.h"
|
2015-03-14 00:09:20 +01:00
|
|
|
#include "VolumeManager.h"
|
|
|
|
#include "ResponseCode.h"
|
2015-03-03 06:01:40 +01:00
|
|
|
|
2015-12-05 00:50:53 +01:00
|
|
|
#include <android-base/stringprintf.h>
|
|
|
|
#include <android-base/logging.h>
|
2015-03-03 06:01:40 +01:00
|
|
|
#include <cutils/fs.h>
|
|
|
|
#include <private/android_filesystem_config.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#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 {
|
|
|
|
|
|
|
|
static const char* kFusePath = "/system/bin/sdcard";
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
static const char* kAsecPath = "/mnt/secure/asec";
|
2015-03-03 06:01:40 +01:00
|
|
|
|
|
|
|
PublicVolume::PublicVolume(dev_t device) :
|
2015-03-14 00:09:20 +01:00
|
|
|
VolumeBase(Type::kPublic), mDevice(device), mFusePid(0) {
|
|
|
|
setId(StringPrintf("public:%u,%u", major(device), minor(device)));
|
|
|
|
mDevPath = StringPrintf("/dev/block/vold/%s", getId().c_str());
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
PublicVolume::~PublicVolume() {
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t PublicVolume::readMetadata() {
|
2015-04-01 20:54:32 +02:00
|
|
|
status_t res = ReadMetadataUntrusted(mDevPath, mFsType, mFsUuid, mFsLabel);
|
2015-04-09 06:07:21 +02:00
|
|
|
notifyEvent(ResponseCode::VolumeFsTypeChanged, mFsType);
|
|
|
|
notifyEvent(ResponseCode::VolumeFsUuidChanged, mFsUuid);
|
|
|
|
notifyEvent(ResponseCode::VolumeFsLabelChanged, mFsLabel);
|
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
|
|
|
|
if (!access(legacyPath.c_str(), R_OK | X_OK)
|
|
|
|
&& access(securePath.c_str(), R_OK | X_OK)) {
|
|
|
|
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() {
|
2015-03-14 00:09:20 +01:00
|
|
|
// TODO: expand to support mounting other filesystems
|
2015-03-31 19:35:33 +02:00
|
|
|
readMetadata();
|
|
|
|
|
2015-06-24 22:30:45 +02:00
|
|
|
if (mFsType != "vfat") {
|
|
|
|
LOG(ERROR) << getId() << " unsupported filesystem " << mFsType;
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2015-05-22 07:35:42 +02:00
|
|
|
if (vfat::Check(mDevPath)) {
|
2015-03-31 19:35:33 +02:00
|
|
|
LOG(ERROR) << getId() << " failed filesystem check";
|
2015-03-03 06:01:40 +01:00
|
|
|
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
|
|
|
|
2015-08-06 20:40:00 +02:00
|
|
|
mFuseDefault = StringPrintf("/mnt/runtime/default/%s", stableName.c_str());
|
|
|
|
mFuseRead = StringPrintf("/mnt/runtime/read/%s", stableName.c_str());
|
|
|
|
mFuseWrite = StringPrintf("/mnt/runtime/write/%s", stableName.c_str());
|
2015-06-24 20:49:24 +02:00
|
|
|
|
2015-05-13 21:36:48 +02:00
|
|
|
setInternalPath(mRawPath);
|
2015-07-31 01:54:23 +02:00
|
|
|
if (getMountFlags() & MountFlags::kVisible) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-05-22 07:35:42 +02:00
|
|
|
if (vfat::Mount(mDevPath, mRawPath, false, false, false,
|
2015-03-03 06:01:40 +01:00
|
|
|
AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) {
|
2015-03-31 19:35:33 +02:00
|
|
|
PLOG(ERROR) << getId() << " failed to mount " << mDevPath;
|
2015-03-03 06:01:40 +01:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2015-04-18 02:35:20 +02:00
|
|
|
if (getMountFlags() & MountFlags::kPrimary) {
|
2015-03-14 00:09:20 +01:00
|
|
|
initAsecStage();
|
|
|
|
}
|
|
|
|
|
2015-07-01 00:54:17 +02:00
|
|
|
if (!(getMountFlags() & MountFlags::kVisible)) {
|
|
|
|
// Not visible to apps, so no need to spin up FUSE
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-12-15 08:20:41 +01:00
|
|
|
if (fs_prepare_dir(mFuseDefault.c_str(), 0700, AID_ROOT, AID_ROOT) ||
|
|
|
|
fs_prepare_dir(mFuseRead.c_str(), 0700, AID_ROOT, AID_ROOT) ||
|
|
|
|
fs_prepare_dir(mFuseWrite.c_str(), 0700, AID_ROOT, AID_ROOT)) {
|
|
|
|
PLOG(ERROR) << getId() << " failed to create FUSE mount points";
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2015-06-24 20:49:24 +02:00
|
|
|
dev_t before = GetDevice(mFuseWrite);
|
|
|
|
|
2015-03-03 06:01:40 +01:00
|
|
|
if (!(mFusePid = fork())) {
|
2015-07-01 00:54:17 +02:00
|
|
|
if (getMountFlags() & MountFlags::kPrimary) {
|
2015-03-14 00:09:20 +01:00
|
|
|
if (execl(kFusePath, kFusePath,
|
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)) {
|
|
|
|
PLOG(ERROR) << "Failed to exec";
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
} else {
|
2015-03-14 00:09:20 +01:00
|
|
|
if (execl(kFusePath, kFusePath,
|
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-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)) {
|
|
|
|
PLOG(ERROR) << "Failed to exec";
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-01 00:54:17 +02:00
|
|
|
LOG(ERROR) << "FUSE exiting";
|
2015-03-03 06:01:40 +01:00
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mFusePid == -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;
|
|
|
|
}
|
|
|
|
|
2015-06-24 20:49:24 +02:00
|
|
|
while (before == GetDevice(mFuseWrite)) {
|
|
|
|
LOG(VERBOSE) << "Waiting for FUSE to spin up...";
|
|
|
|
usleep(50000); // 50ms
|
|
|
|
}
|
|
|
|
|
2015-03-03 06:01:40 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t PublicVolume::doUnmount() {
|
|
|
|
if (mFusePid > 0) {
|
|
|
|
kill(mFusePid, SIGTERM);
|
|
|
|
TEMP_FAILURE_RETRY(waitpid(mFusePid, nullptr, 0));
|
|
|
|
mFusePid = 0;
|
|
|
|
}
|
|
|
|
|
2016-03-31 04:37:28 +02:00
|
|
|
KillProcessesUsingPath(getPath());
|
|
|
|
|
2015-04-13 06:50:32 +02:00
|
|
|
ForceUnmount(kAsecPath);
|
2015-06-24 20:49:24 +02:00
|
|
|
|
|
|
|
ForceUnmount(mFuseDefault);
|
|
|
|
ForceUnmount(mFuseRead);
|
|
|
|
ForceUnmount(mFuseWrite);
|
2015-03-03 06:01:40 +01:00
|
|
|
ForceUnmount(mRawPath);
|
|
|
|
|
2015-06-24 20:49:24 +02:00
|
|
|
rmdir(mFuseDefault.c_str());
|
|
|
|
rmdir(mFuseRead.c_str());
|
|
|
|
rmdir(mFuseWrite.c_str());
|
|
|
|
rmdir(mRawPath.c_str());
|
2015-03-14 00:09:20 +01:00
|
|
|
|
2015-06-24 20:49:24 +02:00
|
|
|
mFuseDefault.clear();
|
|
|
|
mFuseRead.clear();
|
|
|
|
mFuseWrite.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) {
|
|
|
|
if (fsType == "vfat" || fsType == "auto") {
|
|
|
|
if (WipeBlockDevice(mDevPath) != OK) {
|
|
|
|
LOG(WARNING) << getId() << " failed to wipe";
|
|
|
|
}
|
|
|
|
if (vfat::Format(mDevPath, 0)) {
|
|
|
|
LOG(ERROR) << getId() << " failed to format";
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Unsupported filesystem " << fsType;
|
|
|
|
return -EINVAL;
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
2015-05-22 07:35:42 +02:00
|
|
|
|
2015-03-03 06:01:40 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace vold
|
|
|
|
} // namespace android
|