Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 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 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 17 | #include "Utils.h" |
| 18 | #include "VolumeBase.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 19 | #include "VolumeManager.h" |
| 20 | #include "ResponseCode.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 21 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
| 23 | #include <android-base/logging.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 24 | |
| 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 Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 31 | using android::base::StringPrintf; |
| 32 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 33 | namespace android { |
| 34 | namespace vold { |
| 35 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 36 | VolumeBase::VolumeBase(Type type) : |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 37 | mType(type), mMountFlags(0), mMountUserId(-1), mCreated(false), mState( |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 38 | State::kUnmounted), mSilent(false) { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | VolumeBase::~VolumeBase() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 42 | CHECK(!mCreated); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 45 | void VolumeBase::setState(State state) { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 46 | mState = state; |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 47 | #if ENABLE_BINDER |
| 48 | auto listener = getListener(); |
| 49 | if (listener) listener->onVolumeStateChanged(getId(), static_cast<int32_t>(mState)); |
| 50 | #else |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 51 | notifyEvent(ResponseCode::VolumeStateChanged, StringPrintf("%d", mState)); |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 52 | #endif |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 55 | status_t VolumeBase::setDiskId(const std::string& diskId) { |
| 56 | if (mCreated) { |
| 57 | LOG(WARNING) << getId() << " diskId change requires destroyed"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 58 | return -EBUSY; |
| 59 | } |
| 60 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 61 | mDiskId = diskId; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 62 | return OK; |
| 63 | } |
| 64 | |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 65 | status_t VolumeBase::setPartGuid(const std::string& partGuid) { |
| 66 | if (mCreated) { |
| 67 | LOG(WARNING) << getId() << " partGuid change requires destroyed"; |
| 68 | return -EBUSY; |
| 69 | } |
| 70 | |
| 71 | mPartGuid = partGuid; |
| 72 | return OK; |
| 73 | } |
| 74 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 75 | status_t VolumeBase::setMountFlags(int mountFlags) { |
| 76 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 77 | LOG(WARNING) << getId() << " flags change requires state unmounted or unmountable"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 78 | return -EBUSY; |
| 79 | } |
| 80 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 81 | mMountFlags = mountFlags; |
| 82 | return OK; |
| 83 | } |
| 84 | |
| 85 | status_t VolumeBase::setMountUserId(userid_t mountUserId) { |
| 86 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 87 | LOG(WARNING) << getId() << " user change requires state unmounted or unmountable"; |
| 88 | return -EBUSY; |
| 89 | } |
| 90 | |
| 91 | mMountUserId = mountUserId; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 92 | return OK; |
| 93 | } |
| 94 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 95 | status_t VolumeBase::setSilent(bool silent) { |
| 96 | if (mCreated) { |
| 97 | LOG(WARNING) << getId() << " silence change requires destroyed"; |
| 98 | return -EBUSY; |
| 99 | } |
| 100 | |
| 101 | mSilent = silent; |
| 102 | return OK; |
| 103 | } |
| 104 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 105 | status_t VolumeBase::setId(const std::string& id) { |
| 106 | if (mCreated) { |
| 107 | LOG(WARNING) << getId() << " id change requires not created"; |
| 108 | return -EBUSY; |
| 109 | } |
| 110 | |
| 111 | mId = id; |
| 112 | return OK; |
| 113 | } |
| 114 | |
| 115 | status_t VolumeBase::setPath(const std::string& path) { |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 116 | if (mState != State::kChecking) { |
| 117 | LOG(WARNING) << getId() << " path change requires state checking"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 118 | return -EBUSY; |
| 119 | } |
| 120 | |
| 121 | mPath = path; |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 122 | #if ENABLE_BINDER |
| 123 | auto listener = getListener(); |
| 124 | if (listener) listener->onVolumePathChanged(getId(), mPath); |
| 125 | #else |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 126 | notifyEvent(ResponseCode::VolumePathChanged, mPath); |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 127 | #endif |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 128 | return OK; |
| 129 | } |
| 130 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 131 | status_t VolumeBase::setInternalPath(const std::string& internalPath) { |
| 132 | if (mState != State::kChecking) { |
| 133 | LOG(WARNING) << getId() << " internal path change requires state checking"; |
| 134 | return -EBUSY; |
| 135 | } |
| 136 | |
| 137 | mInternalPath = internalPath; |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 138 | #if ENABLE_BINDER |
| 139 | auto listener = getListener(); |
| 140 | if (listener) listener->onVolumeInternalPathChanged(getId(), mInternalPath); |
| 141 | #else |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 142 | notifyEvent(ResponseCode::VolumeInternalPathChanged, mInternalPath); |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 143 | #endif |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 144 | return OK; |
| 145 | } |
| 146 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 147 | void VolumeBase::notifyEvent(int event) { |
| 148 | if (mSilent) return; |
| 149 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event, |
| 150 | getId().c_str(), false); |
| 151 | } |
| 152 | |
| 153 | void VolumeBase::notifyEvent(int event, const std::string& value) { |
| 154 | if (mSilent) return; |
| 155 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event, |
| 156 | StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false); |
| 157 | } |
| 158 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 159 | android::sp<android::os::IVoldListener> VolumeBase::getListener() { |
| 160 | if (mSilent) { |
| 161 | return nullptr; |
| 162 | } else { |
| 163 | return VolumeManager::Instance()->getListener(); |
| 164 | } |
| 165 | } |
| 166 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 167 | void VolumeBase::addVolume(const std::shared_ptr<VolumeBase>& volume) { |
| 168 | mVolumes.push_back(volume); |
| 169 | } |
| 170 | |
| 171 | void VolumeBase::removeVolume(const std::shared_ptr<VolumeBase>& volume) { |
| 172 | mVolumes.remove(volume); |
| 173 | } |
| 174 | |
| 175 | std::shared_ptr<VolumeBase> VolumeBase::findVolume(const std::string& id) { |
| 176 | for (auto vol : mVolumes) { |
| 177 | if (vol->getId() == id) { |
| 178 | return vol; |
| 179 | } |
| 180 | } |
| 181 | return nullptr; |
| 182 | } |
| 183 | |
| 184 | status_t VolumeBase::create() { |
| 185 | CHECK(!mCreated); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 186 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 187 | mCreated = true; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 188 | status_t res = doCreate(); |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 189 | #if ENABLE_BINDER |
| 190 | auto listener = getListener(); |
| 191 | if (listener) listener->onVolumeCreated(getId(), |
| 192 | static_cast<int32_t>(mType), mDiskId, mPartGuid); |
| 193 | #else |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 194 | notifyEvent(ResponseCode::VolumeCreated, |
| 195 | StringPrintf("%d \"%s\" \"%s\"", mType, mDiskId.c_str(), mPartGuid.c_str())); |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 196 | #endif |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 197 | setState(State::kUnmounted); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 198 | return res; |
| 199 | } |
| 200 | |
| 201 | status_t VolumeBase::doCreate() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 202 | return OK; |
| 203 | } |
| 204 | |
| 205 | status_t VolumeBase::destroy() { |
| 206 | CHECK(mCreated); |
| 207 | |
| 208 | if (mState == State::kMounted) { |
| 209 | unmount(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 210 | setState(State::kBadRemoval); |
| 211 | } else { |
| 212 | setState(State::kRemoved); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 215 | #if ENABLE_BINDER |
| 216 | auto listener = getListener(); |
| 217 | if (listener) listener->onVolumeDestroyed(getId()); |
| 218 | #else |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 219 | notifyEvent(ResponseCode::VolumeDestroyed); |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 220 | #endif |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 221 | status_t res = doDestroy(); |
| 222 | mCreated = false; |
| 223 | return res; |
| 224 | } |
| 225 | |
| 226 | status_t VolumeBase::doDestroy() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 227 | return OK; |
| 228 | } |
| 229 | |
| 230 | status_t VolumeBase::mount() { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 231 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 232 | LOG(WARNING) << getId() << " mount requires state unmounted or unmountable"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 233 | return -EBUSY; |
| 234 | } |
| 235 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 236 | setState(State::kChecking); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 237 | status_t res = doMount(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 238 | if (res == OK) { |
| 239 | setState(State::kMounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 240 | } else { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 241 | setState(State::kUnmountable); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | return res; |
| 245 | } |
| 246 | |
| 247 | status_t VolumeBase::unmount() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 248 | if (mState != State::kMounted) { |
| 249 | LOG(WARNING) << getId() << " unmount requires state mounted"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 250 | return -EBUSY; |
| 251 | } |
| 252 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 253 | setState(State::kEjecting); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 254 | for (const auto& vol : mVolumes) { |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 255 | if (vol->destroy()) { |
| 256 | LOG(WARNING) << getId() << " failed to destroy " << vol->getId() |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 257 | << " stacked above"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 258 | } |
| 259 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 260 | mVolumes.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 261 | |
| 262 | status_t res = doUnmount(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 263 | setState(State::kUnmounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 264 | return res; |
| 265 | } |
| 266 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 267 | status_t VolumeBase::format(const std::string& fsType) { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 268 | if (mState == State::kMounted) { |
| 269 | unmount(); |
| 270 | } |
| 271 | |
| 272 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 273 | LOG(WARNING) << getId() << " format requires state unmounted or unmountable"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 274 | return -EBUSY; |
| 275 | } |
| 276 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 277 | setState(State::kFormatting); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 278 | status_t res = doFormat(fsType); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 279 | setState(State::kUnmounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 280 | return res; |
| 281 | } |
| 282 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 283 | status_t VolumeBase::doFormat(const std::string& fsType) { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 284 | return -ENOTSUP; |
| 285 | } |
| 286 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 287 | } // namespace vold |
| 288 | } // namespace android |