1 /*
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 
17 #include "VolumeBase.h"
18 #include "Utils.h"
19 #include "VolumeManager.h"
20 
21 #include <android-base/logging.h>
22 #include <android-base/stringprintf.h>
23 
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <sys/mount.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 
30 using android::base::StringPrintf;
31 
32 namespace android {
33 namespace vold {
34 
VolumeBase(Type type)35 VolumeBase::VolumeBase(Type type)
36     : mType(type),
37       mMountFlags(0),
38       mMountUserId(USER_UNKNOWN),
39       mCreated(false),
40       mState(State::kUnmounted),
41       mSilent(false) {}
42 
~VolumeBase()43 VolumeBase::~VolumeBase() {
44     CHECK(!mCreated);
45 }
46 
setState(State state)47 void VolumeBase::setState(State state) {
48     mState = state;
49 
50     auto listener = getListener();
51     if (listener) {
52         listener->onVolumeStateChanged(getId(), static_cast<int32_t>(mState));
53     }
54 }
55 
setDiskId(const std::string & diskId)56 status_t VolumeBase::setDiskId(const std::string& diskId) {
57     if (mCreated) {
58         LOG(WARNING) << getId() << " diskId change requires destroyed";
59         return -EBUSY;
60     }
61 
62     mDiskId = diskId;
63     return OK;
64 }
65 
setPartGuid(const std::string & partGuid)66 status_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 
setMountFlags(int mountFlags)76 status_t VolumeBase::setMountFlags(int mountFlags) {
77     if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
78         LOG(WARNING) << getId() << " flags change requires state unmounted or unmountable";
79         return -EBUSY;
80     }
81 
82     mMountFlags = mountFlags;
83     return OK;
84 }
85 
setMountUserId(userid_t mountUserId)86 status_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;
93     return OK;
94 }
95 
setSilent(bool silent)96 status_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 
setId(const std::string & id)106 status_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 
setPath(const std::string & path)116 status_t VolumeBase::setPath(const std::string& path) {
117     if (mState != State::kChecking) {
118         LOG(WARNING) << getId() << " path change requires state checking";
119         return -EBUSY;
120     }
121 
122     mPath = path;
123 
124     auto listener = getListener();
125     if (listener) listener->onVolumePathChanged(getId(), mPath);
126 
127     return OK;
128 }
129 
setInternalPath(const std::string & internalPath)130 status_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;
137 
138     auto listener = getListener();
139     if (listener) {
140         listener->onVolumeInternalPathChanged(getId(), mInternalPath);
141     }
142 
143     return OK;
144 }
145 
getListener() const146 android::sp<android::os::IVoldListener> VolumeBase::getListener() const {
147     if (mSilent) {
148         return nullptr;
149     } else {
150         return VolumeManager::Instance()->getListener();
151     }
152 }
153 
addVolume(const std::shared_ptr<VolumeBase> & volume)154 void VolumeBase::addVolume(const std::shared_ptr<VolumeBase>& volume) {
155     mVolumes.push_back(volume);
156 }
157 
removeVolume(const std::shared_ptr<VolumeBase> & volume)158 void VolumeBase::removeVolume(const std::shared_ptr<VolumeBase>& volume) {
159     mVolumes.remove(volume);
160 }
161 
findVolume(const std::string & id)162 std::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 
create()171 status_t VolumeBase::create() {
172     CHECK(!mCreated);
173 
174     mCreated = true;
175     status_t res = doCreate();
176 
177     auto listener = getListener();
178     if (listener) {
179         listener->onVolumeCreated(getId(), static_cast<int32_t>(mType), mDiskId, mPartGuid);
180     }
181 
182     setState(State::kUnmounted);
183     return res;
184 }
185 
doCreate()186 status_t VolumeBase::doCreate() {
187     return OK;
188 }
189 
destroy()190 status_t VolumeBase::destroy() {
191     CHECK(mCreated);
192 
193     if (mState == State::kMounted) {
194         unmount();
195         setState(State::kBadRemoval);
196     } else {
197         setState(State::kRemoved);
198     }
199 
200     auto listener = getListener();
201     if (listener) {
202         listener->onVolumeDestroyed(getId());
203     }
204 
205     status_t res = doDestroy();
206     mCreated = false;
207     return res;
208 }
209 
doDestroy()210 status_t VolumeBase::doDestroy() {
211     return OK;
212 }
213 
mount()214 status_t VolumeBase::mount() {
215     if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
216         LOG(WARNING) << getId() << " mount requires state unmounted or unmountable";
217         return -EBUSY;
218     }
219 
220     setState(State::kChecking);
221     status_t res = doMount();
222     setState(res == OK ? State::kMounted : State::kUnmountable);
223 
224     return res;
225 }
226 
unmount()227 status_t VolumeBase::unmount() {
228     if (mState != State::kMounted) {
229         LOG(WARNING) << getId() << " unmount requires state mounted";
230         return -EBUSY;
231     }
232 
233     setState(State::kEjecting);
234     for (const auto& vol : mVolumes) {
235         if (vol->destroy()) {
236             LOG(WARNING) << getId() << " failed to destroy " << vol->getId() << " stacked above";
237         }
238     }
239     mVolumes.clear();
240 
241     status_t res = doUnmount();
242     setState(State::kUnmounted);
243     return res;
244 }
245 
format(const std::string & fsType)246 status_t VolumeBase::format(const std::string& fsType) {
247     if (mState == State::kMounted) {
248         unmount();
249     }
250 
251     if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) {
252         LOG(WARNING) << getId() << " format requires state unmounted or unmountable";
253         return -EBUSY;
254     }
255 
256     setState(State::kFormatting);
257     status_t res = doFormat(fsType);
258     setState(State::kUnmounted);
259     return res;
260 }
261 
doFormat(const std::string & fsType)262 status_t VolumeBase::doFormat(const std::string& fsType) {
263     return -ENOTSUP;
264 }
265 
operator <<(std::ostream & stream) const266 std::ostream& VolumeBase::operator<<(std::ostream& stream) const {
267     return stream << " VolumeBase{id=" << mId << ",mountFlags=" << mMountFlags
268                   << ",mountUserId=" << mMountUserId << "}";
269 }
270 
271 }  // namespace vold
272 }  // namespace android
273