blob: 429f13485ff0d75e065fcd4a6b91df0d29490891 [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"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080020
Elliott Hughes7e128fb2015-12-04 15:50:53 -080021#include <android-base/stringprintf.h>
22#include <android-base/logging.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080023
24#include <fcntl.h>
25#include <stdlib.h>
26#include <sys/mount.h>
27#include <sys/stat.h>
28#include <sys/types.h>
29
Jeff Sharkey36801cc2015-03-13 16:09:20 -070030using android::base::StringPrintf;
31
Jeff Sharkeydeb24052015-03-02 21:01:40 -080032namespace android {
33namespace vold {
34
Jeff Sharkey36801cc2015-03-13 16:09:20 -070035VolumeBase::VolumeBase(Type type) :
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070036 mType(type), mMountFlags(0), mMountUserId(-1), mCreated(false), mState(
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070037 State::kUnmounted), mSilent(false) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080038}
39
40VolumeBase::~VolumeBase() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070041 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080042}
43
Jeff Sharkey36801cc2015-03-13 16:09:20 -070044void VolumeBase::setState(State state) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -080045 mState = state;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -060046
Jeff Sharkey814e9d32017-09-13 11:49:44 -060047 auto listener = getListener();
48 if (listener) listener->onVolumeStateChanged(getId(), static_cast<int32_t>(mState));
Jeff Sharkeydeb24052015-03-02 21:01:40 -080049}
50
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070051status_t VolumeBase::setDiskId(const std::string& diskId) {
52 if (mCreated) {
53 LOG(WARNING) << getId() << " diskId change requires destroyed";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080054 return -EBUSY;
55 }
56
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070057 mDiskId = diskId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070058 return OK;
59}
60
Jeff Sharkeybc40cc82015-06-18 14:25:08 -070061status_t VolumeBase::setPartGuid(const std::string& partGuid) {
62 if (mCreated) {
63 LOG(WARNING) << getId() << " partGuid change requires destroyed";
64 return -EBUSY;
65 }
66
67 mPartGuid = partGuid;
68 return OK;
69}
70
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070071status_t VolumeBase::setMountFlags(int mountFlags) {
72 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
73 LOG(WARNING) << getId() << " flags change requires state unmounted or unmountable";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070074 return -EBUSY;
75 }
76
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070077 mMountFlags = mountFlags;
78 return OK;
79}
80
81status_t VolumeBase::setMountUserId(userid_t mountUserId) {
82 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
83 LOG(WARNING) << getId() << " user change requires state unmounted or unmountable";
84 return -EBUSY;
85 }
86
87 mMountUserId = mountUserId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070088 return OK;
89}
90
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070091status_t VolumeBase::setSilent(bool silent) {
92 if (mCreated) {
93 LOG(WARNING) << getId() << " silence change requires destroyed";
94 return -EBUSY;
95 }
96
97 mSilent = silent;
98 return OK;
99}
100
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700101status_t VolumeBase::setId(const std::string& id) {
102 if (mCreated) {
103 LOG(WARNING) << getId() << " id change requires not created";
104 return -EBUSY;
105 }
106
107 mId = id;
108 return OK;
109}
110
111status_t VolumeBase::setPath(const std::string& path) {
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700112 if (mState != State::kChecking) {
113 LOG(WARNING) << getId() << " path change requires state checking";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700114 return -EBUSY;
115 }
116
117 mPath = path;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600118
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600119 auto listener = getListener();
120 if (listener) listener->onVolumePathChanged(getId(), mPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600121
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700122 return OK;
123}
124
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700125status_t VolumeBase::setInternalPath(const std::string& internalPath) {
126 if (mState != State::kChecking) {
127 LOG(WARNING) << getId() << " internal path change requires state checking";
128 return -EBUSY;
129 }
130
131 mInternalPath = internalPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600132
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600133 auto listener = getListener();
134 if (listener) listener->onVolumeInternalPathChanged(getId(), mInternalPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600135
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700136 return OK;
137}
138
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600139android::sp<android::os::IVoldListener> VolumeBase::getListener() {
140 if (mSilent) {
141 return nullptr;
142 } else {
143 return VolumeManager::Instance()->getListener();
144 }
145}
146
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700147void VolumeBase::addVolume(const std::shared_ptr<VolumeBase>& volume) {
148 mVolumes.push_back(volume);
149}
150
151void VolumeBase::removeVolume(const std::shared_ptr<VolumeBase>& volume) {
152 mVolumes.remove(volume);
153}
154
155std::shared_ptr<VolumeBase> VolumeBase::findVolume(const std::string& id) {
156 for (auto vol : mVolumes) {
157 if (vol->getId() == id) {
158 return vol;
159 }
160 }
161 return nullptr;
162}
163
164status_t VolumeBase::create() {
165 CHECK(!mCreated);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700166
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700167 mCreated = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700168 status_t res = doCreate();
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600169
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600170 auto listener = getListener();
171 if (listener) listener->onVolumeCreated(getId(),
172 static_cast<int32_t>(mType), mDiskId, mPartGuid);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600173
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700174 setState(State::kUnmounted);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700175 return res;
176}
177
178status_t VolumeBase::doCreate() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700179 return OK;
180}
181
182status_t VolumeBase::destroy() {
183 CHECK(mCreated);
184
185 if (mState == State::kMounted) {
186 unmount();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700187 setState(State::kBadRemoval);
188 } else {
189 setState(State::kRemoved);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700190 }
191
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600192
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600193 auto listener = getListener();
194 if (listener) listener->onVolumeDestroyed(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600195
Jeff Sharkey9c484982015-03-31 10:35:33 -0700196 status_t res = doDestroy();
197 mCreated = false;
198 return res;
199}
200
201status_t VolumeBase::doDestroy() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700202 return OK;
203}
204
205status_t VolumeBase::mount() {
Jeff Sharkey0fd95352015-04-04 21:38:59 -0700206 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
207 LOG(WARNING) << getId() << " mount requires state unmounted or unmountable";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700208 return -EBUSY;
209 }
210
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700211 setState(State::kChecking);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800212 status_t res = doMount();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700213 if (res == OK) {
214 setState(State::kMounted);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800215 } else {
Jeff Sharkey0fd95352015-04-04 21:38:59 -0700216 setState(State::kUnmountable);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800217 }
218
219 return res;
220}
221
222status_t VolumeBase::unmount() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700223 if (mState != State::kMounted) {
224 LOG(WARNING) << getId() << " unmount requires state mounted";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800225 return -EBUSY;
226 }
227
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700228 setState(State::kEjecting);
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700229 for (const auto& vol : mVolumes) {
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700230 if (vol->destroy()) {
231 LOG(WARNING) << getId() << " failed to destroy " << vol->getId()
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700232 << " stacked above";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800233 }
234 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700235 mVolumes.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800236
237 status_t res = doUnmount();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700238 setState(State::kUnmounted);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800239 return res;
240}
241
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700242status_t VolumeBase::format(const std::string& fsType) {
Jeff Sharkey0fd95352015-04-04 21:38:59 -0700243 if (mState == State::kMounted) {
244 unmount();
245 }
246
247 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
248 LOG(WARNING) << getId() << " format requires state unmounted or unmountable";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800249 return -EBUSY;
250 }
251
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700252 setState(State::kFormatting);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700253 status_t res = doFormat(fsType);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700254 setState(State::kUnmounted);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800255 return res;
256}
257
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700258status_t VolumeBase::doFormat(const std::string& fsType) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800259 return -ENOTSUP;
260}
261
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800262} // namespace vold
263} // namespace android