blob: b04dd70f92d0a6af0a7a8a874153b04f86af0620 [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 "VolumeBase.h"
Paul Crowley8915d622018-09-18 15:14:18 -070018#include "Utils.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/logging.h>
Paul Crowley8915d622018-09-18 15:14:18 -070022#include <android-base/stringprintf.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
Paul Crowley8915d622018-09-18 15:14:18 -070035VolumeBase::VolumeBase(Type type)
36 : mType(type),
37 mMountFlags(0),
38 mMountUserId(-1),
39 mCreated(false),
40 mState(State::kUnmounted),
41 mSilent(false) {}
Jeff Sharkeydeb24052015-03-02 21:01:40 -080042
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 Sharkeycbe69fc2017-09-15 16:50:28 -060049
Jeff Sharkey814e9d32017-09-13 11:49:44 -060050 auto listener = getListener();
Paul Crowley8915d622018-09-18 15:14:18 -070051 if (listener) {
52 listener->onVolumeStateChanged(getId(), static_cast<int32_t>(mState));
53 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -080054}
55
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070056status_t VolumeBase::setDiskId(const std::string& diskId) {
57 if (mCreated) {
58 LOG(WARNING) << getId() << " diskId change requires destroyed";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080059 return -EBUSY;
60 }
61
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070062 mDiskId = diskId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070063 return OK;
64}
65
Jeff Sharkeybc40cc82015-06-18 14:25:08 -070066status_t VolumeBase::setPartGuid(const std::string& partGuid) {
67 if (mCreated) {
68 LOG(WARNING) << getId() << " partGuid change requires destroyed";
69 return -EBUSY;
70 }
71
72 mPartGuid = partGuid;
73 return OK;
74}
75
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070076status_t VolumeBase::setMountFlags(int mountFlags) {
77 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
78 LOG(WARNING) << getId() << " flags change requires state unmounted or unmountable";
Jeff Sharkey36801cc2015-03-13 16:09:20 -070079 return -EBUSY;
80 }
81
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070082 mMountFlags = mountFlags;
83 return OK;
84}
85
86status_t VolumeBase::setMountUserId(userid_t mountUserId) {
87 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
88 LOG(WARNING) << getId() << " user change requires state unmounted or unmountable";
89 return -EBUSY;
90 }
91
92 mMountUserId = mountUserId;
Jeff Sharkey36801cc2015-03-13 16:09:20 -070093 return OK;
94}
95
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070096status_t VolumeBase::setSilent(bool silent) {
97 if (mCreated) {
98 LOG(WARNING) << getId() << " silence change requires destroyed";
99 return -EBUSY;
100 }
101
102 mSilent = silent;
103 return OK;
104}
105
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700106status_t VolumeBase::setId(const std::string& id) {
107 if (mCreated) {
108 LOG(WARNING) << getId() << " id change requires not created";
109 return -EBUSY;
110 }
111
112 mId = id;
113 return OK;
114}
115
116status_t VolumeBase::setPath(const std::string& path) {
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700117 if (mState != State::kChecking) {
118 LOG(WARNING) << getId() << " path change requires state checking";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700119 return -EBUSY;
120 }
121
122 mPath = path;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600123
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600124 auto listener = getListener();
125 if (listener) listener->onVolumePathChanged(getId(), mPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600126
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700127 return OK;
128}
129
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700130status_t VolumeBase::setInternalPath(const std::string& internalPath) {
131 if (mState != State::kChecking) {
132 LOG(WARNING) << getId() << " internal path change requires state checking";
133 return -EBUSY;
134 }
135
136 mInternalPath = internalPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600137
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600138 auto listener = getListener();
Paul Crowley8915d622018-09-18 15:14:18 -0700139 if (listener) {
140 listener->onVolumeInternalPathChanged(getId(), mInternalPath);
141 }
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600142
Jeff Sharkey1d6fbcc2015-04-24 16:00:03 -0700143 return OK;
144}
145
Greg Kaiseref9abab2018-12-18 08:42:08 -0800146android::sp<android::os::IVoldListener> VolumeBase::getListener() const {
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600147 if (mSilent) {
148 return nullptr;
149 } else {
150 return VolumeManager::Instance()->getListener();
151 }
152}
153
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700154void VolumeBase::addVolume(const std::shared_ptr<VolumeBase>& volume) {
155 mVolumes.push_back(volume);
156}
157
158void VolumeBase::removeVolume(const std::shared_ptr<VolumeBase>& volume) {
159 mVolumes.remove(volume);
160}
161
162std::shared_ptr<VolumeBase> VolumeBase::findVolume(const std::string& id) {
163 for (auto vol : mVolumes) {
164 if (vol->getId() == id) {
165 return vol;
166 }
167 }
168 return nullptr;
169}
170
171status_t VolumeBase::create() {
172 CHECK(!mCreated);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700173
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700174 mCreated = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700175 status_t res = doCreate();
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600176
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600177 auto listener = getListener();
Paul Crowley8915d622018-09-18 15:14:18 -0700178 if (listener) {
179 listener->onVolumeCreated(getId(), static_cast<int32_t>(mType), mDiskId, mPartGuid);
180 }
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600181
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700182 setState(State::kUnmounted);
Jeff Sharkey9c484982015-03-31 10:35:33 -0700183 return res;
184}
185
186status_t VolumeBase::doCreate() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700187 return OK;
188}
189
190status_t VolumeBase::destroy() {
191 CHECK(mCreated);
192
193 if (mState == State::kMounted) {
194 unmount();
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700195 setState(State::kBadRemoval);
196 } else {
197 setState(State::kRemoved);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700198 }
199
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600200 auto listener = getListener();
Paul Crowley8915d622018-09-18 15:14:18 -0700201 if (listener) {
202 listener->onVolumeDestroyed(getId());
203 }
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600204
Jeff Sharkey9c484982015-03-31 10:35:33 -0700205 status_t res = doDestroy();
206 mCreated = false;
207 return res;
208}
209
210status_t VolumeBase::doDestroy() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700211 return OK;
212}
213
214status_t VolumeBase::mount() {
Jeff Sharkey0fd95352015-04-04 21:38:59 -0700215 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
216 LOG(WARNING) << getId() << " mount requires state unmounted or unmountable";
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700217 return -EBUSY;
218 }
219
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700220 setState(State::kChecking);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800221 status_t res = doMount();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700222 if (res == OK) {
223 setState(State::kMounted);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800224 } else {
Jeff Sharkey0fd95352015-04-04 21:38:59 -0700225 setState(State::kUnmountable);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800226 }
227
228 return res;
229}
230
231status_t VolumeBase::unmount() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700232 if (mState != State::kMounted) {
233 LOG(WARNING) << getId() << " unmount requires state mounted";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800234 return -EBUSY;
235 }
236
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700237 setState(State::kEjecting);
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700238 for (const auto& vol : mVolumes) {
Jeff Sharkey3161fb32015-04-12 16:03:33 -0700239 if (vol->destroy()) {
Paul Crowley8915d622018-09-18 15:14:18 -0700240 LOG(WARNING) << getId() << " failed to destroy " << vol->getId() << " stacked above";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800241 }
242 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700243 mVolumes.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800244
245 status_t res = doUnmount();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700246 setState(State::kUnmounted);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800247 return res;
248}
249
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700250status_t VolumeBase::format(const std::string& fsType) {
Jeff Sharkey0fd95352015-04-04 21:38:59 -0700251 if (mState == State::kMounted) {
252 unmount();
253 }
254
255 if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
256 LOG(WARNING) << getId() << " format requires state unmounted or unmountable";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800257 return -EBUSY;
258 }
259
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700260 setState(State::kFormatting);
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700261 status_t res = doFormat(fsType);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700262 setState(State::kUnmounted);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800263 return res;
264}
265
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700266status_t VolumeBase::doFormat(const std::string& fsType) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800267 return -ENOTSUP;
268}
269
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800270} // namespace vold
271} // namespace android