2 * Copyright (C) 2015 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #define LOG_TAG "Vold"
20 #include "VolumeBase.h"
22 #include <cutils/log.h>
26 #include <sys/mount.h>
28 #include <sys/types.h>
33 VolumeBase::VolumeBase(VolumeType type) :
34 mType(type), mState(VolumeState::kUnmounted) {
37 VolumeBase::~VolumeBase() {
40 void VolumeBase::setState(VolumeState state) {
43 // TODO: publish state up to framework
46 void VolumeBase::stackVolume(const std::shared_ptr<VolumeBase>& volume) {
47 mStacked.push_back(volume);
50 void VolumeBase::unstackVolume(const std::shared_ptr<VolumeBase>& volume) {
51 mStacked.remove(volume);
54 status_t VolumeBase::mount() {
55 if (getState() != VolumeState::kUnmounted) {
56 SLOGE("Must be unmounted to mount %s", getId().c_str());
60 setState(VolumeState::kMounting);
61 status_t res = doMount();
63 setState(VolumeState::kMounted);
65 setState(VolumeState::kCorrupt);
71 status_t VolumeBase::unmount() {
72 if (getState() != VolumeState::kMounted) {
73 SLOGE("Must be mounted to unmount %s", getId().c_str());
77 setState(VolumeState::kUnmounting);
79 for (std::string target : mBindTargets) {
84 for (std::shared_ptr<VolumeBase> v : mStacked) {
86 ALOGW("Failed to unmount %s stacked above %s", v->getId().c_str(),
92 status_t res = doUnmount();
93 setState(VolumeState::kUnmounted);
97 status_t VolumeBase::format() {
98 if (getState() != VolumeState::kUnmounted
99 || getState() != VolumeState::kCorrupt) {
100 SLOGE("Must be unmounted or corrupt to format %s", getId().c_str());
104 setState(VolumeState::kFormatting);
105 status_t res = doFormat();
106 setState(VolumeState::kUnmounted);
110 status_t VolumeBase::doFormat() {
114 status_t VolumeBase::mountBind(const std::string& source, const std::string& target) {
115 if (::mount(source.c_str(), target.c_str(), "", MS_BIND, NULL)) {
116 SLOGE("Failed to bind mount %s to %s: %s", source.c_str(),
117 target.c_str(), strerror(errno));
120 mBindTargets.push_back(target);
124 status_t VolumeBase::unmountBind(const std::string& target) {
125 ForceUnmount(target);
126 mBindTargets.remove(target);
131 } // namespace android