blob: 2db5afec3a74c68be831886628751c9e29e71ab6 [file] [log] [blame]
Jeff Sharkeydeb24052015-03-02 21:01:40 -08001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
Jeff Sharkeydeb24052015-03-02 21:01:40 -080017#include "Utils.h"
18#include "VolumeBase.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070019#include "VolumeManager.h"
20#include "ResponseCode.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080021
Jeff Sharkey36801cc2015-03-13 16:09:20 -070022#include <base/stringprintf.h>
23#include <base/logging.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080024
25#include <fcntl.h>
26#include <stdlib.h>
27#include <sys/mount.h>
28#include <sys/stat.h>
29#include <sys/types.h>
30
Jeff Sharkey36801cc2015-03-13 16:09:20 -070031using android::base::StringPrintf;
32
33#define DEBUG 1
34
Jeff Sharkeydeb24052015-03-02 21:01:40 -080035namespace android {
36namespace vold {
37
Jeff Sharkey36801cc2015-03-13 16:09:20 -070038VolumeBase::VolumeBase(Type type) :
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070039 mType(type), mMountFlags(0), mMountUserId(-1), mCreated(false), mState(
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070040 State::kUnmounted), mSilent(false) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080041}
42
43VolumeBase::~VolumeBase() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070044 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080045}
46
Jeff Sharkey36801cc2015-03-13 16:09:20 -070047void VolumeBase::setState(State state) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080048 mState = state;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070049 notifyEvent(ResponseCode::VolumeStateChanged, StringPrintf("%d", mState));
Jeff Sharkeydeb24052015-03-02 21:01:40 -080050}
51
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070052status_t VolumeBase::setDiskId(const std::string& diskId) {
53 if (mCreated) {
54 LOG(WARNING) << getId() << " diskId change requires destroyed";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080055 return -EBUSY;
56 }
57
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070058 mDiskId = diskId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070059 return OK;
60}
61
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070062status_t VolumeBase::setMountFlags(int mountFlags) {
63 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
64 LOG(WARNING) << getId() << " flags change requires state unmounted or unmountable";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070065 return -EBUSY;
66 }
67
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070068 mMountFlags = mountFlags;
69 return OK;
70}
71
72status_t VolumeBase::setMountUserId(userid_t mountUserId) {
73 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
74 LOG(WARNING) << getId() << " user change requires state unmounted or unmountable";
75 return -EBUSY;
76 }
77
78 mMountUserId = mountUserId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070079 return OK;
80}
81
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070082status_t VolumeBase::setSilent(bool silent) {
83 if (mCreated) {
84 LOG(WARNING) << getId() << " silence change requires destroyed";
85 return -EBUSY;
86 }
87
88 mSilent = silent;
89 return OK;
90}
91
Jeff Sharkey36801cc2015-03-13 16:09:20 -070092status_t VolumeBase::setId(const std::string& id) {
93 if (mCreated) {
94 LOG(WARNING) << getId() << " id change requires not created";
95 return -EBUSY;
96 }
97
98 mId = id;
99 return OK;
100}
101
102status_t VolumeBase::setPath(const std::string& path) {
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700103 if (mState != State::kChecking) {
104 LOG(WARNING) << getId() << " path change requires state checking";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700105 return -EBUSY;
106 }
107
108 mPath = path;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700109 notifyEvent(ResponseCode::VolumePathChanged, mPath);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700110 return OK;
111}
112
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700113void VolumeBase::notifyEvent(int event) {
114 if (mSilent) return;
115 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
116 getId().c_str(), false);
117}
118
119void VolumeBase::notifyEvent(int event, const std::string& value) {
120 if (mSilent) return;
121 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
122 StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
123}
124
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700125void VolumeBase::addVolume(const std::shared_ptr<VolumeBase>& volume) {
126 mVolumes.push_back(volume);
127}
128
129void VolumeBase::removeVolume(const std::shared_ptr<VolumeBase>& volume) {
130 mVolumes.remove(volume);
131}
132
133std::shared_ptr<VolumeBase> VolumeBase::findVolume(const std::string& id) {
134 for (auto vol : mVolumes) {
135 if (vol->getId() == id) {
136 return vol;
137 }
138 }
139 return nullptr;
140}
141
142status_t VolumeBase::create() {
143 CHECK(!mCreated);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700144
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700145 mCreated = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700146 status_t res = doCreate();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700147 notifyEvent(ResponseCode::VolumeCreated, StringPrintf("%d %s", mType, mDiskId.c_str()));
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700148 setState(State::kUnmounted);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700149 return res;
150}
151
152status_t VolumeBase::doCreate() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700153 return OK;
154}
155
156status_t VolumeBase::destroy() {
157 CHECK(mCreated);
158
159 if (mState == State::kMounted) {
160 unmount();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700161 setState(State::kBadRemoval);
162 } else {
163 setState(State::kRemoved);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700164 }
165
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700166 notifyEvent(ResponseCode::VolumeDestroyed);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700167 status_t res = doDestroy();
168 mCreated = false;
169 return res;
170}
171
172status_t VolumeBase::doDestroy() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700173 return OK;
174}
175
176status_t VolumeBase::mount() {
Jeff Sharkey0fd95352015-04-04 21:38:59 -0700177 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
178 LOG(WARNING) << getId() << " mount requires state unmounted or unmountable";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700179 return -EBUSY;
180 }
181
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700182 setState(State::kChecking);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800183 status_t res = doMount();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700184 if (res == OK) {
185 setState(State::kMounted);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800186 } else {
Jeff Sharkey0fd95352015-04-04 21:38:59 -0700187 setState(State::kUnmountable);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800188 }
189
190 return res;
191}
192
193status_t VolumeBase::unmount() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700194 if (mState != State::kMounted) {
195 LOG(WARNING) << getId() << " unmount requires state mounted";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800196 return -EBUSY;
197 }
198
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700199 setState(State::kEjecting);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800200
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700201 for (auto vol : mVolumes) {
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700202 if (vol->destroy()) {
203 LOG(WARNING) << getId() << " failed to destroy " << vol->getId()
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700204 << " stacked above";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800205 }
206 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700207 mVolumes.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800208
209 status_t res = doUnmount();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700210 setState(State::kUnmounted);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800211 return res;
212}
213
214status_t VolumeBase::format() {
Jeff Sharkey0fd95352015-04-04 21:38:59 -0700215 if (mState == State::kMounted) {
216 unmount();
217 }
218
219 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
220 LOG(WARNING) << getId() << " format requires state unmounted or unmountable";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800221 return -EBUSY;
222 }
223
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700224 setState(State::kFormatting);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800225 status_t res = doFormat();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700226 setState(State::kUnmounted);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800227 return res;
228}
229
230status_t VolumeBase::doFormat() {
231 return -ENOTSUP;
232}
233
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800234} // namespace vold
235} // namespace android