blob: ca056a437d5c61b2adfecad5f31f1eab0e83a949 [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 Sharkey1d6fbcc2015-04-24 16:00:03 -0700113status_t VolumeBase::setInternalPath(const std::string& internalPath) {
114 if (mState != State::kChecking) {
115 LOG(WARNING) << getId() << " internal path change requires state checking";
116 return -EBUSY;
117 }
118
119 mInternalPath = internalPath;
120 notifyEvent(ResponseCode::VolumeInternalPathChanged, mInternalPath);
121 return OK;
122}
123
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700124void VolumeBase::notifyEvent(int event) {
125 if (mSilent) return;
126 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
127 getId().c_str(), false);
128}
129
130void VolumeBase::notifyEvent(int event, const std::string& value) {
131 if (mSilent) return;
132 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
133 StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
134}
135
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700136void VolumeBase::addVolume(const std::shared_ptr<VolumeBase>& volume) {
137 mVolumes.push_back(volume);
138}
139
140void VolumeBase::removeVolume(const std::shared_ptr<VolumeBase>& volume) {
141 mVolumes.remove(volume);
142}
143
144std::shared_ptr<VolumeBase> VolumeBase::findVolume(const std::string& id) {
145 for (auto vol : mVolumes) {
146 if (vol->getId() == id) {
147 return vol;
148 }
149 }
150 return nullptr;
151}
152
153status_t VolumeBase::create() {
154 CHECK(!mCreated);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700155
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700156 mCreated = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700157 status_t res = doCreate();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700158 notifyEvent(ResponseCode::VolumeCreated, StringPrintf("%d %s", mType, mDiskId.c_str()));
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700159 setState(State::kUnmounted);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700160 return res;
161}
162
163status_t VolumeBase::doCreate() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700164 return OK;
165}
166
167status_t VolumeBase::destroy() {
168 CHECK(mCreated);
169
170 if (mState == State::kMounted) {
171 unmount();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700172 setState(State::kBadRemoval);
173 } else {
174 setState(State::kRemoved);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700175 }
176
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700177 notifyEvent(ResponseCode::VolumeDestroyed);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700178 status_t res = doDestroy();
179 mCreated = false;
180 return res;
181}
182
183status_t VolumeBase::doDestroy() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700184 return OK;
185}
186
187status_t VolumeBase::mount() {
Jeff Sharkey0fd95352015-04-04 21:38:59 -0700188 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
189 LOG(WARNING) << getId() << " mount requires state unmounted or unmountable";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700190 return -EBUSY;
191 }
192
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700193 setState(State::kChecking);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800194 status_t res = doMount();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700195 if (res == OK) {
196 setState(State::kMounted);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800197 } else {
Jeff Sharkey0fd95352015-04-04 21:38:59 -0700198 setState(State::kUnmountable);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800199 }
200
201 return res;
202}
203
204status_t VolumeBase::unmount() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700205 if (mState != State::kMounted) {
206 LOG(WARNING) << getId() << " unmount requires state mounted";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800207 return -EBUSY;
208 }
209
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700210 setState(State::kEjecting);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800211
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700212 for (auto vol : mVolumes) {
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700213 if (vol->destroy()) {
214 LOG(WARNING) << getId() << " failed to destroy " << vol->getId()
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700215 << " stacked above";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800216 }
217 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700218 mVolumes.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800219
220 status_t res = doUnmount();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700221 setState(State::kUnmounted);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800222 return res;
223}
224
225status_t VolumeBase::format() {
Jeff Sharkey0fd95352015-04-04 21:38:59 -0700226 if (mState == State::kMounted) {
227 unmount();
228 }
229
230 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
231 LOG(WARNING) << getId() << " format requires state unmounted or unmountable";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800232 return -EBUSY;
233 }
234
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700235 setState(State::kFormatting);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800236 status_t res = doFormat();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700237 setState(State::kUnmounted);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800238 return res;
239}
240
241status_t VolumeBase::doFormat() {
242 return -ENOTSUP;
243}
244
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800245} // namespace vold
246} // namespace android