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 "VolumeBase.h"
|
2018-09-19 00:14:18 +02:00
|
|
|
#include "Utils.h"
|
2015-03-14 00:09:20 +01: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>
|
2018-09-19 00:14:18 +02:00
|
|
|
#include <android-base/stringprintf.h>
|
2015-03-03 06:01:40 +01:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
using android::base::StringPrintf;
|
|
|
|
|
2015-03-03 06:01:40 +01:00
|
|
|
namespace android {
|
|
|
|
namespace vold {
|
|
|
|
|
2018-09-19 00:14:18 +02:00
|
|
|
VolumeBase::VolumeBase(Type type)
|
|
|
|
: mType(type),
|
|
|
|
mMountFlags(0),
|
2018-09-18 22:07:45 +02:00
|
|
|
mMountUserId(USER_UNKNOWN),
|
2018-09-19 00:14:18 +02:00
|
|
|
mCreated(false),
|
|
|
|
mState(State::kUnmounted),
|
|
|
|
mSilent(false) {}
|
2015-03-03 06:01:40 +01:00
|
|
|
|
|
|
|
VolumeBase::~VolumeBase() {
|
2015-03-14 00:09:20 +01:00
|
|
|
CHECK(!mCreated);
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
void VolumeBase::setState(State state) {
|
2015-03-03 06:01:40 +01:00
|
|
|
mState = state;
|
2017-09-16 00:50:28 +02:00
|
|
|
|
2017-09-13 19:49:44 +02:00
|
|
|
auto listener = getListener();
|
2018-09-19 00:14:18 +02:00
|
|
|
if (listener) {
|
|
|
|
listener->onVolumeStateChanged(getId(), static_cast<int32_t>(mState));
|
|
|
|
}
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
2015-04-18 02:35:20 +02:00
|
|
|
status_t VolumeBase::setDiskId(const std::string& diskId) {
|
|
|
|
if (mCreated) {
|
|
|
|
LOG(WARNING) << getId() << " diskId change requires destroyed";
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDiskId = diskId;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-06-18 23:25:08 +02:00
|
|
|
status_t VolumeBase::setPartGuid(const std::string& partGuid) {
|
|
|
|
if (mCreated) {
|
|
|
|
LOG(WARNING) << getId() << " partGuid change requires destroyed";
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPartGuid = partGuid;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-04-18 02:35:20 +02:00
|
|
|
status_t VolumeBase::setMountFlags(int mountFlags) {
|
|
|
|
if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
|
|
|
|
LOG(WARNING) << getId() << " flags change requires state unmounted or unmountable";
|
2015-03-14 00:09:20 +01:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2015-04-18 02:35:20 +02:00
|
|
|
mMountFlags = mountFlags;
|
2015-03-14 00:09:20 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-04-18 02:35:20 +02:00
|
|
|
status_t VolumeBase::setMountUserId(userid_t mountUserId) {
|
|
|
|
if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
|
|
|
|
LOG(WARNING) << getId() << " user change requires state unmounted or unmountable";
|
2015-03-14 00:09:20 +01:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2015-04-18 02:35:20 +02:00
|
|
|
mMountUserId = mountUserId;
|
2015-03-14 00:09:20 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-04-09 06:07:21 +02:00
|
|
|
status_t VolumeBase::setSilent(bool silent) {
|
|
|
|
if (mCreated) {
|
|
|
|
LOG(WARNING) << getId() << " silence change requires destroyed";
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSilent = silent;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
status_t VolumeBase::setId(const std::string& id) {
|
|
|
|
if (mCreated) {
|
|
|
|
LOG(WARNING) << getId() << " id change requires not created";
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
mId = id;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t VolumeBase::setPath(const std::string& path) {
|
2015-04-18 02:35:20 +02:00
|
|
|
if (mState != State::kChecking) {
|
|
|
|
LOG(WARNING) << getId() << " path change requires state checking";
|
2015-03-14 00:09:20 +01:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPath = path;
|
2017-09-16 00:50:28 +02:00
|
|
|
|
2017-09-13 19:49:44 +02:00
|
|
|
auto listener = getListener();
|
|
|
|
if (listener) listener->onVolumePathChanged(getId(), mPath);
|
2017-09-16 00:50:28 +02:00
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
return OK;
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
2015-04-25 01:00:03 +02:00
|
|
|
status_t VolumeBase::setInternalPath(const std::string& internalPath) {
|
|
|
|
if (mState != State::kChecking) {
|
|
|
|
LOG(WARNING) << getId() << " internal path change requires state checking";
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
mInternalPath = internalPath;
|
2017-09-16 00:50:28 +02:00
|
|
|
|
2017-09-13 19:49:44 +02:00
|
|
|
auto listener = getListener();
|
2018-09-19 00:14:18 +02:00
|
|
|
if (listener) {
|
|
|
|
listener->onVolumeInternalPathChanged(getId(), mInternalPath);
|
|
|
|
}
|
2015-04-25 01:00:03 +02:00
|
|
|
|
2017-09-16 00:50:28 +02:00
|
|
|
return OK;
|
2015-04-09 06:07:21 +02:00
|
|
|
}
|
|
|
|
|
2018-08-01 19:24:13 +02:00
|
|
|
status_t VolumeBase::setLabel(const std::string& label) {
|
|
|
|
if (mState != State::kChecking) {
|
|
|
|
LOG(WARNING) << getId() << " label change requires state checking";
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
mLabel = label;
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2017-09-13 19:49:44 +02:00
|
|
|
android::sp<android::os::IVoldListener> VolumeBase::getListener() {
|
|
|
|
if (mSilent) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
return VolumeManager::Instance()->getListener();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
void VolumeBase::addVolume(const std::shared_ptr<VolumeBase>& volume) {
|
|
|
|
mVolumes.push_back(volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VolumeBase::removeVolume(const std::shared_ptr<VolumeBase>& volume) {
|
|
|
|
mVolumes.remove(volume);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<VolumeBase> VolumeBase::findVolume(const std::string& id) {
|
|
|
|
for (auto vol : mVolumes) {
|
|
|
|
if (vol->getId() == id) {
|
|
|
|
return vol;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t VolumeBase::create() {
|
|
|
|
CHECK(!mCreated);
|
2015-03-31 19:35:33 +02:00
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
mCreated = true;
|
2015-03-31 19:35:33 +02:00
|
|
|
status_t res = doCreate();
|
2017-09-16 00:50:28 +02:00
|
|
|
|
2017-09-13 19:49:44 +02:00
|
|
|
auto listener = getListener();
|
2018-09-19 00:14:18 +02:00
|
|
|
if (listener) {
|
|
|
|
listener->onVolumeCreated(getId(), static_cast<int32_t>(mType), mDiskId, mPartGuid);
|
|
|
|
}
|
2017-09-16 00:50:28 +02:00
|
|
|
|
2015-04-13 01:03:33 +02:00
|
|
|
setState(State::kUnmounted);
|
2015-03-31 19:35:33 +02:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t VolumeBase::doCreate() {
|
2015-03-14 00:09:20 +01:00
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t VolumeBase::destroy() {
|
|
|
|
CHECK(mCreated);
|
|
|
|
|
|
|
|
if (mState == State::kMounted) {
|
|
|
|
unmount();
|
2015-04-18 02:35:20 +02:00
|
|
|
setState(State::kBadRemoval);
|
|
|
|
} else {
|
|
|
|
setState(State::kRemoved);
|
2015-03-14 00:09:20 +01:00
|
|
|
}
|
|
|
|
|
2017-09-13 19:49:44 +02:00
|
|
|
auto listener = getListener();
|
2018-09-19 00:14:18 +02:00
|
|
|
if (listener) {
|
|
|
|
listener->onVolumeDestroyed(getId());
|
|
|
|
}
|
2017-09-16 00:50:28 +02:00
|
|
|
|
2015-03-31 19:35:33 +02:00
|
|
|
status_t res = doDestroy();
|
|
|
|
mCreated = false;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t VolumeBase::doDestroy() {
|
2015-03-14 00:09:20 +01:00
|
|
|
return OK;
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
status_t VolumeBase::mount() {
|
2015-04-05 06:38:59 +02:00
|
|
|
if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
|
|
|
|
LOG(WARNING) << getId() << " mount requires state unmounted or unmountable";
|
2015-03-03 06:01:40 +01:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2015-04-18 02:35:20 +02:00
|
|
|
setState(State::kChecking);
|
2015-03-03 06:01:40 +01:00
|
|
|
status_t res = doMount();
|
2015-03-14 00:09:20 +01:00
|
|
|
if (res == OK) {
|
2018-09-18 22:07:45 +02:00
|
|
|
res = VolumeManager::Instance()->onVolumeMounted(this);
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
2018-09-18 22:07:45 +02:00
|
|
|
setState(res == OK ? State::kMounted : State::kUnmountable);
|
2015-03-03 06:01:40 +01:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
status_t VolumeBase::unmount() {
|
2015-03-14 00:09:20 +01:00
|
|
|
if (mState != State::kMounted) {
|
|
|
|
LOG(WARNING) << getId() << " unmount requires state mounted";
|
2015-03-03 06:01:40 +01:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2015-04-18 02:35:20 +02:00
|
|
|
setState(State::kEjecting);
|
2016-07-27 23:11:02 +02:00
|
|
|
for (const auto& vol : mVolumes) {
|
2015-04-13 01:03:33 +02:00
|
|
|
if (vol->destroy()) {
|
2018-09-19 00:14:18 +02:00
|
|
|
LOG(WARNING) << getId() << " failed to destroy " << vol->getId() << " stacked above";
|
2015-03-03 06:01:40 +01:00
|
|
|
}
|
|
|
|
}
|
2015-03-14 00:09:20 +01:00
|
|
|
mVolumes.clear();
|
2015-03-03 06:01:40 +01:00
|
|
|
|
2018-09-18 22:07:45 +02:00
|
|
|
status_t res = VolumeManager::Instance()->onVolumeUnmounted(this);
|
|
|
|
if (res == OK) {
|
|
|
|
res = doUnmount();
|
|
|
|
setState(State::kUnmounted);
|
|
|
|
}
|
2015-03-03 06:01:40 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-05-22 07:35:42 +02:00
|
|
|
status_t VolumeBase::format(const std::string& fsType) {
|
2015-04-05 06:38:59 +02:00
|
|
|
if (mState == State::kMounted) {
|
|
|
|
unmount();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
|
|
|
|
LOG(WARNING) << getId() << " format requires state unmounted or unmountable";
|
2015-03-03 06:01:40 +01:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
2015-03-14 00:09:20 +01:00
|
|
|
setState(State::kFormatting);
|
2015-05-22 07:35:42 +02:00
|
|
|
status_t res = doFormat(fsType);
|
2015-03-14 00:09:20 +01:00
|
|
|
setState(State::kUnmounted);
|
2015-03-03 06:01:40 +01:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-05-22 07:35:42 +02:00
|
|
|
status_t VolumeBase::doFormat(const std::string& fsType) {
|
2015-03-03 06:01:40 +01:00
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
2018-09-18 22:07:45 +02:00
|
|
|
std::ostream& VolumeBase::operator<<(std::ostream& stream) const {
|
|
|
|
return stream << " VolumeBase{id=" << mId << ",label=" << mLabel
|
|
|
|
<< ",mountFlags=" << mMountFlags << ",mountUserId=" << mMountUserId << "}";
|
|
|
|
}
|
|
|
|
|
2015-03-03 06:01:40 +01:00
|
|
|
} // namespace vold
|
|
|
|
} // namespace android
|