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 "VolumeBase.h" |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 18 | #include "Utils.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 19 | #include "VolumeManager.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 20 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 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 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 30 | using android::base::StringPrintf; |
| 31 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 32 | namespace android { |
| 33 | namespace vold { |
| 34 | |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 35 | VolumeBase::VolumeBase(Type type) |
| 36 | : mType(type), |
| 37 | mMountFlags(0), |
| 38 | mMountUserId(-1), |
| 39 | mCreated(false), |
| 40 | mState(State::kUnmounted), |
| 41 | mSilent(false) {} |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 42 | |
| 43 | VolumeBase::~VolumeBase() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 44 | CHECK(!mCreated); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 47 | void VolumeBase::setState(State state) { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 48 | mState = state; |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 49 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 50 | auto listener = getListener(); |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 51 | if (listener) { |
| 52 | listener->onVolumeStateChanged(getId(), static_cast<int32_t>(mState)); |
| 53 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 54 | } |
| 55 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 56 | status_t VolumeBase::setDiskId(const std::string& diskId) { |
| 57 | if (mCreated) { |
| 58 | LOG(WARNING) << getId() << " diskId change requires destroyed"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 59 | return -EBUSY; |
| 60 | } |
| 61 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 62 | mDiskId = diskId; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 63 | return OK; |
| 64 | } |
| 65 | |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 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 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 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"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 79 | return -EBUSY; |
| 80 | } |
| 81 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 82 | mMountFlags = mountFlags; |
| 83 | return OK; |
| 84 | } |
| 85 | |
| 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; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 93 | return OK; |
| 94 | } |
| 95 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 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 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 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 | |
| 116 | status_t VolumeBase::setPath(const std::string& path) { |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 117 | if (mState != State::kChecking) { |
| 118 | LOG(WARNING) << getId() << " path change requires state checking"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 119 | return -EBUSY; |
| 120 | } |
| 121 | |
| 122 | mPath = path; |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 123 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 124 | auto listener = getListener(); |
| 125 | if (listener) listener->onVolumePathChanged(getId(), mPath); |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 126 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 127 | return OK; |
| 128 | } |
| 129 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 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; |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 137 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 138 | auto listener = getListener(); |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 139 | if (listener) { |
| 140 | listener->onVolumeInternalPathChanged(getId(), mInternalPath); |
| 141 | } |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 142 | |
Jeff Sharkey | 1d6fbcc | 2015-04-24 16:00:03 -0700 | [diff] [blame] | 143 | return OK; |
| 144 | } |
| 145 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 146 | android::sp<android::os::IVoldListener> VolumeBase::getListener() { |
| 147 | if (mSilent) { |
| 148 | return nullptr; |
| 149 | } else { |
| 150 | return VolumeManager::Instance()->getListener(); |
| 151 | } |
| 152 | } |
| 153 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 154 | void VolumeBase::addVolume(const std::shared_ptr<VolumeBase>& volume) { |
| 155 | mVolumes.push_back(volume); |
| 156 | } |
| 157 | |
| 158 | void VolumeBase::removeVolume(const std::shared_ptr<VolumeBase>& volume) { |
| 159 | mVolumes.remove(volume); |
| 160 | } |
| 161 | |
| 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 | |
| 171 | status_t VolumeBase::create() { |
| 172 | CHECK(!mCreated); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 173 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 174 | mCreated = true; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 175 | status_t res = doCreate(); |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 176 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 177 | auto listener = getListener(); |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 178 | if (listener) { |
| 179 | listener->onVolumeCreated(getId(), static_cast<int32_t>(mType), mDiskId, mPartGuid); |
| 180 | } |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 181 | |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 182 | setState(State::kUnmounted); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 183 | return res; |
| 184 | } |
| 185 | |
| 186 | status_t VolumeBase::doCreate() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 187 | return OK; |
| 188 | } |
| 189 | |
| 190 | status_t VolumeBase::destroy() { |
| 191 | CHECK(mCreated); |
| 192 | |
| 193 | if (mState == State::kMounted) { |
| 194 | unmount(); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 195 | setState(State::kBadRemoval); |
| 196 | } else { |
| 197 | setState(State::kRemoved); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Jeff Sharkey | 814e9d3 | 2017-09-13 11:49:44 -0600 | [diff] [blame] | 200 | auto listener = getListener(); |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 201 | if (listener) { |
| 202 | listener->onVolumeDestroyed(getId()); |
| 203 | } |
Jeff Sharkey | cbe69fc | 2017-09-15 16:50:28 -0600 | [diff] [blame] | 204 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 205 | status_t res = doDestroy(); |
| 206 | mCreated = false; |
| 207 | return res; |
| 208 | } |
| 209 | |
| 210 | status_t VolumeBase::doDestroy() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 211 | return OK; |
| 212 | } |
| 213 | |
| 214 | status_t VolumeBase::mount() { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 215 | if ((mState != State::kUnmounted) && (mState != State::kUnmountable)) { |
| 216 | LOG(WARNING) << getId() << " mount requires state unmounted or unmountable"; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 217 | return -EBUSY; |
| 218 | } |
| 219 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 220 | setState(State::kChecking); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 221 | status_t res = doMount(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 222 | if (res == OK) { |
| 223 | setState(State::kMounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 224 | } else { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 225 | setState(State::kUnmountable); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | return res; |
| 229 | } |
| 230 | |
| 231 | status_t VolumeBase::unmount() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 232 | if (mState != State::kMounted) { |
| 233 | LOG(WARNING) << getId() << " unmount requires state mounted"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 234 | return -EBUSY; |
| 235 | } |
| 236 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 237 | setState(State::kEjecting); |
Chih-Hung Hsieh | 11a2ce8 | 2016-07-27 14:11:02 -0700 | [diff] [blame] | 238 | for (const auto& vol : mVolumes) { |
Jeff Sharkey | 3161fb3 | 2015-04-12 16:03:33 -0700 | [diff] [blame] | 239 | if (vol->destroy()) { |
Paul Crowley | 8915d62 | 2018-09-18 15:14:18 -0700 | [diff] [blame] | 240 | LOG(WARNING) << getId() << " failed to destroy " << vol->getId() << " stacked above"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 241 | } |
| 242 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 243 | mVolumes.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 244 | |
| 245 | status_t res = doUnmount(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 246 | setState(State::kUnmounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 247 | return res; |
| 248 | } |
| 249 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 250 | status_t VolumeBase::format(const std::string& fsType) { |
Jeff Sharkey | 0fd9535 | 2015-04-04 21:38:59 -0700 | [diff] [blame] | 251 | 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 Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 257 | return -EBUSY; |
| 258 | } |
| 259 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 260 | setState(State::kFormatting); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 261 | status_t res = doFormat(fsType); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 262 | setState(State::kUnmounted); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 263 | return res; |
| 264 | } |
| 265 | |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 266 | status_t VolumeBase::doFormat(const std::string& fsType) { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 267 | return -ENOTSUP; |
| 268 | } |
| 269 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 270 | } // namespace vold |
| 271 | } // namespace android |